nsalem
nsalem

Reputation: 19

TypeError when using numpy.take

I’m trying to use numpy.take() to get, like the documentation says, elements from an array along an axis.

My code:

print np.take(testArray,axis=1)

Gives the following counter-intuitive error:

TypeError: take() takes at least 2 arguments (2 given)

Well, if two are given, what is wrong then?

In order to debug, I printed the shapes:

print testArray[:, 1].shape
print testArray[:, 1]       

(1L, 4L)
[[      251       100         4 886271884]]

Upvotes: 0

Views: 71

Answers (2)

Aiman Al-Eryani
Aiman Al-Eryani

Reputation: 709

As Sven has mentioned, you're only supplying 1 of the required arguments. The axis argument is only optional. The 2nd argument you need is a python list of indices of elements you want to extract:

print(np.take(testArray, list(range(testArray.shape[1])), axis=1))

And if you're using python < 3.0, where range() still returned a list:

print np.take(testArray, range(testArray.shape[1]), axis=1)

Upvotes: 0

Sven Marnach
Sven Marnach

Reputation: 601739

This is a horrible error message, and fortunately it's fixed in Python 3.3 and up.

What it actually means is that the function takes at least two postional arguments, but you have given one positional argument and one keyword argument. You also need to provide an array of indices as second argument to specify which elements to take; see the documentation of numpy.take() for further details.

Upvotes: 2

Related Questions