将数列转换成矩阵
arr1 = np.array([[2,3,4],
[3,4,5]])
print (arr1)
结果:[[2 3 4]
[3 4 5]]
生成零矩阵
arr2 = np.zeros((2,3),dtype=np.int64).#创建一个两行三列的零矩阵,并定义为int64位
print(arr2)
print(arr2.dtype)
结果:
[[0 0 0]
[0 0 0]]
int64
reshape函数运用
arr3 = arr2.reshape(3,2)#通过reshpe转换为制定行列
print (arr3)
arr.shape,arr.size,ones,zeros,empty矩阵
arr4 = np.ones((2,3), dtype=np.float32)#全为一的矩阵
arr5 = np.empty((2,3))#接近于零的矩阵
print(arr4)
print(arr5)
结果:
[[1. 1. 1.]
[1. 1. 1.]]
[[0.0078125 0.0078125 0.0078125]
[0.0078125 0.0078125 0.0078125]]
arange的运用
arr6 = np.arange(2,14,2)#生成从2-14的矩阵,左闭右开,最后一个数字为步长
arr7 = np.arange(2,14)#不写步长默认为1
print(arr6)
print(arr7)
print(arr6.reshape(2,3))
结果:
[ 2 4 6 8 10 12]
[ 2 3 4 5 6 7 8 9 10 11 12 13]
[[ 2 4 6]
[ 8 10 12]]
np.linspace()
arr8 = np.linspace(1,12,6).reshape(2,3)#从1-到12,个数为3
print(arr8)
结果:
[[ 1. 3.2 5.4]
[ 7.6 9.8 12. ]]