chimerge算法matlab实现,有监督的卡方分箱算法
实现代码
import numpy as np
import pandas as pd
from collections import Counter
def chimerge(data, attr, label, max_intervals):
distinct_vals = sorted(set(data[attr])) # Sort the distinct values
labels = sorted(set(data[label])) # Get all possible labels
empty_count = {l: 0 for l in labels} # A helper function for padding the Counter()
intervals = [[distinct_vals[i], distinct_vals[i]] for i in range(len(distinct_vals))] # Initialize the intervals for each attribute
while len(intervals) > max_intervals: # While loop
chi = []
for i in range(len(intervals)-1):
# Calculate the Chi2 value
obs0 = data[data[attr].between(intervals[i][0], intervals[i][1])]
obs1 = data[data[attr].between(intervals[i+1][0], intervals[i+1][1])]
total = len(obs0) + len(obs1)
count_0 = np.array([v for i, v in {**empty_count, **Counter(obs0[label])}.items()])
count_1 = np.array([v for i, v in {**empty_count, **Counter(obs1[label])}.items()])
count_total = count_0 + count_1
expected_0 = count_total*sum(count_0)/total
expected_1 = count_total*sum(count_1)/total
chi_ = (count_0 - expected_0)**2/expected_0 + (count_1 - expected_1)**2/expected_1
chi_ = np.nan_to_num(chi_) # Deal with the zero counts
chi.append(sum(chi_)) # Finally do the summation for Chi2
min_chi = min(chi) # Find the minimal Chi2 for current iteration
for i, v in enumerate(chi):
if v == min_chi:
min_chi_index = i # Find the index of the interval to be merged
break
new_intervals = [] # Prepare for the merged new data array
skip = False
done = False
for i in range(len(intervals)):
if skip:
skip = False
continue
if i == min_chi_index and not done: # Merge the intervals
t = intervals[i] + intervals[i+1]
new_intervals.append([min(t), max(t)])
skip = True
done = True
else:
new_intervals.append(intervals[i])
intervals = new_intervals
for i in intervals:
print(‘[‘, i[0], ‘,‘, i[1], ‘]‘, sep=‘‘)
使用例子
iris = pd.read_csv(‘http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data‘, header=None)
iris.columns = [‘sepal_l‘, ‘sepal_w‘, ‘petal_l‘, ‘petal_w‘, ‘type‘]
for attr in [‘sepal_l‘, ‘sepal_w‘, ‘petal_l‘, ‘petal_w‘]:
print(‘Interval for‘, attr)
chimerge(data=iris, attr=attr, label=‘type‘, max_intervals=3)
结果:
原文:https://www.cnblogs.com/hichens/p/13585854.html
chimerge算法matlab实现,有监督的卡方分箱算法相关推荐
- python 卡方分箱算法_python实现二分类的卡方分箱示例
解决的问题: 1.实现了二分类的卡方分箱: 2.实现了最大分组限定停止条件,和最小阈值限定停止条件: 问题,还不太清楚,后续补充. 1.自由度k,如何来确定,卡方阈值的自由度为 分箱数-1,显著性水平 ...
- python分箱代码_python实现二分类的卡方分箱示例
解决的问题: 1.实现了二分类的卡方分箱: 2.实现了最大分组限定停止条件,和最小阈值限定停止条件: 问题,还不太清楚,后续补充. 1.自由度k,如何来确定,卡方阈值的自由度为 分箱数-1,显著性水平 ...
- python 卡方分箱算法_特征工程 - 分箱
卡方分箱 python自帶分箱函數 -- 無法實現對分類型數據的分箱,可借助卡方分箱算法實現 import numpy as np import pandas as pd data = np.ra ...
- python评分卡建模-卡方分箱(2)之代码实现
卡方值计算 计算卡方值的函数需要输入numpy格式的频数表.对于pandas数据集,只需使用pd.crosstab计算即可,例如变量"总账户数" 与 目标变量 "是否坏客 ...
- python评分卡建模-卡方分箱
今天主要给大家讲讲卡方分箱算法ChiMerge.先给大家介绍一下经常被提到的卡方分布和卡方检验是什么. 一.卡方分布 卡方分布(chi-square distribution, χ2-distribu ...
- 卡方 python_Python评分卡建模—卡方分箱
原标题:Python评分卡建模-卡方分箱 今天主要给大家讲讲卡方分箱算法ChiMerge.先给大家介绍一下经常被提到的卡方分布和卡方检验是什么. 一.卡方分布 卡方分布(chi-square dist ...
- python卡方分箱_Python评分卡建模—卡方分箱
为服从自由度为k的卡方分布,记作: 或者记作 . 二.卡方检验 χ2检验是以χ2分布为基础的一种假设检验方法,主要用于分类变量之间的独立性检验. 其基本思想是根据样本数据推断总体的分布与期望分布是否有 ...
- 逻辑回归预测事件发生的概率_通过逻辑回归,对信用卡申请数据使用卡方分箱法预测违约率建模...
一. 建模步骤 (1)从数据中衍生特征 (2)对类别型变量和数值型变量进行补缺 (3)基于卡方分箱法对变量进行分箱 (4)WOE编码后的单变量分析与多变量分析 (5)应用逻辑回归模型 (6)尺度化 ( ...
- 特征离散化(一) 之 卡方分箱
特征离散化(一) 之 卡方分箱 特征离散化(二) 之 Chi2分箱 特征离散化(三) 之 最小熵分箱 特征离散化(四) 之 bestKS分箱 特征离散化(五) 之 评分卡最优分箱 离散特征在数据挖掘的 ...
最新文章
- 计算机网络中路由器,浅析计算机网络中的路由器
- c语言中getche的用法,c语言中getch的用法
- java model 中文乱码_Java解压zip 解决编码和中文乱码问题
- python软件怎么用-用Python如何打出你的第一个程序
- SQL Server存储过程里全库查找引用的数据库对象(表、存储过程等)
- ExtJS + Gears
- appium和selenium不同与相同之处
- leetcode题解-买卖股票的最佳时机
- Flutter 页面滚动吸顶详解(NestedScrollView)
- 《数据结构与算法分析》习题-----第二章(3)(关于list的题目)
- python中日期使用_在Python中使用日期时间的每月总金额
- 支付宝授权登陆php,php实现支付宝授权登录
- Cisco交换机下载IOS
- su室外渲染参数设置_【QA答疑】VRay3.4 for SketchUp2017 渲染参数设置
- 「短篇小说」灵囚 540 天
- 为什么Jmeter 运行时时到达持续时间不停止?
- Java快递配送管理系统
- 用m ip衡量的计算机性能指标是,ip网络技术要求网络性能参数与指标.pdf.pdf
- caffe与Python接口的配置(VC2013 Windows CUDA7.5 Python2.7.12)
- RSHELIOS速腾32线激光配置记录