当前位置: 首页 > news >正文

模块四-数据转换与操作——24. 数据分箱

24. 数据分箱

1. 概述

数据分箱(Binning)是将连续变量离散化的过程,将数值范围划分为多个区间,每个区间称为一个"箱"。分箱常用于将连续变量转换为分类变量,便于分析和建模。

importpandasaspdimportnumpyasnp# 创建示例数据np.random.seed(42)df=pd.DataFrame({'姓名':[f'用户_{i}'foriinrange(1,21)],'年龄':np.random.randint(18,70,20),'收入':np.random.randint(3000,30000,20),'分数':np.random.randint(0,100,20),'消费次数':np.random.randint(1,100,20)})print("原始数据:")print(df.head())

2. cut() - 等宽分箱

2.1 基本用法

cut()将数据按指定的区间边界进行分箱。

# 定义年龄区间age_bins=[0,30,50,100]age_labels=['青年','中年','老年']df['年龄段']=pd.cut(df['年龄'],bins=age_bins,labels=age_labels)print("年龄段分布:")print(df[['年龄','年龄段']].head())# 查看各区间计数print("\n年龄段统计:")print(df['年龄段'].value_counts())

2.2 自动生成区间

# 指定区间数量,自动等宽分箱score_bins=4# 分成4等份score_labels=['低','中低','中高','高']df['分数等级']=pd.cut(df['分数'],bins=score_bins,labels=score_labels)print("分数等级(等宽):")print(df[['分数','分数等级']].head(10))print("\n区间分布:")print(df['分数等级'].value_counts().sort_index())

2.3 查看区间边界

# 不指定 labels,查看区间范围age_groups=pd.cut(df['年龄'],bins=[0,30,50,100])print("年龄区间:")print(age_groups.head())# 查看区间类别print("\n区间类别:")print(age_groups.cat.categories)

3. qcut() - 等频分箱

qcut()根据数据的分位数进行分箱,使每个箱包含大致相同数量的样本。

# 等频分箱(4等份)df['收入等级_q']=pd.qcut(df['收入'],q=4,labels=['低','中低','中高','高'])print("收入等级(等频):")print(df[['收入','收入等级_q']].head())# 查看各区间样本数(应该大致相等)print("\n等频分箱统计:")print(df['收入等级_q'].value_counts().sort_index())

4. cut() vs qcut() 对比

# 创建极端数据np.random.seed(42)data=pd.DataFrame({'值':np.random.exponential(10,100)# 指数分布数据})# 等宽分箱data['等宽']=pd.cut(data['值'],bins=5,labels=['箱1','箱2','箱3','箱4','箱5'])# 等频分箱data['等频']=pd.qcut(data['值'],q=5,labels=['箱1','箱2','箱3','箱4','箱5'])print("等宽 vs 等频对比:")print("等宽分箱分布:")print(data['等宽'].value_counts().sort_index())print("\n等频分箱分布:")print(data['等频'].value_counts().sort_index())

5. 自定义分箱规则

5.1 使用自定义函数

defcustom_binning(x):ifx<30:return'青年'elifx<50:return'中年'else:return'老年'df['年龄段_自定义']=df['年龄'].apply(custom_binning)print("自定义分箱:")print(df[['年龄','年龄段_自定义']].head())

5.2 使用字典映射

# 先使用 cut,再映射age_bins=[0,30,50,100]df['年龄段代码']=pd.cut(df['年龄'],bins=age_bins,labels=False)print("年龄段代码:")print(df[['年龄','年龄段代码']].head())# 代码映射为中文code_map={0:'青年',1:'中年',2:'老年'}df['年龄段_映射']=df['年龄段代码'].map(code_map)

6. 分箱后的聚合分析

# 按年龄段统计平均收入print("各年龄段平均收入:")print(df.groupby('年龄段')['收入'].mean().round(0))# 按分数等级统计平均收入print("\n各分数等级平均收入:")print(df.groupby('分数等级')['收入'].mean().round(0))# 多维度分组print("\n年龄段 × 分数等级 平均收入:")pivot=df.pivot_table(values='收入',index='年龄段',columns='分数等级',aggfunc='mean').round(0)print(pivot)

7. 完整示例:客户价值分群

# 创建客户数据np.random.seed(42)customers=pd.DataFrame({'客户ID':[f'CUST{i:04d}'foriinrange(1,101)],'年消费金额':np.random.exponential(5000,100).round(0),'购买次数':np.random.poisson(15,100),'客单价':np.random.normal(300,100,100).round(0),'会员天数':np.random.randint(1,1000,100)})# 修正负值customers['客单价']=customers['客单价'].clip(lower=0)print("="*60)print("客户价值分群分析")print("="*60)print("\n原始数据统计:")print(customers[['年消费金额','购买次数','客单价','会员天数']].describe())# 1. 消费金额分箱(等频)customers['消费等级']=pd.qcut(customers['年消费金额'],q=4,labels=['低消费','中低消费','中高消费','高消费'])print("\n1. 消费等级分布:")print(customers['消费等级'].value_counts())# 2. 购买频次分箱freq_bins=[0,5,10,20,200]freq_labels=['低频','中低频','中高频','高频']customers['频次等级']=pd.cut(customers['购买次数'],bins=freq_bins,labels=freq_labels)print("\n2. 频次等级分布:")print(customers['频次等级'].value_counts())# 3. 客单价分箱price_bins=[0,200,300,400,1000]price_labels=['低单价','中单价','高单价','超高单价']customers['单价等级']=pd.cut(customers['客单价'],bins=price_bins,labels=price_labels)print("\n3. 单价等级分布:")print(customers['单价等级'].value_counts())# 4. 会员时长分箱days_bins=[0,30,90,180,365,1000]days_labels=['新客','活跃','忠诚','资深','元老']customers['会员等级']=pd.cut(customers['会员天数'],bins=days_bins,labels=days_labels)print("\n4. 会员等级分布:")print(customers['会员等级'].value_counts())# 5. 客户价值评分defcalculate_score(row):# 消费金额得分(0-40分)amount_score=min(row['年消费金额']/20000*40,40)# 频次得分(0-30分)freq_score=min(row['购买次数']/30*30,30)# 单价得分(0-20分)price_score=min(row['客单价']/500*20,20)# 忠诚度得分(0-10分)loyalty_score=min(row['会员天数']/365*10,10)returnround(amount_score+freq_score+price_score+loyalty_score,2)customers['价值得分']=customers.apply(calculate_score,axis=1)# 6. 客户分群score_bins=[0,25,50,75,100]score_labels=['低价值','中低价值','中高价值','高价值']customers['客户分群']=pd.cut(customers['价值得分'],bins=score_bins,labels=score_labels)print("\n5. 客户分群分布:")print(customers['客户分群'].value_counts())# 7. 分群统计print("\n6. 各客户群平均指标:")segment_stats=customers.groupby('客户分群').agg({'年消费金额':'mean','购买次数':'mean','客单价':'mean','会员天数':'mean','价值得分':'mean'}).round(2)print(segment_stats)# 8. 交叉分析print("\n7. 消费等级 × 会员等级 分布:")cross_tab=pd.crosstab(customers['消费等级'],customers['会员等级'])print(cross_tab)

8. 分箱方法对比

方法特点适用场景
cut()等宽区间宽度相同数据均匀分布
cut()自定义边界灵活控制区间有业务含义的边界
qcut()等频每箱样本数相同数据倾斜、需要平衡样本
apply()自定义完全自定义复杂逻辑

9. 总结

函数说明示例
cut(x, bins)等宽分箱pd.cut(df['age'], bins=[0,30,60,100])
cut(x, bins, labels)带标签分箱pd.cut(df['age'], bins=4, labels=['A','B','C','D'])
qcut(x, q)等频分箱pd.qcut(df['income'], q=4)
cut(..., labels=False)返回区间代码pd.cut(df['age'], bins=3, labels=False)
value_counts()统计分布df['bin'].value_counts()

http://www.jsqmd.com/news/824805/

相关文章:

  • 2026年重磅上新:评价好的瓷砖研发厂家 - 品牌推广大师
  • Linux重定向与管道:从文件描述符到高效命令行工作流
  • 多智能体协作框架AgentStack:从单体智能到协作智能的范式跃迁
  • 【绝密工作流】:政治学研究者不愿公开的NotebookLM三重验证法——事实核查、逻辑链补全、立场偏差识别
  • 杰理之似于“PO”声,如果切换的时机刚好在音量较高的时候,比较容易出现【篇】
  • AMD Ryzen硬件调试终极指南:SMUDebugTool深度探索与实战应用
  • 第四章-11-主机状态
  • 基于MCP协议与Graph API实现AI助手无缝集成Outlook邮箱
  • 从零构建STM32MP157异构通信链路:OpenAMP框架实战解析
  • 跟着 MDN 学 HTML day_51:(深入理解 XPathEvaluator 接口)
  • Midjourney v7风格漂移现象权威报告:NVIDIA A100实测数据显示,未启用--stylize 500时风格稳定性下降67.3%
  • SAR ADC设计新手必看:用VerilogA理想DAC模型加速你的动态性能评估
  • AI增强渗透测试:LLM辅助安全评估的架构设计与实战指南
  • 树莓派Pico上使用Blinka兼容层调用CircuitPython传感器库
  • Power PMAC玩转EtherCAT:手把手教你配置Elmo驱动器循环力矩模式(CST)
  • 如何用Python脚本破解百度网盘限速:完整免费教程与实战指南
  • AI赋能代码冻结期:智能协作框架提升研发效能
  • 3步解决PUBG压枪难题:罗技鼠标宏智能压枪脚本深度解析
  • 模块四-数据转换与操作——25. 哑变量与编码
  • 别再乱发优惠券了!用Python的CausalML库精准定位‘策略提升用户’,提升营销ROI
  • 别再让棋盘格照片吃灰了!用Python+OpenCV手把手教你搞定相机畸变校准(附完整代码)
  • 第四章-12-环境变量
  • Intel Lunar Lake核显架构解析:Xe2-LPG如何重塑轻薄本图形性能
  • RK3399嵌入式AI人脸识别终端开发:硬件架构、软件栈与实战优化
  • Burp Suite HTTPS证书安装与配置实战指南
  • 3分钟搞定!FigmaCN终极中文插件:让英文界面秒变中文的免费神器
  • Aviator表达式引擎:从编译优化到规则引擎实战
  • GreenDFL框架:去中心化联邦学习的可持续性优化实践
  • AWS实战:基于Python与Aurora pgvector构建企业级RAG应用
  • IAR全面支持CW32 MCU:从环境搭建到深度优化的嵌入式开发实战