更新时间:2023-02-26 18:39:30
怎么样:
from numpy.core.umath_tests import inner1d
Z = inner1d(X,Y)
例如:
X = np.random.normal(size=(10,5))
Y = np.random.normal(size=(10,5))
Z1 = inner1d(X,Y)
Z2 = [np.dot(X[k],Y[k]) for k in range(10)]
print np.allclose(Z1,Z2)
返回True
修改,因为我没有看到问题的3D部分
Edit Correction since I didn't see the 3D part of the question
from numpy.core.umath_tests import matrix_multiply
X = np.random.normal(size=(10,5,3))
Y = np.random.normal(size=(10,3,5))
Z1 = matrix_multiply(X,Y)
Z2 = np.array([np.dot(X[k],Y[k]) for k in range(10)])
np.allclose(Z1,Z2) # <== returns True
之所以可行,是因为(作为文档字符串状态)matrix_multiply
提供了
This works because (as the docstring states), matrix_multiply
provides
matrix_multiply(x1,x2 [,out])矩阵
matrix_multiply(x1, x2[, out]) matrix
最后两个维度上的乘法