2016-05-01 6 views
-2

私は3x10の行列(数値配列の形式)を持ち、3x3変換行列でそれを乗算したいと考えています。私はnp.dotが完全な行列の乗算をしているとは思わない。この乗法を配列で行う方法はありますか?ナンシー、3x3アレイを3x10アレイで掛けますか?

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) 

全体行列の乗算を行うドット関数は、あなたがtransfの最後の2つのエントリでコンマが欠落しているだけで"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)には、2次元配列の場合、 'np.dot'は行列乗算... – mgilson

答えて

2

、そこではありません。それらを修正し、あなたが期待どおりには行列の乗算を取得します:

# 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次元配列ではありませんので、np.dotは行列の乗算を実行することはできません。

1

単純に*演算子ですが、arrayではなく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 

希望します。

+0

行列*は配列の点と同じです。 – hpaulj

+0

そして、新しいPython/numpysにも同じように動作する '@'演算子があります。 – hpaulj

+0

@hpauljあなたにはその源がありますか?私は '@'がPythonの演算子として働いていたことを知らなかった – KevinOrr