数据预处理-定类数据处理 定类离散特征转化为One-Hot-Encoding独热编码

本文介绍了如何将UCI心脏病数据集中用数字表示的定类特征转换为人类可读的类别,如'sex'字段转为'male'和'female'。展示了如何使用Pandas进行数据清洗和One-Hot编码,确保离散特征为object类型,便于后续分析。
摘要由CSDN通过智能技术生成

用 数字标号的定类数据 转化成 类别

如sex 0 1 其中 1是男性 0是女性

可以转化成 male 和female

 本文使用的是uci 心脏病数据集

将整数编码转为实际对应的字符串

df['sex'][df['sex']==0]='female'
df['sex'][df['sex']==1]='male'

df['chest_pain_type'][df['chest_pain_type']==0]='typical angina'
df['chest_pain_type'][df['chest_pain_type']==1]='antypical angina'
df['chest_pain_type'][df['chest_pain_type']==2]='non-anginal pain'
df['chest_pain_type'][df['chest_pain_type']==3]='asymptomatic'

df['fasting_blood_sugar'][df['fasting_blood_sugar']==0]='lower than 120mg/ml'
df['fasting_blood_sugar'][df['fasting_blood_sugar']==1]='greater than 120mg/ml'

df['resting_electrocardiographic'][df['resting_electrocardiographic']==0]='normal'
df['resting_electrocardiographic'][df['resting_electrocardiographic']==1]='ST-T wave abnarmality'
df['resting_electrocardiographic'][df['resting_electrocardiographic']==2]='left wentricular hapertorphy'

df['exercise_induced_angina'][df['exercise_induced_angina']==0]='no'
df['exercise_induced_angina'][df['exercise_induced_angina']==1]='yes'

df['ST_slope'][df['ST_slope']==0]='upsloping'
df['ST_slope'][df['ST_slope']==1]='flat'
df['ST_slope'][df['ST_slope']==2]='downsloping'

df['thal'][df['thal']==0]='unknown'
df['thal'][df['thal']==1]='normal'
df['thal'][df['thal']==2]='fixed defect'
df['thal'][df['thal']==3]='reversable defect'

效果

在pandas中,

离散的定类和定序特征列应该是object类型

连续的定距和定比特征列应该是int64或者float64的浮点数类型

将定类和定序的特征类转化为One-Hot独热编码

df=pd.get_dummies(df)
df.columns

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

beansqq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值