Numpy,用3x3数组乘以3x10数组?

2024-06-26 15:01:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个3x10矩阵(以numpy数组的形式)并且想用一个3x3变换矩阵乘以它。我不认为美国运输部做全矩阵乘法。有没有一种用数组进行乘法的方法?在

transf = np.array([ [0.1, -0.4, 0],[0.9, 0.75, -0.1],[0.5, 0.75, -0.9] ])

one = [0,1,2,3,4,5,6,8,9]
two = [1,2,3,4,5,6,8,9,10]
three = [2,3,4,5,6,8,9,10,11]

data = np.array([ one, two, three ])

new_data = np.dot(transf,data)

有没有一个点函数可以完成整个矩阵的乘法,而不仅仅是"For N dimensions it is a sum product over the last axis of a and the second-to-last of b"


Tags: ofthenumpydatanp矩阵数组array
2条回答

它只是通过*运算符实现的,但是您需要定义matrix,而不是{}。在

import numpy as np
transf = np.matrix([ [1,2,3],[4,5,6],[1,2,3] ])     # 3x3 matrix
data = np.matrix([[2], [3], [4] ])      # 3x1 matrix

print transf * data

希望有帮助。在

transf的最后两个条目中缺少逗号。修正它们,你将得到你所期望的矩阵乘法:

# Missing commas between 0.75 and -0.1, 0.75 and -0.9.
transf = np.array([ [0.1, -0.4, 0],[0.9, 0.75 -0.1],[0.5, 0.75 -0.9] ])

# Fix with commas
transf = np.array([ [0.1, -0.4, 0],[0.9, 0.75, -0.1],[0.5, 0.75, -0.9]])

因为第一个数组实际上不是合法的二维数组,np.dot不能执行矩阵乘法。在

相关问题 更多 >