Numpy Array 获取按行搜索的行索引 [英] Numpy Array Get row index searching by a row
本文介绍了Numpy Array 获取按行搜索的行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 numpy 的新手,我正在 Python 中使用随机森林实现聚类.我的问题是:
如何在数组中找到确切行的索引?例如
[[ 0. 5. 2.][ 0. 0. 3. ][0.0.0.]]
然后我寻找 [0.0. 3.]
并得到结果 1(第二行的索引).
有什么建议吗?遵循代码(不起作用...)
用于索引,枚举中的元素(leaf_node.x):对于 index_second_element, element_two in enumerate(leaf_node.x):如果(索引 <= index_second_element):index_row = np.where(X == 元素)index_column = np.where(X == element_two)self.similarity_matrix[index_row][index_column] += 1
解决方案
为什么不简单地做这样的事情?
<预><代码>>>>一种数组([[ 0., 5., 2.],[ 0., 0., 3.],[ 0., 0., 0.]])>>>乙数组([ 0., 0., 3.])>>>a==b数组([[真,假,假],[真实,真实,真实],[真,真,假]],dtype=bool)>>>np.all(a==b,axis=1)数组([假,真,假],dtype=bool)>>>np.where(np.all(a==b,axis=1))(数组([1]),)I am new to numpy and I am implementing clustering with random forest in python. My question is:
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).
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
解决方案
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]),)
这篇关于Numpy Array 获取按行搜索的行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文