假设我有一个2d NumPy ndarray,就像这样:
[[ 0, 1, 2, 3 ], [ 4, 5, 6, 7 ], [ 8, 9, 10, 11 ]]
从概念上讲,我想要做的是:
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
我有一个极其缓慢,强力的方法,在功能上实现了这一点:
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 ) )
然而,这似乎是NumPy应该做的那种操作.我认为 - 作为NumPy的新手 - 我只是遗漏了文档中的一些基本内容.有什么指针吗?
请注意,如果创建新的ndarray更快,而不是就地编辑它,那么这也适用于我正在做的事情; 操作速度是首要关注的问题.