stefos
stefos

Reputation: 23

create numpy array with varying shape

I want to create a numpy array in order to fill it with numpy arrays. for example:

a = [] (simple array or numpy array) 
b = np.array([[5,3],[7,9],[3,8],[2,1]])
a = np.concatenate([a,b])
c = np.array([[1,2],[2,9],[3,0]])
a = np.concatenate([a,c])

I would like to do so because I have wav files from which I extract some features so I can't read from 2 files concurrently but iteratively. How can I create an empty ndarray with the second dimension fixed e.g. a.shape = (x,2) or how can I concatenate the arrays even without the creation of a "storage" array ?

Upvotes: 0

Views: 804

Answers (2)

stefos
stefos

Reputation: 23

Actually there are 2 options. The first one is: a = np.empty((0, 2)) , which creates an empty np array with the first dimension varying. The second is to create an empty array a = [] , append the np arrays in the array and then use np.vstack to concatenate them all together in the end. The latter the most efficient option.

Upvotes: 1

Nuageux
Nuageux

Reputation: 1686

You have to had brackets in concatenate function:

b = np.array([[5,3],[7,9],[3,8],[2,1]])
c = np.array([[1,2],[2,9],[3,0]])
a = np.concatenate([b,c])

Output:

[[5 3]
 [7 9]
 [3 8]
 [2 1]
 [1 2]
 [2 9]
 [3 0]]

Upvotes: 0

Related Questions