当标准化时,我如何得到精确的平均值0?

2024-03-29 12:18:30 发布

您现在位置:Python中文网/ 问答频道 /正文

array = preprocessing.scale(array)

我使用python中sklearn的预处理模块,将数组的平均值改为0。问题是,我的平均值是10^16,而不是0。你知道吗

问题是,我真的需要一个0的平均值,否则我无法计算我的信号协方差矩阵。你知道吗

我怎样才能解决这个问题?你知道吗

提前多谢了。你知道吗


Tags: 模块信号矩阵数组sklearnarray平均值scale
1条回答
网友
1楼 · 发布于 2024-03-29 12:18:30

下面是一种使用math.fsum使平均零到机器精度的方法。你知道吗

>>> import numpy as np
>>> import math
>>> 
>>> a = np.random.random((1000,))
>>> 
>>> a -= math.fsum(a)/a.size
# while fsum gives the exact solution rounded to machine precision
# there will be a residual error
# this must be borne by the smallest term
>>> a[np.argmin(np.abs(a))] -= math.fsum(a)
>>> 
# now we are zero to machine precision
>>> math.fsum(a)
0.0

坏消息是:numpy没有意识到。。。。你知道吗

>>> np.mean(a)
-3.552713678800501e-18

相关问题 更多 >