Numpy std(标准差)函数奇怪的行为 [英] Numpy std (standard deviation) function weird behavior

查看:43
本文介绍了Numpy std(标准差)函数奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从统计学的角度来看,当所有值都相等时,标准差应该是 0.对于 arr1 结果如预期:0,但对于 arr21.3877787807814457e-17 - 非常小但不是0,这会导致例如出现问题zscore.

From statistics point of view standard deviation when all values are equal should be 0. For arr1 result is as expected: 0, but for arr2 is 1.3877787807814457e-17 - very small but not 0, which leads to issues with e.g. zscore.

这是正确的行为还是奇怪的错误?

Is this a proper behavior or weird bug?

import numpy as np

arr1 = [20.0] * 3
#[20.0, 20.0, 20.0]

arr2 = [-0.087] * 3
#[-0.087, -0.087, -0.087]

np.std(arr1) #0.0
np.std(arr2) #1.3877787807814457e-17

推荐答案

std 的 Numpy 文档说明:

The Numpy documentation for std states:

标准差是平均值的平方根均值的平方偏差,即 std = sqrt(mean(abs(x - x.mean())**2)).

The standard deviation is the square root of the average of the squared deviations from the mean, i.e., std = sqrt(mean(abs(x - x.mean())**2)).

平均平方偏差通常计算为x.sum()/N,其中 N = len(x).但是,如果指定了 ddof,则使用除数 N - ddof.在标准统计实践中,ddof=1提供无限方差的无偏估计量人口.ddof=0 提供最大似然估计正态分布变量的方差.标准差在此函数中计算的是估计值的平方根方差,因此即使使用 ddof=1,也不会是无偏估计标准差本身.

The average squared deviation is normally calculated as x.sum() / N, where N = len(x). If, however, ddof is specified, the divisor N - ddof is used instead. In standard statistical practice, ddof=1 provides an unbiased estimator of the variance of the infinite population. ddof=0 provides a maximum likelihood estimate of the variance for normally distributed variables. The standard deviation computed in this function is the square root of the estimated variance, so even with ddof=1, it will not be an unbiased estimate of the standard deviation per se.

注意,对于复数,std取绝对值之前平方,所以结果总是实数和非负数.

Note that, for complex numbers, std takes the absolute value before squaring, so that the result is always real and nonnegative.

对于浮点输入,std 使用相同的精度计算输入有.根据输入数据,这可能导致结果不准确,特别是对于 float32(见下面的例子).使用 dtype 关键字指定精度更高的累加器可以缓解这个问题.

For floating-point input, the std is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for float32 (see example below). Specifying a higher-accuracy accumulator using the dtype keyword can alleviate this issue.

a = np.zeros((2, 512*512), dtype=np.float32) 
a[0, :] = 1.0 
a[1, :] = 0.1 np.std(a)
>>>0.45000005 

但是对于float64:

a = np.zeros((2, 512*512), dtype=np.float64) 
a[0, :] = 1.0 
a[1, :] = 0.1 
np.std(a)
>>>0.45 

这篇关于Numpy std(标准差)函数奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆