‘numpy.ndarray’ object has no attribute ‘index’

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

Leave a Comment