Condensed matrix function to find pairs(用于查找对的压缩矩阵函数)
问题描述
对于一组观察:
[a1,a2,a3,a4,a5]
它们的成对距离
d=[[0,a12,a13,a14,a15]
[a21,0,a23,a24,a25]
[a31,a32,0,a34,a35]
[a41,a42,a43,0,a45]
[a51,a52,a53,a54,0]]
以压缩矩阵形式给出(上面的上三角,从 scipy.spatial.distance.pdist 计算):
Are given in a condensed matrix form (upper triangular of the above, calculated from scipy.spatial.distance.pdist ):
c=[a12,a13,a14,a15,a23,a24,a25,a34,a35,a45]
问题是,鉴于我在压缩矩阵中有索引,是否有一个函数(最好在 python 中)f 来快速给出使用哪两个观测值来计算它们?
The question is, given that I have the index in the condensed matrix is there a function (in python preferably) f to quickly give which two observations were used to calculate them?
f(c,0)=(1,2)
f(c,5)=(2,4)
f(c,9)=(4,5)
...
我尝试了一些解决方案,但没有一个值得一提:(
I have tried some solutions but none worth mentioning :(
推荐答案
您可能会发现 triu_indices 很有用.喜欢,
You may find triu_indices useful. Like,
In []: ti= triu_indices(5, 1)
In []: r, c= ti[0][5], ti[1][5]
In []: r, c
Out[]: (1, 3)
注意索引从0开始.你可以随意调整它,例如:
Just notice that indices starts from 0. You may adjust it as you like, for example:
In []: def f(n, c):
..: n= ceil(sqrt(2* n))
..: ti= triu_indices(n, 1)
..: return ti[0][c]+ 1, ti[1][c]+ 1
..:
In []: f(len(c), 5)
Out[]: (2, 4)
这篇关于用于查找对的压缩矩阵函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用于查找对的压缩矩阵函数
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
