转载:https://blog.csdn.net/u010665216/article/details/78528261

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

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]

我们将预测值从小到大排列:

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)

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

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()

我们将数据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()

计算橙色区域面积:

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)

最大可能的基尼系数:

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

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)

数据挖掘中的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))

Gini系数的原理的更多相关文章

  1. Gini 系数与熵的关系

    首先来看二者的基本定义: ⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪H(X)=−∑k=1KpklnpkGini(X)=∑k=1Kpk(1−pk) 将 f(x)=−lnx 在 x=1 处进行一阶泰勒展开(忽略高阶无穷小 ...

  2. CART(分类回归树)原理和实现

    前面我们了解了决策树和adaboost的决策树墩的原理和实现,在adaboost我们看到,用简单的决策树墩的效果也很不错,但是对于更多特征的样本来说,可能需要很多数量的决策树墩 或许我们可以考虑使用更 ...

  3. sklearn_随机森林random forest原理_乳腺癌分类器建模(推荐AAA)

     sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  4. 3.决策树ID3算法原理

    1.决策树的作用 主要用于解决分类问题的一种算法 2.建立决策树的3中常用算法 1).ID3--->信息增益 2).c4.5--> 信息增益率 4).CART Gini系数 3.提出问题: ...

  5. cart中回归树的原理和实现

    前面说了那么多,一直围绕着分类问题讨论,下面我们开始学习回归树吧, cart生成有两个关键点 如何评价最优二分结果 什么时候停止和如何确定叶子节点的值 cart分类树采用gini系数来对二分结果进行评 ...

  6. 拆系数FFT及其部分优化

    模拟考某题一开始由于校内OJ太慢直接拆系数FFT跑不过 后来被神仙婊了一顿之后发现复杂度写炸了改了改随便过 模版题:任意模数NTT 三模数NTT 常数巨大,跑的极慢 拆系数FFT 原理是对于两个多项式 ...

  7. 决策树decision tree原理介绍_python sklearn建模_乳腺癌细胞分类器(推荐AAA)

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  8. 大白话5分钟带你走进人工智能-第31节集成学习之最通俗理解GBDT原理和过程

    目录 1.前述 2.向量空间的梯度下降: 3.函数空间的梯度下降: 4.梯度下降的流程: 5.在向量空间的梯度下降和在函数空间的梯度下降有什么区别呢? 6.我们看下GBDT的流程图解: 7.我们看一个 ...

  9. 用cart(分类回归树)作为弱分类器实现adaboost

    在之前的决策树到集成学习里我们说了决策树和集成学习的基本概念(用了adaboost昨晚集成学习的例子),其后我们分别学习了决策树分类原理和adaboost原理和实现, 上两篇我们学习了cart(决策分 ...

随机推荐

  1. linux查看网络信息命令

    #遇到一条很长的命令怎么办,想换行的时候在末尾增加\符号就可以换行继续输入命令了 #在ifconfig内容中找出ip [root@bogon ~]# ifconfig ens33|grep " ...

  2. MySQL InnoDB Engine--缓冲器数据交换

    通常情况下,缓冲池无法将整个数据库所有数据都进行缓冲,而且不同数据的访问频率不一样,有些数据会被频繁访问,而有些数据可能数月不会被访问一次,因此数据库使用最近最少使用LRU latest Recent ...

  3. Understanding how uid and gid work in Docker containers

    转自:https://medium.com/@mccode/understanding-how-uid-and-gid-work-in-docker-containers-c37a01d01cf Un ...

  4. Gravitee.io alert 引擎架构

    alert 在我们的实际开发中应用的场景很多,我们需要进行系统状态的查看,以及特殊异常请求的处理 参考架构图 从下图可以看出,还是很方便的,同时支持slack email... 的实时消息通知,而且我 ...

  5. Coding kata: get the top two teams in one group

    In this week, we did a coding kata, the subject is to select the top two teams of football group mat ...

  6. Js时间格式[转载]

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...

  7. mongodb之 复制集维护小结

    原文地址:https://www.cnblogs.com/zhaowenzhong/p/5667312.html 一.新增副本集成员 1.登录primary 2.use admin >rs.ad ...

  8. spring cloud 各子项目作用

    spring cloud 各子项目作用: table th:first-of-type { width: 80px; } table th:nth-of-type(2) { width: 150px; ...

  9. centos7编译安装php7.2

    去官网下载php7.2安装包,选择一个结点下载:http://php.net/downloads.php 下载:wget -ivh http://cn.php.net/distributions/ph ...

  10. Sqoop 导入及导出表数据子集命令详解

    Sqoop命令详解 1.import命令 案例1:将mysql表test中的数据导入hive的hivetest表,hive的hivetest表不存在. sqoop import --connect j ...