python - Numpy - 从距离矩阵中提取唯一值

标签 python arrays numpy scipy

输入数据:

我有以下 distance_matrix :

  [[1.        , 0.14285714, 0.25      , 0.83333333, 0.63636364],
   [0.14285714, 1.        , 0.33333333, 0.84615385, 0.66666667],
   [0.25      , 0.33333333, 1.        , 0.76923077, 0.58333333],
   [0.83333333, 0.84615385, 0.76923077, 1.        , 0.69230769],
   [0.63636364, 0.66666667, 0.58333333, 0.69230769, 1.        ]]

当前结果:

np.where(distane_matrix <= 0.25)返回以下输出:

(array([0, 0, 1, 2]), array([1, 2, 0, 0]))

期望的结果:

(array([0, 0]), array([1, 2]))

说明:

用语言表达,因为我知道:

  1. [0,1][1,0] 具有相同的值
  2. [0,2][2,0] 具有相同的值
  3. [0,1][0,2]满足np.where()的要求

想要[1,0][2,0]在输出中返回,因为它是冗余信息。执行此操作的最佳方法是什么?

最佳答案

假设d是一个指定的距离矩阵。

演示:

In [28]: r = np.triu(d, 1)

In [29]: r
Out[29]:
array([[0.        , 0.14285714, 0.25      , 0.83333333, 0.63636364],
       [0.        , 0.        , 0.33333333, 0.84615385, 0.66666667],
       [0.        , 0.        , 0.        , 0.76923077, 0.58333333],
       [0.        , 0.        , 0.        , 0.        , 0.69230769],
       [0.        , 0.        , 0.        , 0.        , 0.        ]])

In [30]: np.where((r>0) & (r<=0.25))
Out[30]: (array([0, 0], dtype=int64), array([1, 2], dtype=int64))

关于python - Numpy - 从距离矩阵中提取唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50825505/

相关文章:

python - 如何使用列上的尾随行对同一列进行计算 | Pandas python

python - 通过索引列表使用不同数组中的值,使用 += 更改 numpy 数组中的特定值

python - 使用 numpy 和 pandas 本地化随机点

python - 在 model_dir 中找不到经过训练的模型

python - 设置一个字典中包含另一字典路径的值

Python:对象没有属性错误:一个数组

php - mysql 选择数组中的位置 (php, mysql)

python - 如何从 Dragon NaturallySpeaking 中的高级脚本语音命令调用 python 函数?

python - Python 中的 traverse_ 等效项?

C++ |如何在函数结果中返回所有 PID?