2016-05-01 63 views
-2

我有一個3x10矩陣(以numpy數組的形式),並希望乘以3x3變換矩陣。我不認爲np.dot正在進行全矩陣乘法。有沒有一種方法與數組進行乘法運算?Numpy,乘3x3數組乘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"

+1

[documentation](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.dot.html)指出,對於2d陣列,'np.dot'等同於矩陣乘法... – mgilson

回答

2

你錯過了逗號的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]]) 

因爲第一陣列實際上不是一個合法的2-d陣列,np.dot不能執行矩陣乘法。

1

這只是*運營商,但您將需要定義matrix而不是array

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 

希望它有幫助。

+0

矩陣*與陣列點相同。 – hpaulj

+0

而較新的Python/numpys有一個「@」運算符,它的行爲方式相同。 – hpaulj

+0

@hpaulj你有這方面的來源嗎?我不知道 '@'在Python中作爲運算符 – KevinOrr