简体   繁体   English

将numpy.ndarray的列表转换为矩阵以执行乘法

[英]turning a list of numpy.ndarray to a matrix in order to perform multiplication

i have vectors of this form : 我有这种形式的载体:

test=np.linspace(0,1,10)

i want to stack them horizontally in order to make a matrix . 我想将它们水平堆叠以形成矩阵。 problem is that i define them in a loop so the first stack is between an empty matrix and the first column vector , which gives the following error: 问题是我在一个循环中定义它们,因此第一个堆栈在空矩阵和第一列向量之间,从而产生以下错误:

ValueError: all the input arrays must have same number of dimensions

bottom line - i have a for loop that with every iteration creates a vector p1 and i want to add it to a final matrix of the form : [p1 p2 p3 p4] which i could then do matrix operations on such as multiplying by the transposed etc 底线-我有一个for循环,每次迭代都会创建一个向量p1,我想将其添加到以下形式的最终矩阵中:[p1 p2 p3 p4]然后我可以对其进行矩阵运算,例如乘以转置等等

If you've got a list of 1D arrays that you want horizontally stacked, you could convert them all to column first, but it's probably easier to just vertically stack them and then transpose: 如果您有要水平堆叠的一维数组的列表,可以先将它们全部转换为列,但是垂直堆叠然后转置可能会更容易:

In [6]: vector_list = [np.linspace(0, 1, 10) for _ in range(3)]

In [7]: np.vstack(vector_list).T
Out[7]:
array([[0.        , 0.        , 0.        ],
       [0.11111111, 0.11111111, 0.11111111],
       [0.22222222, 0.22222222, 0.22222222],
       [0.33333333, 0.33333333, 0.33333333],
       [0.44444444, 0.44444444, 0.44444444],
       [0.55555556, 0.55555556, 0.55555556],
       [0.66666667, 0.66666667, 0.66666667],
       [0.77777778, 0.77777778, 0.77777778],
       [0.88888889, 0.88888889, 0.88888889],
       [1.        , 1.        , 1.        ]])

How did you get this dimension error? 您是怎么得到这个尺寸错误的? What does empty array have to do with it? empty array与它有什么关系?

A list of arrays of the same length: 相同长度的数组列表:

In [610]: alist = [np.linspace(0,1,6), np.linspace(10,11,6)]                                          
In [611]: alist                                                                                       
Out[611]: 
[array([0. , 0.2, 0.4, 0.6, 0.8, 1. ]),
 array([10. , 10.2, 10.4, 10.6, 10.8, 11. ])]

Several ways of making an array from them: 用它们制作数组的几种方法:

In [612]: np.array(alist)                                                                             
Out[612]: 
array([[ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ],
       [10. , 10.2, 10.4, 10.6, 10.8, 11. ]])

In [614]: np.stack(alist)                                                                             
Out[614]: 
array([[ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ],
       [10. , 10.2, 10.4, 10.6, 10.8, 11. ]])

If you want to join them in columns, you can transpose one of the above, or use: 如果要在列中加入它们,可以转置上述之一,或使用:

In [615]: np.stack(alist, axis=1)                                                                     
Out[615]: 
array([[ 0. , 10. ],
       [ 0.2, 10.2],
       [ 0.4, 10.4],
       [ 0.6, 10.6],
       [ 0.8, 10.8],
       [ 1. , 11. ]])

np.column_stack is also handy. np.column_stack也很方便。

In newer numpy versions you can do: 在较新的numpy版本中,您可以执行以下操作:

In [617]: np.linspace((0,10),(1,11),6)                                                                
Out[617]: 
array([[ 0. , 10. ],
       [ 0.2, 10.2],
       [ 0.4, 10.4],
       [ 0.6, 10.6],
       [ 0.8, 10.8],
       [ 1. , 11. ]])

You don't specify how you create the 'empty array' and how you attempt to stack. 您没有指定如何创建“空数组”以及如何尝试堆叠。 I can't exactly recreate the error message (full traceback would have helped). 我无法完全重新创建错误消息(完全回溯会有所帮助)。 But given that message did you check the number of dimensions of the inputs? 但是鉴于该消息,您是否检查了输入的维数? Did they match? 他们匹配吗?

Array stacking in a loop is tricky. 循环中的数组堆叠非常棘手。 You have to pay close attention to the shapes, especially of the initial 'empty' array. 您必须密切注意形状,尤其是初始“空”数组的形状。 There isn't a close analog to the empty list [] . 没有一个类似于空列表[]模拟。 np.array([]) is 1d with shape (1,). np.array([])为1d,形状为(1,)。 np.empty((0,6)) is 2d with shape (0,6). np.empty((0,6))是形状为(0,6)的2d。 Also all the stacking functions create a new array with each call (non operate in-place), so they are inefficient (compared to list append). 同样,所有堆栈函数都会在每次调用时创建一个新数组(无法就地操作),因此它们效率低下(与列表追加相比)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM