close

[Solved] ‘numpy.ndarray’ object has no attribute ‘index’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ‘numpy.ndarray’ object has no attribute ‘index’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ‘numpy.ndarray’ object has no attribute ‘index’ Error Occurs?

Today I get the following error ‘numpy.ndarray’ object has no attribute ‘index’ in python.

How To Solve ‘numpy.ndarray’ object has no attribute ‘index’ Error ?

  1. How To Solve 'numpy.ndarray' object has no attribute 'index' Error ?

    To Solve 'numpy.ndarray' object has no attribute 'index' Error 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.

  2. 'numpy.ndarray' object has no attribute 'index'

    To Solve 'numpy.ndarray' object has no attribute 'index' Error 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.

Solution 1

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())

Solution 2

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read