rksh
rksh

Reputation: 4050

Joining Array In Python

Hi I want to join multiple arrays in python, using numpy to form multidimensional arrays, it's inside of a for loop, this is a pseudocode

import numpy as np
h = np.zeros(4)
for x in range(3):
   x1 = some array of length of 4 returned from a previous function (3,5,6,7)
   h = np.concatenate((h,x1), axis =0)

The first iteration goes fine, but during the second iteration on the for loop I get the following error,

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

The output array should look something like this

 [[0,0,0,0],[3,5,6,7],[6,3,6,7]]

etc

So how can I join the arrays?

Thanks

Upvotes: 0

Views: 2527

Answers (3)

hpaulj
hpaulj

Reputation: 231355

It is best to collect values in a list, and perform the concatenate or array creation once, at the end.

h = [np.zeros(4)]
for x in range(3):
   x1 = some array of length of 4 returned from a previous function (3,5,6,7)
   h = h.append(x1)
h = np.array(h)
# or h = np.vstack(h)

All the concatenate/stack/array functions takes a list of multiple items. It is faster to append to a list than to do a concatenate of 2 items.

======================

Let's try your approach step by step:

In [189]: h=np.zeros(4)
In [190]: h
Out[190]: array([ 0.,  0.,  0.,  0.])    # 1d array (4,) shape
In [191]: x1=np.array([3,5,6,7])     # another 1d
In [192]: h1=np.concatenate((h,x1),axis=0)
In [193]: h1
Out[193]: array([ 0.,  0.,  0.,  0.,  3.,  5.,  6.,  7.])
In [194]: h1.shape
Out[194]: (8,)       # also a 1d array, but with 8 items
In [195]: x1=np.array([6,3,6,7])
In [196]: h1=np.concatenate((h1,x1),axis=0)
In [197]: h1
Out[197]: array([ 0.,  0.,  0.,  0.,  3.,  5.,  6.,  7.,  6.,  3.,  6.,  7.])

In this case I'm adding (4,) arrays one after the other, still getting a 1d array.

If I go back an create x1 as 2d (1,4):

In [198]: h=np.zeros(4)
In [199]: x1=np.array([[6,3,6,7]])
In [200]: h1=np.concatenate((h,x1),axis=0)
...
ValueError: all the input arrays must have same number of dimensions

I get this dimension error right away.

The fact that you get the error on the 2nd iteration suggests that the 1st x1 is (4,), but the 2nd is 2d.

When you have dimensions errors like this, check the shapes.

vstack adds dimensions to the inputs, as needed, so you can build 2d arrays:

In [207]: h=np.zeros(4)
In [208]: x1=np.array([3,5,6,7])
In [209]: h=np.vstack((h,x1))
In [210]: h
Out[210]: 
array([[ 0.,  0.,  0.,  0.],
       [ 3.,  5.,  6.,  7.]])
In [211]: x1=np.array([6,3,6,7])
In [212]: h=np.vstack((h,x1))
In [213]: h
Out[213]: 
array([[ 0.,  0.,  0.,  0.],
       [ 3.,  5.,  6.,  7.],
       [ 6.,  3.,  6.,  7.]])

Upvotes: 1

steve chang
steve chang

Reputation: 54

There are multiple ways of doing this. I'll list a few examples:

First, we import numpy and define a function that generates those arrays of length 4.

import numpy as np

def previous_function_returning_array_of_length_4(x):
    return np.array(range(4)) + x

The first way involves creating a list of arrays, then calling numpy.array() to convert the list to a 2D array.

h0 = np.zeros(4)
arrays = [h0]
for x in range(3):
    x1 = previous_function_returning_array_of_length_4(x)
    arrays.append(x1)

h = np.array(arrays)

You can do the same with np.vstack():

h0 = np.zeros(4)
arrays = [h0]
for x in range(3):
    x1 = previous_function_returning_array_of_length_4(x)
    arrays.append(x1)

h = np.vstack(arrays)

Alternatively, if you know how many arrays you are going to create, you can create the 2D array first and fill in the values:

h = np.zeros((4, 4))
for ii in range(3):
    x1 = previous_function_returning_array_of_length_4(ii)
    h[ii + 1, ...] = x1

There are more ways, but hopefully, this will give you an idea of what to do.

Upvotes: 1

wander95
wander95

Reputation: 1366

You need to use vstack. It allows you to stack arrays. You take a sequence of arrays and stack them vertically to make a single array

 import numpy as np  
 h = np.zeros(4)
 for x in range(3):
     x1 = [3,5,6,7]
     h = np.vstack((h,x1))
     # not h = np.concatenate((h,x1), axis =0)

  print h

Output:

[[ 0.  0.  0.  0.]
 [ 3.  5.  6.  7.]
 [ 3.  5.  6.  7.]
 [ 3.  5.  6.  7.]]

more edits later. If you do want to use cocatenate only, you can do the following way as well:

 import numpy as np
 h1 = np.zeros(4)

 for x in range(3):
      x1 = np.array([3,5,6,7])
      h1= np.concatenate([h1,x1.T], axis =0)

 print h1.shape
 print h1.reshape(4,4)

Output:

(16,)
[[ 0.  0.  0.  0.]
 [ 3.  5.  6.  7.]
 [ 3.  5.  6.  7.]
 [ 3.  5.  6.  7.]]

Both have different applications. You can choose according to your need.

Upvotes: 1

Related Questions