0

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

2 Answers 2

2

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.

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

1 Comment

Thank you very much for the explanation!
0

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)

1 Comment

Thank you very much for the explanation & the code!

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.