简体   繁体   English

Python 中一维矩阵的成对计算

[英]Pairwise Calculations on 1D Matrix in Python

My question is basically the same as this post here ...and maybe it's exactly the answer I just don't understand the syntax of the np.arrange() function.我的问题与这里的这篇文章基本相同......也许这正是我不明白 np.arrange() function 的语法的答案。

Here is what I am trying to do:这是我正在尝试做的事情:

For each unique pair of values in a 1D array I want to calculate the ratio of max value to min value.对于一维数组中的每一对唯一值,我想计算最大值与最小值的比率。 All the examples I can find, including the above mentioned, would indicate the np.arrange() function only works with sequential values ie [1,2,3,4,5] and not from a unique array such as [1.34, 87.5, 2.10, 700.4]我能找到的所有示例,包括上面提到的,都表明 np.arrange() function 仅适用于顺序值,即 [1,2,3,4,5] 而不是来自唯一数组,例如 [1.34, 87.5 , 2.10, 700.4]

max(0,1)/min(0,1) = (87.5/1.34)最大值(0,1)/最小值(0,1)=(87.5/1.34)

max(0,2)/min(0,2) = (2.10/1.34)最大值(0,2)/最小值(0,2)=(2.10/1.34)

Is the np.arrange() what I should be using or is there another function better suited to solving the problem?我应该使用 np.arrange() 还是有另一个 function 更适合解决问题?

np.arange() has not been used to solve the problem, rather numpy broadcasting has been used to provide a solution in the link shared by you. np.arange()没有用于解决问题,而是使用 numpy 广播在您分享的链接中提供了解决方案。 np.arange() is used to create the initial array to work on (see doc ). np.arange()用于创建要处理的初始数组(请参阅doc )。

A simple solution to your problem would be using 2 loops like:解决您的问题的一个简单方法是使用 2 个循环,例如:

x = [1.34, 87.5, 2.10, 700.4]
mat = np.zeros(shape=(len(x),len(x)))
for i, ival in enumerate(x):
  for j, jval in enumerate(x):
    mat[i][j]=max(ival,jval)/min(ival,jval)

This yields the pairwise ratio of max to min:这产生 max 与 min 的成对比率:

 Result:
        [[  1.     65.299   1.567 522.687]
         [ 65.299   1.     41.667   8.005]
         [  1.567  41.667   1.    333.524]
         [522.687   8.005 333.524   1.   ]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM