Wie kann ich eine matrix-Transformation auf jede Zeile ein NumPy-array effizient?

Sagen wir, ich habe ein 2d-ndarray NumPy, etwa so:

[[ 0, 1, 2, 3 ],
 [ 4, 5, 6, 7 ],
 [ 8, 9, 10, 11 ]]

Konzeptionell zu sprechen, was ich tun will, ist dies:

For each row:
    Transpose the row
    Multiply the transposed row by a transformation matrix
    Transpose the result
    Store the result in the original ndarray, overwriting the original row data

Ich habe eine extrem langsam, brute-force-Methode, die funktional erreicht dies:

import numpy as np
transform_matrix = np.matrix( /* 4x4 matrix setup clipped for brevity */ )
for i, row in enumerate( data ):
    tr = row.reshape( ( 4, 1 ) )
    new_row = np.dot( transform_matrix, tr )
    data[i] = new_row.reshape( ( 1, 4 ) )

Jedoch, dies scheint, wie die Art der operation, die NumPy sollte gut mit. Ich gehe davon aus, dass - als jemand, der neu zu NumPy - ich bin nur fehlt etwas grundlegendes in der Dokumentation. Alle Zeiger?

Beachten Sie, dass wenn es schneller ist, erstellen Sie eine neue ndarray anstatt Bearbeiten in-place, das kann funktionieren für das, was ich Tue, auch; die Geschwindigkeit der operation ist die primäre Sorge.

InformationsquelleAutor user3089880 | 2013-12-11
Schreibe einen Kommentar