2016-05-01 4 views
-2

Ich habe eine 3x10-Matrix (in Form eines numply-Arrays) und möchte sie mit einer 3x3-Transformationsmatrix multiplizieren. Ich glaube nicht, dass np.dot die volle Matrixmultiplikation durchführt. Gibt es eine Methode für diese Multiplikation mit Arrays?Numpy, multiplizieren Sie 3x3 Array mit 3x10 Array?

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) 

Gibt es eine Punktfunktion, die die gesamte Matrix-Multiplikation der Fall ist, nicht nur "For N dimensions it is a sum product over the last axis of a and the second-to-last of b"

+1

Die [Dokumentation] (http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.dot.html) besagt, dass für 2D-Arrays "np.dot" äquivalent ist Matrix-Multiplikation ... – mgilson

Antwort

2

Sie Kommas in den letzten beiden Einträge von transf fehlt. Fix sie, und Sie werden die Matrixmultiplikation erhalten, wie man es erwarten würde:

# 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]]) 

Da das erste Array nicht tatsächlich ein legitimes 2-D-Array ist, np.dot nicht Matrixmultiplikation durchführen.

1

Es ist einfach durch * Operator, aber Sie müssen die matrix anstelle von array definieren.

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 

Ich hoffe, es hilft.

+0

Die Matrix * ist die gleiche wie der Array-Punkt. – hpaulj

+0

Und neuere Python/Numpys haben einen '@' Operator, der auf die gleiche Weise funktioniert. – hpaulj

+0

@hpaulj Haben Sie eine Quelle dafür? Ich wusste nicht, dass '@' arbeitete als ein Operator in Python – KevinOrr