0

I have data that looks like this

listL= array([array([-12939613.07220617,   3962855.50690994]),
       array([-12939614.67349505,   3962828.80807231]),
       array([-12939484.00289515,   3962828.1637531 ]),
       array([-12939484.98046737,   3962854.91251931]),

print(type(listL)) -> <class 'numpy.ndarray'>
print(type(listL[0])) -> <class 'numpy.ndarray'>
print(type(listL[0][0])) -> <class 'numpy.float64'>

I have a second piece of data that looks like this:

A = array([[ 73.87682896,   8.55827956],
       [ 57.43741519,  10.40224912],
       [ 87.88970753,  75.42971056],

print(type(A)) -> <class 'numpy.ndarray'>
print(type(A[0])) -> <class 'numpy.ndarray'>
print(type(A[0][0])) -> <class 'numpy.float64'>

The types are the same across both sets of data, but I have a function that works with 'A', but not with 'listL' and I cannot figure out why.

A[spatial.KDTree(A).query(coordinate)[1]]

works, but

listL[spatial.KDTree(listL).query(coordinate)[1]]

returns the error:

not enough values to unpack (expected 2, got 1)
1
  • The type doesn't help much. Look at shape and dtype instead. listL Iwhich is missing a ])) is a 1d object dtype array (shape (4,). A is a 2d array of numbers, shape (3,2). np.stack(listL) should produce a (4,2) numeric array. Commented Jun 8, 2018 at 4:20

1 Answer 1

1

ListL is in some respects a pathological structure, it is an array of arrays where a 2D array is expected.

numpy/scipy goes to some lengths to graciously accept most things that can be interpreted as 2D array, for example if you repace KDTRee(ListL) with KDTree(list(ListL)) it works.

Why does it work? Because list(listL) behaves like most "essentially 2D" structures (like lists of lists etc.). We can send it through array or asarray or asanyarray etc. to obtain a genuine 2D array.

>>> np.array(list(listL))
array([[-12939613.07220617,   3962855.50690994],
       [-12939614.67349505,   3962828.80807231],
       [-12939484.00289515,   3962828.1637531 ],
       [-12939484.98046737,   3962854.91251931]])
>>> np.array(list(listL)).shape
(4, 2)

An array of arrays is one of the few cases left that still trip up the array conversion machinery:

>>> np.array(listL)
array([array([-12939613.07220617,   3962855.50690994]),
       array([-12939614.67349505,   3962828.80807231]),
       array([-12939484.00289515,   3962828.1637531 ]),
       array([-12939484.98046737,   3962854.91251931])], dtype=object)
>>> np.array(listL).shape
(4,)

We can see despite our attempt to convert to an ordinary array, listL keeps reporting its shape as 1D. Which appears to be what triggers the exception you are observing.

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.