how to append a numpy matrix into an empty numpy array

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I want to append a numpy array(matrix) into an array through a loop

data=[[2 2 2] [3 3 3]] Weights=[[4 4 4] [4 4 4] [4 4 4]] All=np.array([])   for i in data:     #i=[2 2 2 ]  #for example     h=i*Weights              #h=[[8 8 8][8 8 8][8 8 8]]      All=np.concatenate((All,h),axis=0)                 

I ge this error:

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

I want "All" variable to be

[[8 8 8][8 8 8][8 8 8] [12 12 12][12 12 12][12 12 12]] 

Any way how I can add "h" to "All" through the loop ?

回答1:

Option 1: Reshape your initial All array to 3 columns so that the number of columns match h:

All=np.array([]).reshape((0,3))  for i in data:     h=i*Weights           All=np.concatenate((All,h))  All #array([[  8.,   8.,   8.], #       [  8.,   8.,   8.], #       [  8.,   8.,   8.], #       [ 12.,  12.,  12.], #       [ 12.,  12.,  12.], #       [ 12.,  12.,  12.]]) 

Option 2: Use a if-else statement to handle initial empty array case:

All=np.array([]) for i in data:     h=i*Weights           if len(All) == 0:         All = h     else:         All=np.concatenate((All,h))  All #array([[ 8,  8,  8], #       [ 8,  8,  8], #       [ 8,  8,  8], #       [12, 12, 12], #       [12, 12, 12], #       [12, 12, 12]]) 

Option 3: Use itertools.product():

import itertools np.array([i*j for i,j in itertools.product(data, Weights)])  #array([[ 8,  8,  8], #       [ 8,  8,  8], #       [ 8,  8,  8], #       [12, 12, 12], #       [12, 12, 12], #       [12, 12, 12]]) 


回答2:

Adam, how about just using a pair of nested loops? I believe this code will do what you want.

import numpy as np data = ([2,2,2],[3,3,3]) weights = ([4,4,4],[4,4,4],[4,4,4])  output=np.array([]) for each_array in data:     for weight in weights:             each_multiplication = np.multiply(each_array, weight)             output = np.append(output,each_multiplication)  print output 

np.multiply() performs element wise multiplication instead of matrix multiplication. As best as I can understand from your sample input and output, this is what you're trying to accomplish.



回答3:

It may not be the best solution but it seems to work.

data = np.array([[2, 2, 2], [3, 3, 3]]) Weights = np.array([[4, 4, 4], [4, 4, 4], [4, 4, 4]]) All = []  for i in data:     for j in Weights:         h = i * j         All.append(h)  All = np.array(All) 

I'd like to say it's not the best solution because it appends the result to a list and at the end converts the list in a numpy array but it works good for small applications. I mean if you have to do heavy calculations like this it's i would consider finding another method. Anyway with this method you don't have to think about the number conversions from floating point. Hope this helps.



回答4:

A preferred way of constructing an array with a loop is to collect values in a list, and perform the concatenate once, at the end:

In [1025]: data Out[1025]:  array([[2, 2, 2],        [3, 3, 3]]) In [1026]: Weights Out[1026]:  array([[4, 4, 4],        [4, 4, 4],        [4, 4, 4]]) 

Append to a list is much faster than repeated concatenate; plus it avoids the 'empty` array shape issue:

In [1027]: alist=[] In [1028]: for row in data:       ...:     alist.append(row*Weights) In [1029]: alist Out[1029]:  [array([[8, 8, 8],         [8, 8, 8],         [8, 8, 8]]), array([[12, 12, 12],         [12, 12, 12],         [12, 12, 12]])]  In [1031]: np.concatenate(alist,axis=0) Out[1031]:  array([[ 8,  8,  8],        [ 8,  8,  8],        [ 8,  8,  8],        [12, 12, 12],        [12, 12, 12],        [12, 12, 12]]) 

You can also join the arrays on a new dimension with np.array or np.stack:

In [1032]: np.array(alist) Out[1032]:  array([[[ 8,  8,  8],         [ 8,  8,  8],         [ 8,  8,  8]],         [[12, 12, 12],         [12, 12, 12],         [12, 12, 12]]]) In [1033]: _.shape Out[1033]: (2, 3, 3) 

I can construct this 3d version with a simple broadcasted multiplication - no loops

In [1034]: data[:,None,:]*Weights[None,:,:] Out[1034]:  array([[[ 8,  8,  8],         [ 8,  8,  8],         [ 8,  8,  8]],         [[12, 12, 12],         [12, 12, 12],         [12, 12, 12]]]) 

Add a .reshape(-1,3) to that to get the (6,3) version.

np.repeat(data,3,axis=0)*np.tile(Weights,[2,1]) also produces the desired 6x3 array.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!