'numpy.ndarray'对象没有属性'index'

时间:2018-07-01 21:40:38

标签: python numpy

我试图找到v的索引,但它总是给我:
function table(name, row, key, val, entrylist){ var result; if (table.data == undefined){ table.data = new Map(); }; switch (arguments.length) { case 1: result = table.data.get(name); break; case 2: table.data.set(name, []); result = table.data.get(name); break; case 3: result = table.data.get(name)[row].get(key); break; case 4: table.data.get(name)[row].set(key, val); result = table.data.get(name)[row].get(key); break; case 5: table.data.get(name)[row] = new Map(entrylist); result = table.data.get(name)[row]; break; default: result = table.data; }; return result; } 我试过了:
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

他们都没有回答我的问题

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

1 个答案:

答案 0 :(得分:6)

首先,index是一个列表方法。这里的v是一个numpy数组,您需要执行以下操作:

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)

使用以下索引获取元素:

v[index_of_minimum]
v[index_of_maximum]

使用断言进行验证:

assert(v[index_of_maximum] == v.max())
assert(v[index_of_minimum] == v.min())