快速实现Python代码以计算乘积的均值

huangapple go评论65阅读模式
英文:

Fast implementations in Python to compute the mean of products

问题

我有一个浮点数元素列表 x=[0.1, 2, 0.5, ...],长度为 l=len(x)。我正在寻找快速/矢量化的实现来计算 x 中所有两对元素的乘积的平均值:

S=0.0
for x1 in x:
    for x2 in x:
        S+=x1*x2/(l*l)

这本质上是 x 的样本协方差。我查看了 numpycov() 函数,但该函数计算协方差矩阵,或者在列表的情况下只是样本方差。而 correlate() 函数使用滑动窗口的样本,而非列表中所有元素对的组合。

编辑: 尽管原始问题中没有说明,但不涉及 l^2 内存的方法将受到极大赞赏。以下提出的 np.outer 和 numpy 广播都需要 l^2 内存,这使它们对于大型 l 不可行。

英文:

I have a list of float elements x=[0.1, 2, 0.5, ...] with length l=len(x). I am looking for fast/vectorized implementations to compute mean of the products between all two pairs from x:

S=0.0
for x1 in x:
    for x2 in x:
        S+=x1*x2/(l*l)

This would essentially be the sample covariance of x. I looked into numpy's cov() function, however, that computes a covariance matrix, or, in the case of a list, simply the sample variance. Also, the correlate() function uses samples from a sliding window, not the combination of all element pairs from the list.

Edit: Although not stated in the original question, methods not involving l^2 memory would be greatly appreciated. np.outer and numpy broadcasting proposed below both have l^2 memory requirements which make them unfeasible for large l.

答案1

得分: 4

你可以使用简单的numpy广播操作来执行你所询问的计算:

import numpy as np

x = np.asarray([0.1, 2, 0.5])
l = len(x)

S = np.sum(x[:, None] * x[None, :]) / l**2
英文:

You can do the calculation you're asking about with simple numpy broadcasting operations:

import numpy as np

x = np.asarray([0.1, 2, 0.5])
l = len(x)

S = np.sum(x[:, None] * x[None, :]) / l**2

答案2

得分: 2

表达式 np.multiply.outer(x, x).mean() 就可以做到。

举个例子,这是你的计算:

In [17]: x = [0.1, 2, 0.5, 3, 0.25, 1]

In [18]: l = len(x)

In [19]: S=0.0
    ...: for x1 in x:
    ...:     for x2 in x:
    ...:         S+=x1*x2/(l*l)
    ...: 

In [20]: S
Out[20]: 1.3034027777777775

这是 np.multiply.outer(x, x).mean()

In [21]: np.multiply.outer(x, x).mean()
Out[21]: 1.3034027777777777

@Blckknght的答案也同样有效。

英文:

The expression np.multiply.outer(x, x).mean() will do it.

For example, here's your calculation:

In [17]: x = [0.1, 2, 0.5, 3, 0.25, 1]

In [18]: l = len(x)

In [19]: S=0.0
    ...: for x1 in x:
    ...:     for x2 in x:
    ...:         S+=x1*x2/(l*l)
    ...: 

In [20]: S
Out[20]: 1.3034027777777775

And here's np.multiply.outer(x, x).mean():

In [21]: np.multiply.outer(x, x).mean()
Out[21]: 1.3034027777777777

@Blckknght's answer also works just as well.

huangapple
  • 本文由 发表于 2023年3月21日 00:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定