Gini coefficient直观的解释与实现

引言

大家在机器学习中经常会看到基尼系数的词汇,有时候在做比赛的时候,有些赛题的Scoring Metric就是基尼系数。我们去Google或者Baidu,得到的都是些不甚满意的经济学相关的解释。那么在机器学习、数据挖掘领域,基尼系数在实际的应用场景中又该如何解释以及如何实现呢?

基尼系数的经济学解释

首先,我们先看一张从Wiki上找来的经典图片:
Gini
基尼系数是一个分布不平衡程度的度量。它被定义成大小在0到1之间的比值:分子是均匀分布直线与洛伦兹曲线之间的面积,分母是均匀分布直线下方的面积。它是由意大利统计学家Corrado Gini提出并于1912年发表论文:“Variability and Mutability”。

基尼系数的计算

首先我们直接构造赛题结果:真实数据与预测数据

predictions = [0.9, 0.3, 0.8, 0.75, 0.65, 0.6, 0.78, 0.7, 0.05, 0.4, 0.4, 0.05, 0.5, 0.1, 0.1]
actual = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

参考Wiki上关于财富基尼系数计算公式的定义:
In some cases, this equation can be applied to calculate the Gini coefficient without direct reference to the Lorenz curve. For example, (taking y to mean the income or wealth of a person or household): For a population uniform on the values yi, i = 1 to n, indexed in non-decreasing order (yi ≤ yi+1):
G = 1 n ( n + 1 − 2 ∑ i = 1 n ( n + 1 − i ) y i ∑ i = 1 n y i ) ( 1 ) G = \frac{1}{n}(n+1-2\frac{\sum_{i=1}^n(n+1-i)y_i}{\sum_{i=1}^ny_i})\tag{$1$} G=n1(n+12i=1nyii=1n(n+1i)yi)(1)
上面的话我通俗翻译下:在某些情况下,我们能够不直接参考洛伦兹曲线来计算出基尼系数。比如,(假设y代表某人或某个家庭的财富值):序列 y i y_i yi是非递减序列。那么序列 y i y_i yi就代表着从穷人到富人的排列顺序。因此基尼系数的公式就是:
G = 1 n ( n + 1 − 2 ∑ i = 1 n ( n + 1 − i ) y i ∑ i = 1 n y i ) ( 1 ) G = \frac{1}{n}(n+1-2\frac{\sum_{i=1}^n(n+1-i)y_i}{\sum_{i=1}^ny_i})\tag{$1$} G=n1(n+12i=1nyii=1n(n+1i)yi)(1)
那么这个公式我在这里将它拆分解释下:

  • n代表y的个数
  • ∑ i = 1 n y i \sum_{i=1}^ny_i i=1nyi代表总财富值
  • ∑ i = 1 n ( n + 1 − i ) y i \sum_{i=1}^n(n+1-i)y_i i=1n(n+1i)yi代表财富值的累计求和

1.数据转换

在这里我们并没有穷人到富人的数据序列,我们可以将预测值从小到大排列。

# Sort the actual values by the predictions
data = zip(actual, predictions)
sorted_data = sorted(data, key=lambda d: d[1])
sorted_actual = [d[0] for d in sorted_data]
print('Sorted Actual Values', sorted_actual)
[out] Sorted Actual Values [0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1]

2.累计求和

在这里我们对排序后的真实值累计求和:

# Sum up the actual values
cumulative_actual = np.cumsum(sorted_actual)
cumulative_index = np.arange(1, len(cumulative_actual)+1)

plt.plot(cumulative_index, cumulative_actual)
plt.xlabel('Cumulative Number of Predictions')
plt.ylabel('Cumulative Actual Values')
plt.show()

cum_sum
上图显示的折线就与我们从wiki上找来的图片中的洛伦兹曲线相对应。

3.Normalization

接下来我们将数据Normalization到0,1之间。并画出45度线。

cumulative_actual_shares = cumulative_actual / sum(actual)
cumulative_index_shares = cumulative_index / len(predictions)

# Add (0, 0) to the plot
x_values = [0] + list(cumulative_index_shares)
y_values = [0] + list(cumulative_actual_shares)

# Display the 45° line stacked on top of the y values
diagonal = [x - y for (x, y) in zip(x_values, y_values)]

plt.stackplot(x_values, y_values, diagonal)
plt.xlabel('Cumulative Share of Predictions')
plt.ylabel('Cumulative Share of Actual Values')
plt.show()

normalization

4.计算橙色区域面积

我们使用线性代数库scipy,求得橙色区域面积:

fy = scipy.interpolate.interp1d(x_values, y_values)
blue_area, _ = scipy.integrate.quad(fy, 0, 1, points=x_values)
orange_area = 0.5 - blue_area
print('Orange Area: %.3f' % orange_area)
[out] Orange Area: 0.189

5.最大可能的基尼系数

前面我们是按照预测值对真实值排序,得到一个基尼系数;现在我们按照真实值给真实值排序,得到最大可能的基尼系数:

cumulative_actual_shares_perfect = np.cumsum(sorted(actual)) / sum(actual)
y_values_perfect = [0] + list(cumulative_actual_shares_perfect)

# Display the 45° line stacked on top of the y values
diagonal = [x - y for (x, y) in zip(x_values, y_values_perfect)]

plt.stackplot(x_values, y_values_perfect, diagonal)
plt.xlabel('Cumulative Share of Predictions')
plt.ylabel('Cumulative Share of Actual Values')
plt.show()

# Integrate the the curve function
fy = scipy.interpolate.interp1d(x_values, y_values_perfect)
blue_area, _ = scipy.integrate.quad(fy, 0, 1, points=x_values)
orange_area = 0.5 - blue_area
print('Orange Area: %.3f' % orange_area)

maxgini

[out] Orange Area: 0.300

数据挖掘中的Scoring Metric的实现

在这里我们封装好基尼系数的函数,可用来作为比赛中的打分函数。

def gini(actual, pred):
    assert (len(actual) == len(pred))
    all = np.asarray(np.c_[actual, pred, np.arange(len(actual))], dtype=np.float)
    all = all[np.lexsort((all[:, 2], -1 * all[:, 1]))]
    totalLosses = all[:, 0].sum()
    giniSum = all[:, 0].cumsum().sum() / totalLosses

    giniSum -= (len(actual) + 1) / 2.
    return giniSum / len(actual)


def gini_normalized(actual, pred):
    return gini(actual, pred) / gini(actual, actual)


gini_predictions = gini(actual, predictions)
gini_max = gini(actual, actual)
ngini= gini_normalized(actual, predictions)
print('Gini: %.3f, Max. Gini: %.3f, Normalized Gini: %.3f' % (gini_predictions, gini_max, ngini))
[out] Gini: 0.189, Max. Gini: 0.300, Normalized Gini: 0.630

总结

关于Gini系数的pdf文章,请戳:传送门

  • 20
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Roaring Kitty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值