0

I have a numpy ndarray train_data of length 200, where every row is another ndarray of length 10304.

However when I print np.shape(train_data), I get (200, 1), and when I print np.shape(train_data[0]) I get (1, ), and when I print np.shape(train_data[0][0]) I get (10304, ).

I am quite confused with this behavior as I supposed the first np.shape(train_data) should return (200, 10304).

Can someone explains to me why this is happening, and how could I get the array to be in shape of (200, 10304)?

3
  • What is the dtype of the train_data? Commented Dec 8, 2020 at 14:05
  • @DavidS train_data.dtype is object, train_data.dtype[0] is object, train_data.dtype[0][0] is uint8. Commented Dec 8, 2020 at 14:31
  • the subarrays must vary in size, so it can't make a 2d array from them. Commented Dec 8, 2020 at 14:47

2 Answers 2

2

This is because the arrays are constructed to be arrays of objects. Basically each element in the array is pointing to another array of size (1, ) which points to another array of size (10304, ). This is not equivalent to a normal ndarray in numpy so the shape is not recognized correctly. You can check this by looking at the dtypes.

To replicate what you see:

import numpy as np

arr = np.empty(200, dtype='object')
for i in range(200):
    temp_arr = np.empty(1, dtype='object')
    temp_arr[0] = np.zeros(10304)
    arr[i] = temp_arr
print(arr.shape)
print(arr[0].shape)
print(arr[0][0].shape)

(200,)
(1,)
(10304,)

To get the (200, 10304) array back you need to "unpack" them:

new_arr = np.array([x[0] for x in arr])

#(200, 10304)
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure why that's happening, try reshaping the array:

B = np.reshape(A, (-1, 2))

2 Comments

This splits it into 2 columns. So from (200, 1) to (100, 2). Not exactly what I want.
Oh, ok I thought that would work, hope you find out soon!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.