引言

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

基尼系数的经济学解释

首先,我们先看一张从Wiki上找来的经典图片:

基尼系数是一个分布不平衡程度的度量。它被定义成大小在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+1−2∑i=1n​yi​∑i=1n​(n+1−i)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+1−2∑i=1n​yi​∑i=1n​(n+1−i)yi​​)(1)
那么这个公式我在这里将它拆分解释下:

  • n代表y的个数
  • ∑ i = 1 n y i \sum_{i=1}^ny_i ∑i=1n​yi​代表总财富值
  • ∑ i = 1 n ( n + 1 − i ) y i \sum_{i=1}^n(n+1-i)y_i ∑i=1n​(n+1−i)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()


上图显示的折线就与我们从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()

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)

[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() / totalLossesginiSum -= (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文章,请戳:传送门

Gini coefficient直观的解释与实现相关推荐

  1. 如何直观的解释back propagation算法?

    作者:Evan Hoo 链接:http://www.zhihu.com/question/27239198/answer/89853077 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非 ...

  2. js值的拷贝和值的引用_到达P值的底部:直观的解释

    js值的拷贝和值的引用 介绍 (Introduction) Welcome to this lesson on calculating p-values. 欢迎参加有关计算p值的课程. Before ...

  3. SLAM后端优化中卡尔曼滤波的直观通俗解释

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文由知乎 郑纯然 授权转载 原文链接: https://zhua ...

  4. 反向传播BP 算法之一种直观的解释

    0. 前言 之前上模式识别课程的时候,老师也讲过 MLP 的 BP 算法, 但是 ppt 过得太快,只有一个大概印象.后来课下自己也尝试看了一下 stanford deep learning 的 wi ...

  5. 卷积神经网络工作原理直观的解释?

    查看全部 35 个回答 YJango 1 日本会津大学 人机界面实验室博士在读 741 人赞同了该回答 该文是卷积神经网络--介绍,并假设你理解前馈神经网络. 如果不是,强烈建议你读完如何简单形象又有 ...

  6. 对于fmri的设计矩阵构造的一个很直观的解释-by 西南大学xulei教授

    本程序意在解释这样几个问题:完整版代码在本文的最后. 1.实验的设计如何转换成设计矩阵?2.设计矩阵的每列表示一个刺激条件,如何确定它们?3.如何根据设计矩阵和每个体素的信号求得该体素对刺激的敏感性? ...

  7. 如何直观地解释 back propagation 算法?

    简单的理解,它的确就是复合函数的链式法则,但其在实际运算中的意义比链式法则要大的多. 多层神经网络的本质就是一个多层复合的函数.借用网上找到的一幅图[1],来直观描绘一下这种复合关系. 其对应的表达式 ...

  8. em算法直观_直观地解释了10种图形算法

    em算法直观 重点 (Top highlight) Graphs have become a powerful means of modelling and capturing data in rea ...

  9. 10种图算法直观可视化解释

    快速介绍10个基本的图算法,并举例和可视化 图已经成为一种强大的建模和捕获真实场景中的数据的手段,比如社交媒体网络.网页和链接,以及GPS中的位置和路线.如果您有一组相互关联的对象,那么您可以使用图来 ...

最新文章

  1. 模拟实现: strstr strcpy strlen strcat strcmp memcpy memmove
  2. 《JavaScript高级程序设计》心得笔记-----第四篇章
  3. “象征界”的奇观:刘天怜花鸟工笔作品印象
  4. 人工智能AI实战100讲(六)-利用CNN来检测伪造图像
  5. SpringBoot 2.x (3):文件上传
  6. 转变思维!采用稀疏化加速 YoloV3 模型!省了 GPU !
  7. 大数据学习笔记54:HBase概述
  8. MySQL存储过程中游标使用
  9. python的sdk是什么意思_python sdk
  10. 如何胜任一个小型公司的技术总监?
  11. C根据输入的城市坐标求各城市间的距离
  12. 软件质量模型详解————思维导图
  13. numpy的使用(一)(reshape()有待研究)
  14. npm i --legacy-peer-deps
  15. 用DirectX12绘制一个几何体的程序详述
  16. 英特尔cpu与主板芯片组对应关系(包含12代)
  17. reduceByKey中的加号是什么意思
  18. RAD Studio 10.3.1 cannot initialize object parameter of type..........“
  19. 互联网日报 | 6月20日 星期日 | 宁德时代否认强制员工购买特斯拉;小米618支付金额破190亿元;岚图FREE正式上市...
  20. c语言二级编程实例,二级c语言编程 -实例

热门文章

  1. 原理图库元件符号之光耦模型的创建
  2. “玲珑杯”ACM比赛 Round #15 A
  3. ps制作普通文字步骤
  4. xss-labs/level-3
  5. 黄淮学院和安阳工学院计算机,一本烂大街了?河南这六所二本高校硕士点都没有,却升入一本招生...
  6. 脑网络阈值选择参考文献
  7. 字符串分割QString::section
  8. python长沙培训学校
  9. 有了开源ROS,机器人就能自由行走?
  10. 如何解决电脑开始菜单任务栏和操作中心灰色无法勾选