1

I'm very new to python and I have been faced with the task of taking several arrays into another array, this is inside of a loop. So if you had

a = np.array([2,3,4,3,4,4,5,3,2,3,4])

and

b = np.array([1,1,1,1,1,2,23,2,3,3,3])

and

c = np.array([])

and wanted the result

c = [[2,3,4,3,4,4,5,3,2,3,4],
     [1,1,1,1,1,2,23,2,3,3,3]]

so if I did c[0,:] I would get [2,3,4,3,4,4,5,3,2,3,4] I tried using c = [c, np.array(a)] then next iteration you get c = [c, np.array(b)] but I i do c[0,:] i get the error message list indices must be integers not tuples

EDIT:

When I get it to print out c it gives [array([2,3,4,3,4,4,5,3,2,3,4],dtype = unit8)]

Do you have any ideas?

1
  • 2
    I would highly recommend not doing this in a loop, especially for large arrays! Every time you concatenate two numpy arrays together, the result is a new copy of the array. Generating the copy becomes slower and slower as the array gets larger and larger on each loop iteration. If you must do your appending inside a loop then I suggest you use a regular Python list, and convert it to a numpy array outside the loop (unlike numpy arrays, you can append to Python lists in place without generating a copy). Commented Nov 30, 2015 at 18:08

4 Answers 4

5
In [10]: np.vstack((a,b))
Out[10]:
array([[ 2,  3,  4,  3,  4,  4,  5,  3,  2,  3,  4],
       [ 1,  1,  1,  1,  1,  2, 23,  2,  3,  3,  3]])

EDIT: Here's an example of using it in a loop to gradually build a matrix:

In [14]: c = np.random.randint(0, 10, 10)

In [15]: c
Out[15]: array([9, 5, 9, 7, 3, 0, 1, 9, 2, 0])

In [16]: for _ in xrange(10):
   ....:     c = np.vstack((c, np.random.randint(0, 10, 10)))
   ....:

In [17]: c
Out[17]:
array([[9, 5, 9, 7, 3, 0, 1, 9, 2, 0],
       [0, 8, 1, 9, 7, 5, 4, 2, 1, 2],
       [2, 1, 4, 2, 9, 6, 7, 1, 3, 2],
       [6, 0, 7, 9, 1, 9, 8, 5, 9, 8],
       [8, 1, 0, 9, 6, 6, 6, 4, 8, 5],
       [0, 0, 5, 0, 6, 9, 9, 4, 6, 9],
       [4, 0, 9, 8, 6, 0, 2, 2, 7, 0],
       [1, 3, 4, 8, 2, 2, 8, 7, 7, 7],
       [0, 0, 4, 8, 3, 6, 5, 6, 5, 7],
       [7, 1, 3, 8, 6, 0, 0, 3, 9, 0],
       [8, 5, 7, 4, 7, 2, 4, 8, 6, 7]])
Sign up to request clarification or add additional context in comments.

6 Comments

Hi when I do this getting array([[2],[1]]) instead of all of the elements
I'd recommend checking the code around it. np.vstack just vertically stacks the arrays you pass to it, and so something else in your code may be cutting off the rest of the results inadvertently.
what i'm trying to do add them to a 3rd array c
Added an edit to show an example of doing it in a loop. Hope that helps.
Thanks this is brilliant, a lot of help, and easy to follow
|
3

Most numpythonic way is using np.array:

>>> c = np.array((a,b))
>>> 
>>> c
array([[ 2,  3,  4,  3,  4,  4,  5,  3,  2,  3,  4],
       [ 1,  1,  1,  1,  1,  2, 23,  2,  3,  3,  3]])

Comments

0

You may try this:

>>> c = [list(a), list(b)]
>>> c
[[2, 3, 4, 3, 4, 4, 5, 3, 2, 3, 4], [1, 1, 1, 1, 1, 2, 23, 2, 3, 3, 3]]

Comments

0

You can concatenate arrays in numpy. For this to work, they must have the same size in all dimensions except the concatenation direction.

If you just say

>>> c = np.concatenate([a,b])

you will get

>>> c
array([ 2,  3,  4,  3,  4,  4,  5,  3,  2,  3,  4,  1,  1,  1,  1,  1,  2,
   23,  2,  3,  3,  3])

So in order to achieve what you want you first have to add another dimension to your vectors a and b like so

>>> a[None,:] 
array([[2, 3, 4, 3, 4, 4, 5, 3, 2, 3, 4]])

or equivalently

>>> a[np.newaxis,:]
array([[2, 3, 4, 3, 4, 4, 5, 3, 2, 3, 4]])

So you could do the following:

>>> c = np.concatenate([a[None,:],b[None,:]],axis = 0)
>>> c
array([[ 2,  3,  4,  3,  4,  4,  5,  3,  2,  3,  4],
   [ 1,  1,  1,  1,  1,  2, 23,  2,  3,  3,  3]])

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.