我使用了numpy的dot函数来计算2D和1D数组的乘积。我注意到当2D数组是matrix类型而1D数组是ndarray类型时,
dot
函数返回的结果与我传递一个类型为ndarray
的2D数组时不同。
问题:为什么结果不同?
简短示例
import numpy as np
a=[[1,2],
[3,4],
[5,6]]
e=np.array([1,2])
b=np.array(a)
print("Ndarrray:%s"%(type(b)))
print(b)
print("Dim of ndarray %d"%(np.ndim(b)))
be=np.dot(b,e)
print(be)
print("Dim of array*array %d\n"%(np.ndim(be)))
c=np.mat(a)
print("Matrix:%s"%(type(c)))
print(c)
print("Dim of matrix %d"%(np.ndim(c)))
ce=np.dot(c,e)
print(ce)
print("Dim of matrix*array %d"%(np.ndim(ce)))
Ndarrray:<class 'numpy.ndarray'>
[[1 2]
[3 4]
[5 6]]
Dim of ndarray 2
[ 5 11 17]
Dim of array*array 1
Matrix:<class 'numpy.matrix'>
[[1 2]
[3 4]
[5 6]]
Dim of matrix 2
[[ 5 11 17]]
Dim of matrix*array 2