np.array()与np.mat()均可应用于生成矩阵
1.np.array()生成数据格式为ndarray,np.mat()生成数据格式为matrix
注意:一维时ndarray可以当做列表,与列表无异
下面是两者产生1~32的8×4的矩阵
import numpy as np
import math
n=np.mat(range(1,33)).reshape(8,4)
m=np.array(range(1,33)).reshape(8,4)
sum=n+m
print(type(n))
print(type(m))
print(n)
print(m)
print(sum)
结果如下:
<class 'numpy.matrix'>
<class 'numpy.ndarray'>
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]
[17 18 19 20]
[21 22 23 24]
[25 26 27 28]
[29 30 31 32]]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]
[17 18 19 20]
[21 22 23 24]
[25 26 27 28]
[29 30 31 32]]
[[ 2 4 6 8]
[10 12 14 16]
[18 20 22 24]
[26 28 30 32]
[34 36 38 40]
[42 44 46 48]
[50 52 54 56]
[58 60 62 64]]
另外,下面是其它两位程序员对两者的运算区别的总结,不再赘述
https://www.cnblogs.com/Bella2017/p/10759614.html
https://www.cnblogs.com/Bella2017/p/10759614.html