我是 numpy 的新手,我正在 python 中使用随机森林实现集群。我的问题是:
如何找到数组中确切行的索引?例如
[[ 0. 5. 2.]
[ 0. 0. 3.]
[ 0. 0. 0.]]
然后我寻找 [0. 0. 3.]
并得到结果 1(第二行的索引)。
有什么建议吗?遵循代码(不工作......)
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
最佳答案
为什么不简单地做这样的事情呢?
>>> 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]),)
关于python - Numpy Array 获取按行搜索的行索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18927475/