【发布时间】:2018-02-27 01:00:19
【问题描述】:
根据https://stackoverflow.com/a/48981834/1840471,这是 Python 中加权基尼系数的实现:
import numpy as np
def gini(x, weights=None):
if weights is None:
weights = np.ones_like(x)
# Calculate mean absolute deviation in two steps, for weights.
count = np.multiply.outer(weights, weights)
mad = np.abs(np.subtract.outer(x, x) * count).sum() / count.sum()
rmad = mad / np.average(x, weights=weights)
# Gini equals half the relative mean absolute deviation.
return 0.5 * rmad
这很干净,适用于中型数组,但正如其最初的建议 (https://stackoverflow.com/a/39513799/1840471) 中所警告的那样,它是 O(n2)。在我的电脑上,这意味着它会在大约 20k 行后中断:
n = 20000 # Works, 30000 fails.
gini(np.random.rand(n), np.random.rand(n))
可以调整它以适用于更大的数据集吗?我的是 ~150k 行。
【问题讨论】:
-
有关相关统计数据,即加权变异系数,请参阅this answer。在其计算中,它使用标准偏差,而不是绝对偏差。
标签: python numpy variations weighted gini