Python NumPy 数组中元素的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18079029/
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
Index of element in NumPy array
提问by Marc Ortiz
In Python we can get the index of a value in an array by using .index(). How can I do it with a NumPy array?
在 Python 中,我们可以使用 .index() 获取数组中某个值的索引。我怎样才能用 NumPy 数组做到这一点?
When I try to do
当我尝试做
decoding.index(i)
it says that the NumPy library doesn't support this function. Is there a way to do it?
它说 NumPy 库不支持此功能。有没有办法做到这一点?
采纳答案by Saullo G. P. Castro
Use np.where
to get the indices where a given condition is True
.
使用np.where
来获得,其中一个给定的条件是指数True
。
Examples:
例子:
For a 2D np.ndarray
called a
:
对于 2Dnp.ndarray
称为a
:
i, j = np.where(a == value) # when comparing arrays of integers
i, j = np.where(np.isclose(a, value)) # when comparing floating-point arrays
For a 1D array:
对于一维数组:
i, = np.where(a == value) # integers
i, = np.where(np.isclose(a, value)) # floating-point
Note that this also works for conditions like >=
, <=
, !=
and so forth...
请注意,这也适用于像条件>=
,<=
,!=
等等...
You can also create a subclass of np.ndarray
with an index()
method:
您还可以np.ndarray
使用index()
方法创建子类:
class myarray(np.ndarray):
def __new__(cls, *args, **kwargs):
return np.array(*args, **kwargs).view(myarray)
def index(self, value):
return np.where(self == value)
Testing:
测试:
a = myarray([1,2,3,4,4,4,5,6,4,4,4])
a.index(4)
#(array([ 3, 4, 5, 8, 9, 10]),)
回答by jlansey
I'm torn between these two ways of implementing an index of a NumPy array:
我在这两种实现 NumPy 数组索引的方法之间左右为难:
idx = list(classes).index(var)
idx = np.where(classes == var)
Both take the same number of characters, but the first method returns an int
instead of a numpy.ndarray
.
两者都采用相同数量的字符,但第一个方法返回 anint
而不是 a numpy.ndarray
。
回答by Statham
You can convert a numpy array to list and get its index .
您可以将 numpy 数组转换为 list 并获取其 index 。
for example:
例如:
tmp = [1,2,3,4,5] #python list
a = numpy.array(tmp) #numpy array
i = list(a).index(2) # i will return index of 2, which is 1
this is just what you wanted.
这正是你想要的。
回答by N.Moudgil
You can use the function numpy.nonzero()
, or the nonzero()
method of an array
您可以使用函数numpy.nonzero()
,或nonzero()
数组的方法
import numpy as np
A = np.array([[2,4],
[6,2]])
index= np.nonzero(A>1)
OR
(A>1).nonzero()
Output:
输出:
(array([0, 1]), array([1, 0]))
First array in output depicts the row indexand second array depicts the corresponding column index.
输出中的第一个数组描述行索引,第二个数组描述相应的列索引。
回答by Eelco Hoogendoorn
This problem can be solved efficiently using the numpy_indexedlibrary (disclaimer: I am its author); which was created to address problems of this type. npi.indices can be viewed as an n-dimensional generalisation of list.index. It will act on nd-arrays (along a specified axis); and also will look up multiple entries in a vectorized manner as opposed to a single item at a time.
使用numpy_indexed库可以有效地解决这个问题(免责声明:我是它的作者);这是为了解决此类问题而创建的。npi.indices 可以看作是 list.index 的 n 维泛化。它将作用于 nd-arrays(沿着指定的轴);并且还将以矢量化的方式查找多个条目,而不是一次查找单个项目。
a = np.random.rand(50, 60, 70)
i = np.random.randint(0, len(a), 40)
b = a[i]
import numpy_indexed as npi
assert all(i == npi.indices(a, b))
This solution has better time complexity (n log n at worst) than any of the previously posted answers, and is fully vectorized.
该解决方案比之前发布的任何答案具有更好的时间复杂度(最坏情况下为 n log n),并且是完全矢量化的。
回答by Victor Zuanazzi
If you are interested in the indexes, the best choice is np.argsort(a)
如果您对索引感兴趣,最好的选择是 np.argsort(a)
a = np.random.randint(0, 100, 10)
sorted_idx = np.argsort(a)