Manipal King
Manipal King

Reputation: 420

how to transfer an numpy array into another array

I have a function that returns a numpy array every second , that i want to store in another array for reference. for eg (array_a is returned)

array_a = [[    25.       50.       25.       25.       50.  ]
           [     1.        1.        1.        1.        1.  ]]

array_collect = np.append(array_a,array_collect)

But when i Print array_collect , i get an added array, not a bigger array with arrays inside it.

 array_collect = [    25.       50.       25.       25.       50.  
                       1.        1.        1.        1.        1.  
                       25.       50.       25.       25.       50.  
                       1.        1.        1.        1.        1.  
                       25.       50.       25.       25.       50.  ] 

what i want is

 array_collect = [   [[  25.     50.       25.       25.       50.  ]
                      [1.        1.        1.        1.        1.   ]] 
                     [[  25.     50.       25.       25.       50.  ]
                      [1.        1.        1.        1.        1.   ]]
                     [[  25.     50.       25.       25.       50.  ]
                      [1.        1.        1.        1.        1.   ]]  ]

How do i get it ??

Upvotes: 0

Views: 796

Answers (3)

Lee
Lee

Reputation: 31100

You could use vstack:

array_collect = np.array([[25.,50.,25.,25.,50.],[1.,1.,1.,1.,1.]])
array_a = np.array([[2.,5.,2.,2.,5.],[1.,1.,1.,1.,1.]])


array_collect=np.vstack((array_collect,array_a))

However, if you know the total number of minutes in advance, it would be better to define your array first (e.g. using zeros) and gradually fill it - this way, it is easier to stay within memory limits.

no_minutes = 5 #say 5 minutes
array_collect = np.zeros((no_minutes,array_a.shape[0],array_a.shape[1]))

Then, for every minute, m

array_collect[m] = array_a

Upvotes: 1

farhawa
farhawa

Reputation: 10417

Just use np.concatenate() and reshape this way:

import numpy as np


array_collect = np.array([[25.,50.,25.,25.,50.],[1.,1.,1.,1.,1.]])
array_a = np.array([[2.,5.,2.,2.,5.],[1.,1.,1.,1.,1.]])

array_collect = np.concatenate((array_collect,array_a),axis=0).reshape(2,2,5)

>>

[[[ 25.  50.  25.  25.  50.]
  [  1.   1.   1.   1.   1.]]

 [[  2.   5.   2.   2.   5.]
  [  1.   1.   1.   1.   1.]]]

Upvotes: 1

Manipal King
Manipal King

Reputation: 420

I found it , this can be done by using :

 np.reshape()

the new array formed can be reshaped using

y= np.reshape(y,(a,b,c))

where a is the no. of arrays stores and (b,c) is the shape of the original array

Upvotes: 0

Related Questions