0

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 ?

4
  • a = np.empty((0, 2)). Commented May 4, 2017 at 14:04
  • 1
    Trying to call concatenate incrementally is really, really slow. It's better to build up a list of arrays to concatenate and then concatenate them all at once. Commented May 4, 2017 at 17:08
  • @Psidom Thank you. Commented May 4, 2017 at 17:34
  • @user2357112 yes. I realized that myself. check my answer. thanks Commented May 4, 2017 at 17:34

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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]]

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.