Python Numpy Array 获取按行搜索的行索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18927475/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:16:39  来源:igfitidea点击:

Numpy Array Get row index searching by a row

pythonarraysnumpyrandom-forest

提问by user2801023

I am new to numpy and I am implementing clustering with random forest in python. My question is:

我是 numpy 的新手,我正在 python 中使用随机森林实现聚类。我的问题是:

How could I find the index of the exact row in an array? For example

如何在数组中找到确切行的索引?例如

[[ 0.  5.  2.]
 [ 0.  0.  3.]
 [ 0.  0.  0.]]

and I look for [0. 0. 3.]and get as result 1(the index of the second row).

我寻找[0. 0. 3.]并得到结果 1(第二行的索引)。

Any suggestion? Follows the code (not working...)

有什么建议吗?遵循代码(不起作用...)

    for index, element in enumerate(leaf_node.x):
        for index_second_element, element_two in enumerate(leaf_node.x):
            if (index <= index_second_element):
                index_row = np.where(X == element)
                index_column = np.where(X == element_two)
                self.similarity_matrix[index_row][index_column] += 1

采纳答案by Daniel

Why not simply do something like this?

为什么不简单地做这样的事情?

>>> a
array([[ 0.,  5.,  2.],
       [ 0.,  0.,  3.],
       [ 0.,  0.,  0.]])
>>> b
array([ 0.,  0.,  3.])

>>> a==b
array([[ True, False, False],
       [ True,  True,  True],
       [ True,  True, False]], dtype=bool)

>>> np.all(a==b,axis=1)
array([False,  True, False], dtype=bool)

>>> np.where(np.all(a==b,axis=1))
(array([1]),)