Nwpulver
Nwpulver

Reputation: 45

numpy array creating array of lists

I am trying to follow along on a video tutorial of numpy and the array is created in the video as follows

a = np.array([[1,2,3,4,5,6,7],[9,10,11,12,13,14]])

when I do this, I get the output

[list([1, 2, 3, 4, 5, 6, 7]) list([9, 10, 11, 12, 13, 14])]

in the video, the output does not look like this and I cannot find a difference in our code. The output in the video looks like a normal 2D numpy array with 7 columns and 2 rows.

also, when I do a.shape() I get a (2,) array which is not a 2 by 7 array, which is what he has in the video. I have tried np.asarray() and different combinations of brackets and parenthesis, I am just confused because my code is exactly the same as the video. The video is less than a year old so I assume nothing has changed with the package? Any help is appreciated, I am hoping to turn this into a learning experience on how to solve problems like this by myself in the future. Thank you.

Upvotes: 1

Views: 64

Answers (2)

Hiho
Hiho

Reputation: 663

Are you the code in the video was not

a = np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])

which works as expected. Notice the 8 at the beginning of the second list, this was missing in your code, which meant that the two lists were different sizes, hence numpy fails to make a float array out of it.

Upvotes: 0

ted
ted

Reputation: 14704

Your second list only has 6 elements so numpy cannot parse it as a 2D array; instead it parses it as a 1D array of object type, and these objects are lists

a = np.array([[1, 2, 3, 4, 5, 6, 7],[9, 10, 11, 12, 13, 14, 15]])

works fine

Upvotes: 1

Related Questions