7

I have a loop that generates numpy arrays:

for x in range(0, 1000):
   myArray = myFunction(x)

The returned array is always one dimensional. I want to combine all the arrays into one array (also one dimensional.

I tried the following, but it failed

allArrays = []
for x in range(0, 1000):
   myArray = myFunction(x)
   allArrays += myArray

The error is ValueError: operands could not be broadcast together with shapes (0) (9095). How can I get that to work?

For instance these two arrays:

[ 234 342 234 5454 34 6]
[ 23 2 1 4 55 34]

Shall be merge into this array:

[ 234 342 234 5454 34 6 23 2 1 4 55 34 ]
2
  • I want to concatenate them. Commented Mar 26, 2014 at 13:51
  • 1
    maybe concatenate((a,b),1) or hstack((a,b)) See also stackoverflow.com/questions/18404077/… Commented Mar 26, 2014 at 13:53

3 Answers 3

12

You probably mean

allArrays = np.array([])
for x in range(0, 1000):
    myArray = myFunction(x)
    allArrays = np.concatenate([allArrays, myArray])

A more concise approach (see wims answer) is to use a list comprehension,

allArrays = np.concatenate([myFunction(x) for x in range]) 
Sign up to request clarification or add additional context in comments.

4 Comments

If I use your first example. I get the error TypeError: only length-1 arrays can be converted to Python scalars
@ustroetz: Thanks for the catch, forgot the brackets as concatenate takes the arrays to be concatenated as a list.
It doesn't work, it says all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 3 dimension(s)
If you check the OP, this question and answer are for the case of 1-dimensional output arrays. It's clear from your comment that the output arrays you have are not always 1-dimensional (notice how the error message references a 3-dimensional array).
11

You should know the shape of returned array. Suppose, myArray.shape = (2, 4) Then

allArrays = np.empty((0, 4))
for x in range(0, 1000):
    myArray = myFunction(x)
    allArrays = np.append(allArrays, myArray, axis = 0)

2 Comments

doesnt this have the first row as the empty values?
no, because we create array with 0 rows and 4 columns: array([], shape=(0, 4), dtype=float64)
10

It sounds like you want to use np.concatenate:

arrays = [myFunction(x) for x in range(1000)]
allArrays = np.concatenate(arrays)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.