python - Numpy Array 获取按行搜索的行索引

标签 python arrays numpy random-forest

我是 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/

相关文章:

python - 使用 if-return-return 还是 if-else-return 效率更高?

python - 检查 Pandas 数据框的异常值

python - 如何在不同数组的 For In 中访问数组的特定单元格?

arrays - 算法分析 - 在排序数组中查找丢失的整数优于 O(n)

python - 将 Bitstring(1 和 0 的字符串)转换为 numpy 数组

python - 如何在 matlab 和 python 中重现相同的随机整数数组?

python - 在pytorch中使用前馈网络构建循环神经网络

c - 打印结构数组

python - 自相关以numpy估计周期性

javascript - 如何将这段 Python 代码翻译成 JavaScript? (涉及集合)