Arthur Attout
Arthur Attout

Reputation: 2916

Difference when output is array or square brackets

I'm trying to understand what is the difference (if there's any) between those two outputs.

array([array([203., 164.,  87., ...,   1.,   1.,   0.]),
       array([39., 44., 40., ..., 40., 30., 21.]),
       array([152., 144., 133., ...,  36.,  36.,  36.])], dtype=object)

And

array([[ 0.,  0.,  5., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ..., 10.,  0.,  0.],
       [ 0.,  0.,  0., ..., 16.,  9.,  0.],
       ...,
       [ 0.,  0.,  1., ...,  6.,  0.,  0.],
       [ 0.,  0.,  2., ..., 12.,  0.,  0.],
       [ 0.,  0., 10., ..., 12.,  1.,  0.]])

To me, both these structures are 2D arrays. But for some reason, one of them is printed differently.

I'm trying to feed the first structure to a complex function (svm.SVC.fit). The second one works, but the first one doesn't:

setting an array element with a sequence

although they seem to be the exact same to me ..

Upvotes: 2

Views: 284

Answers (1)

user3483203
user3483203

Reputation: 51165

As I noted in my comment, they are not both 2D arrays. The first is a 1D array of shape (N, ). You are trying to create a numpy array with variable length subarrays. When this happens, numpy coerces the type of the array to object and makes it one dimensional. You should avoid this at all costs, at it removes many of the benefits to using numpy in the first place.

A common approach is padding the subarrays so they are all a uniform length, but whatever you do, you should not use numpy with jagged arrays.

Upvotes: 1

Related Questions