numpy知识点
程序员文章站
2022-05-28 19:46:51
...
一,np.squeeze
一,np.squeeze
"""
np.squeeze 删除单维度的条 对多维度无效
"""
import numpy as np
a=np.array([[1,2],[3,4],[4,5]])
print(a)
print(a.shape)
b=np.squeeze(a)
print(b)
c=a.reshape(1,6,1)
print(c)
print(np.squeeze(c))
print(np.squeeze(c).shape)
print(np.squeeze(c,axis=0))
print(np.squeeze(c,axis=0).shape)
print(np.squeeze(c,axis=1))
print(np.squeeze(c,axis=1).shape)
维度不为1,报错。
print(np.squeeze(c,axis=2))
print(np.squeeze(c,axis=2).shape)
二,np.newaxis增加维度
import numpy as np
a=np.arange(1,5)
print(a)
print(a.reshape([-1,1]))
b=a[:,np.newaxis]
print(b)
print(b.shape)
三,np.random
import numpy as np
"""
0~1之间产生随机值
"""
a=np.random.rand(3,2)
print(a)
"""
产生标准正态分布的值
"""
a=np.random.randn(3,2)
print(a)
"""
产生随机整数1~3之间
"""
a=np.random.randint(low=1,high=3,size=(3,2))
print(a)
"""
产生高斯分布:均值 方差
"""
a=np.random.normal(loc=0,scale=1,size=(3,2))
print(a)
四,np.logspace
import numpy as np
#等比数列 9/(5-1)=2.25 10^0 10^2.25 10^5.5 10^7.75 10^9
a=np.logspace(0,9,5)
print(a)
五,等差数列
a=np.linspace(2.0, 3.0, num=5)
print(a)
b=np.linspace(2.0, 3.0, num=5, endpoint=False)
print(b)
c=np.linspace(2.0, 3.0, num=5, retstep=True)
print(c)
六:np.argmax,np.sum
a=np.array([[1,0,0],[0,1,0],[0,0,1],[0,1,0]])
print(a)
print(np.argmax(a,1))#对行找最大值索引
b=np.array([[1,0,0],[0,1,0],[1,0,0],[0,0,1]])
print(b)
print(np.argmax(b,1))
print(np.argmax(a,1)==np.argmax(b,1))
print(np.sum(np.argmax(a,1)==np.argmax(b,1)))
np.sum
import numpy as np
a=np.array([[[[1,2],[1,3],[1,4]],
[[1,7],[1,6],[1,5]],
[[1,2],[1,3],[1,4]]]])
print(a.shape)
print(np.sum(a,axis=3))
print(np.sum(a,axis=3).shape)
print(np.sum(a,axis=3,keepdims=True))
print(np.sum(a,axis=3,keepdims=True).shape)
保持维度故变为(1,3,3,1)
七,np.stack,np.hstack,np.vstack
np.stack二维情况
import numpy as np
a=[[1,2,3],
[4,5,6]]
print("列表a如下:")
print(a)
print("增加一维,新维度的下标为0")
c=np.stack(a,axis=0)
print(c)
print("增加一维,新维度的下标为1")
c=np.stack(a,axis=1)
print(c)
a=[[1,2,3],
[4,5,6]]
b=[[1,2,3],
[4,5,6]]
c=[[1,2,3],
[4,5,6]]
print("a=",a)
print("b=",b)
print("c=",c)
print("增加一维,新维度的下标为0")
d=np.stack((a,b,c),axis=0)
print(d)
print(d.shape)
print("增加一维,新维度的下标为1")
d=np.stack((a,b,c),axis=1)
print(d)
print(d.shape)
print("增加一维,新维度的下标为2")
d=np.stack((a,b,c),axis=2)
print(d)
print(d.shape)
np.hstack按照水平方向连接
import numpy as np
a=[[1],[2],[3]]
b=[[1],[2],[3]]
c=[[1],[2],[3]]
d=[[1],[2],[3]]
print(np.hstack((a,b,c,d)))
np.vstack按垂直方向连接
import numpy as np
a=[[1],[2],[3]]
b=[[1],[2],[3]]
c=[[1],[2],[3]]
d=[[1],[2],[3]]
print(np.vstack((a,b,c,d)))
把sober算子变成两个通道的sober算子,其中生成的2用作刚好是输入的channel
import tensorflow as tf
import numpy as np
fx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]).astype(np.float32)
fy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]).astype(np.float32)
fx = np.stack((fx, fx), axis=2)
print(fx)
# fy = np.stack((fy, fy), axis=2)
# print(fy)
fx = np.reshape(fx, (3, 3, 2, 1))
print(fx)
# fy = np.reshape(fy, (3, 3, 2, 1))
tf_fx = tf.Variable(tf.constant(fx))
八,np.array
>>> a
array([0, 2, 1])
>>> scores
array([[1, 2, 3],
[2, 3, 4],
[4, 5, 6]])
>>> scores[a]
array([[1, 2, 3],
[4, 5, 6],
[2, 3, 4]])
一个样本对应一列,下面的代码可以用来寻找每个样本标签量对应的数值。
九,np.concatenate
import numpy as np
a = np.array([[1, 2], [3, 4]])
b=np.array([[5,6]])
c=np.concatenate((a, b), axis=0)
print(c)
c=np.concatenate((a, b.T), axis=1)
print(c)
c=np.concatenate((a, b.T), axis=None)
print(c)
对于列表也可以
a=np.concatenate([[[1,2]],[[3,4]]],axis=0) print(a)
十, .transpose()
import numpy as np
a=np.array([[1],[2],[3],[4],[5],[6]])
print(a)
b=a[:2,:].transpose()
c=a[2:4,:].transpose()
d=a[4:,:].transpose()
print(b,c,d)
e=np.append(b,c,axis=0)
a=np.append(e,d,axis=0)
print(a)
十一,one_hot
import numpy as np
#转换one-hot
def convert_to_one_hot(label):
n_classes=max(label)+1
label = np.eye(n_classes)[label.reshape(-1)]
return label
label=[1,0,2,3,0]
Y=convert_to_one_hot(np.array(label))
print(Y)
十二,np广播机制
上一篇: JS特效——音乐钢琴
下一篇: JS特效——旋转木马轮播图