2

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

9
  • @chepner if I write type(firstArray[0]) or type(secondArray[0]) they both yield numpy.ndarray Commented Dec 6, 2018 at 21:35
  • Mmmh .. type(firstArray[0][0]) and type(secondArray[0][0]) are both numpy.float64 Commented Dec 6, 2018 at 21:37
  • " the first one doesn't, ": what happens? error? wrong results? Commented Dec 6, 2018 at 21:38
  • It complains that I'm setting an array element with a sequence.. But before trying to understand this message, I'd like to understand if both these outputs are different at all. If they are, I'll be able to fix the second one to correspond to the first, and the error should go away Commented Dec 6, 2018 at 21:39
  • It looks like whatever produced the first array returned the array of array objects whereas the second one returned the array of array values. the error in your comment is suggesting that it is trying to read in array([...]) as opposed to [...]. Commented Dec 6, 2018 at 21:45

1 Answer 1

1

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.

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

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.