21

I'm trying to find the index of v but it always gives me:
'numpy.ndarray' object has no attribute 'index' I've tried:
TypeError: slice indices must be integers or None or have an __index__ method. How to resolve it? How to find the index of an array within an array.
Finding the index of an item given a list containing it in Python

none of them have answered my question

v = np.random.randn(10)
print(v)
maximum = np.max(v)
minimum = np.min(v)
print(maximum, minimum)
v.index(maximum, minimum)

edit: Oh, crap i put ma instead of maximum my bad. I just started programing then.

6
  • 2
    what is ma and mi ? Commented Jul 1, 2018 at 21:42
  • 4
    what do you mean by saying index ?? Commented Jul 1, 2018 at 21:42
  • also it look like youre looking for np.where() Commented Jul 1, 2018 at 21:42
  • @Likedapro see my answer Commented Jul 1, 2018 at 21:45
  • index is a list method. v is a numpy array. There's a difference. Commented Jul 1, 2018 at 22:01

2 Answers 2

25

First of all, index is a list method. Here v is a numpy array and you need to do the following:

v = np.random.randn(10)
print(v)
maximum = np.max(v)
minimum = np.min(v)
print(maximum, minimum)

index_of_maximum = np.where(v == maximum)
index_of_minimum = np.where(v == minimum)

Get the elements using these indices:

v[index_of_minimum]
v[index_of_maximum]

Verify using assert:

assert(v[index_of_maximum] == v.max())
assert(v[index_of_minimum] == v.min())
Sign up to request clarification or add additional context in comments.

Comments

17

If you are using Numpy:

values = np.array([3,6,1,5])
index_min = np.argmin(values)
print(index_min)

returns the index of 2.

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.