ndarray创建方式
x1 = np.array([1,2,3,4])
[1 2 3 4]
x2 = np.array((5,6,7,8))
[5 6 7 8]
x3 = np.array([[1,2],[3,4],(0.2,0.5)])
[[1. 2. ]
[3. 4. ]
[0.2 0.5]]
2.arange ones zeros等函数
y1 = np.arange(10)
[0 1 2 3 4 5 6 7 8 9]
y2 = np.ones((2,3,6))
[[[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]]
[[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1.]]]
y3 = np.zeros((3,6),dtype=np.int32)
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
y4 = np.eye(5)
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
3.linspace concatenate
z1 = np.linspace(1,10,4)
[ 1. 4. 7. 10.]
z2 = np.linspace(1,10,4,endpoint=False)
[1. 3.25 5.5 7.75]
z = np.concatenate((z1,z2))
[ 1. 4. 7. 10. 1. 3.25 5.5 7.75]
ndarray维度变换
a = np.ones((2,3,4),dtype=np.int32)
print(a.reshape((3,8)))
[[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]]
print(a.flatten())
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
ndarray转为列表
print(a.tolist())
[[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]]
待更新…