背景
让我们想想,有一个价值列表,显示一个人的活动长达几个小时。那个人在那个时候没有任何动静。因此,所有的值都是0。
提出了什么问题?
在Google上搜索,我发现了以下的偏度公式。同样的公式也适用于其他一些网站。在分母部分,标准偏差(SD)包括在内。对于类似的非零值列表(例如,1,1,1)和0值(即0,0,0),SD将为0。因此,我应该得到NaN (除以0)的偏斜度。令人惊讶的是,我在调用pandas.DataFrame.skew()时得到0。

我的问题
为什么当值列表的SD为0时,pandas.DataFrame.skew()返回0?
最小可重现性示例
import pandas as pd
ot_df = pd.DataFrame(data={'Day 1': [0, 0, 0, 0, 0, 0],
'Day 2': [0, 0, 0, 0, 0, 0],
'Day 3': [0, 0, 0, 0, 0, 0]})
print(ot_df.skew(axis=1))备注:我检查了这个站点的几个问答(例如,这个(熊猫是如何计算斜度的?))和其他的(例如,这一个 of GitHub)。但我没有找到我的问题的答案。
发布于 2022-01-14 09:10:42
您可以在这里找到实现:https://github.com/pandas-dev/pandas/blob/main/pandas/core/nanops.py
正如你所看到的,有一个:
with np.errstate(invalid="ignore", divide="ignore"):
result = (count * (count - 1) ** 0.5 / (count - 2)) * (m3 / m2 ** 1.5)
dtype = values.dtype
if is_float_dtype(dtype):
result = result.astype(dtype)
if isinstance(result, np.ndarray):
result = np.where(m2 == 0, 0, result)
result[count < 3] = np.nan
else:
result = 0 if m2 == 0 else result
if count < 3:
return np.nan如您所见,如果m2 (对于所有常量值为0 )为0,则结果为0。
如果你问它为什么是这样实施的,我只能推测。我想,这样做是出于实际的原因--如果您正在计算偏度,您想要检查变量的分布是否是对称的(您可以说,它确实是:https://stats.stackexchange.com/questions/114823/skewness-of-a-random-variable-that-have-zero-variance-and-zero-third-central-mom)。
编辑:它是由:https://github.com/pandas-dev/pandas/issues/11974 https://github.com/pandas-dev/pandas/pull/12121完成的
在变量的常量值情况下,您可能会添加一个问题,以便在此方法的行为上添加标志。它应该很容易修复。
https://stackoverflow.com/questions/70707850
复制相似问题