Python 'numpy.ndarray' 对象没有属性 'index'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51127209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'numpy.ndarray' object has no attribute 'index'
提问by Vehicom0607
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
我试图找到 v 的索引,但它总是给我:'numpy.ndarray' object has no attribute 'index'
我试过:类型错误
:切片索引必须是整数或无或有一个 __index__ 方法。如何解决?如何在数组中查找数组的索引。
在给定包含它的列表的 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.
编辑:哦,废话,我把 ma 而不是最大的我的坏。那时我才开始编程。
回答by seralouk
First of all, index
is a list method. Here v
is a numpy array and you need to do the following:
首先,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)
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())
回答by ColinMac
If you are using Numpy:
如果您使用的是 Numpy:
values = np.array([3,6,1,5])
index_min = np.argmin(values)
print(index_min)
returns the index of 2.
返回 2 的索引。