这篇文章是今天发布的CTGAN的补充,我们可以使用pandas的cut函数将数据进行离散化、将连续变量进行分段汇总,这比写自定义函数要简单的多。
需要注意的是pandas默认的分段数值必须要多一位,否则会报错(分段数值也可以是负数)。
下面看看代码,还是使用泰坦尼克数据集:
import seaborn as sns
import pandas as pd
import numpy as np
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)df=pd.read_csv("titanic.csv")
# You also get the same dataset from seaborn
#df=sns.load_dataset('titanic').head(10)df.head()
df['New_Age']=pd.cut(df['Age'],[0,10,18,25,40,90])
df.pivot_table("Survived", index=["Sex","Pclass"],columns="new_age")
另外一个函数是qcut:他会根据这些值的频率来选择箱子的均匀间隔,即每个箱子中含有的数的数量是相同的。
本文分享自 DeepHub IMBA 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有