numpy的阵列获取行索引由行搜索 [英] Numpy Array Get row index searching by a row
本文介绍了numpy的阵列获取行索引由行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来numpy的,我随机森林使用Python实现集群。我的问题是:
我怎么能找到确切的行的数组索引?例如:
[0。5。2]
[0 0 3]
[0 0 0]
和我期待 [0。 0.3。]
并获得尽可能结果1(第二行的索引)。
任何建议?遵循code(不工作...)
对于指数,在历数(leaf_node.x)元素:
对于index_second_element,element_two在历数(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。]])
>>> b
阵列([0,0,3])>>>一个== b
阵列([真,假,假]
[真,真,真]
[真,真,假],DTYPE =布尔)>>> np.all(A == B,轴= 1)
阵列([假,真,假],DTYPE =布尔)>>> np.where(np.all(A == B,轴= 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的阵列获取行索引由行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文