Adult数据集预处理

因为adult数据集没有列名,先设置列名

复制代码
df = pd.read_csv('adult.csv', header = None, names =
['age', 'workclass', 'fnlwgt', 'education', 'education-num', 'marital-status', 'occupation', 'relationship',  'race',
 'sex', 'capital-gain', 'capital-loss', 'hours-per-week', 'native-country', 'income'])

查看是否有缺失值,结果是没有

复制代码
print(df.tail())
for col in df. columns:
    if type(df[col][0]) is str:
        print ("unknown value count in "+ col +" is "+ str(df[df[col]=='unknown']['income'].count()))

现在观察每列有几种可能,方便进行哑编码或者独热编码。adult数据集一共有以下14个数据特征,其中数字类型的特征不需要进行处理。

图片出自http://t.csdnimg.cn/uIe5s

因为人口普查员序号和预测任务没有关系,所以删掉fnlwgt列。又因为75%以上的人是没有资本收益和资本输出的,所以capital-gain和capital-loss也不需要。所以,删掉这三列。

复制代码
df.drop('fnlwgt', axis = 1, inplace = True)
df.drop('capital-gain', axis = 1, inplace = True)
df.drop('capital-loss', axis = 1, inplace = True)
复制代码
age、education-num、hours-per-week这三列都是数值,不需要更改。

对缺失值进行众数填充,以及把二分类问题转换成0和1

复制代码
df.replace(" ?", pd.NaT, inplace = True)
df.replace(" >50K", 1, inplace = True)
df.replace(" <=50K", 0, inplace = True)
df.replace(" Male", 1, inplace = True)
df.replace(" Female", 0, inplace = True)
trans = {'workclass' : df['workclass'].mode()[0], 'occupation' : df['occupation'].mode()[0], 'native-country' : df['native-country'].mode()[0]}
df.fillna(trans, inplace = True)

接下来查看字符型的特别有几种可能。

首先是workclass列

复制代码
counts = df['workclass'].value_counts()
print(counts)

有8种可能

education有16种可能

婚姻状况有7种可能

职业有种可能14种可能

社会角色有6种可能

种族有5种可能

性别显然有两种可能

国籍有41种可能(太多了不截图了)

所以'workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'native-country'这7列数据需要进行独热编码

可以看到进行独热编码后一共有103列,32561.

正常adult数据集一共有14个数据特征,其中删除三列,另有7个特征需要进行独热编码,这7个特征一共会产生97个列。

所以最终列数应该是14-3+97-7=101,但是实际有103列是包含了序号列和收入列。

所有预处理编码如下所示

复制代码
import pandas as pd
import random
df = pd.read_csv('adult.csv', header = None, names =
['age', 'workclass', 'fnlwgt', 'education', 'education-num', 'marital-status', 'occupation', 'relationship',  'race',
 'sex', 'capital-gain', 'capital-loss', 'hours-per-week', 'native-country', 'income'])

print(df.tail())
for col in df. columns:
    if type(df[col][0]) is str:
        print ("unknown value count in "+ col +" is "+ str(df[df[col]=='unknown']['income'].count()))

df.replace(" ?", pd.NaT, inplace = True)
df.replace(" >50K", 1, inplace = True)
df.replace(" <=50K", 0, inplace = True)
df.replace(" Male", 1, inplace = True)
df.replace(" Female", 0, inplace = True)
trans = {'workclass' : df['workclass'].mode()[0], 'occupation' : df['occupation'].mode()[0], 'native-country' : df['native-country'].mode()[0]}
df.fillna(trans, inplace = True)
print(df.describe())
df.drop('fnlwgt', axis = 1, inplace = True)
df.drop('capital-gain', axis = 1, inplace = True)
df.drop('capital-loss', axis = 1, inplace = True)
print(df.head())
# 对指定列进行独热编码
encoded_cols = pd.get_dummies(df[['workclass', 'education', 'marital-status', 'occupation', 'relationship',  'race', 'native-country']])

# 将独热编码后的列与原始数据进行合并
df = pd.concat([df, encoded_cols], axis=1)

# 删除原始的 'job', 'contact', 'marital' 列
df = df.drop(['workclass', 'education', 'marital-status', 'occupation', 'relationship',  'race', 'native-country'], axis=1)
# counts = df['native-country'].value_counts()
# print(counts)
# 将 'y' 列移动到最后一列
y_column = df.pop('income')  # 移除 'y' 列并返回该列
df['income'] = y_column  # 将 'y' 列添加到 DataFrame 的最后一列
df.to_csv('processed_data.csv', index=True)

参考文章:
【精选】Adult数据集分析(一)_云隐雾匿的博客-CSDN博客

Adult数据集分析及四种模型实现-CSDN博客

相关推荐
寒月霜华5 小时前
机器学习-模型验证
人工智能·深度学习·机器学习
救救孩子把6 小时前
3-机器学习与大模型开发数学教程-第0章 预备知识-0-3 函数初步(多项式、指数、对数、三角函数、反函数)
人工智能·数学·机器学习
CareyWYR6 小时前
每周AI论文速递(250908-250912)
人工智能
张晓~183399481216 小时前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
deephub6 小时前
量子机器学习入门:三种数据编码方法对比与应用
人工智能·机器学习·量子计算·数据编码·量子机器学习
AI 嗯啦7 小时前
计算机视觉----opencv实战----指纹识别的案例
人工智能·opencv·计算机视觉
max5006007 小时前
基于多元线性回归、随机森林与神经网络的农作物元素含量预测及SHAP贡献量分析
人工智能·python·深度学习·神经网络·随机森林·线性回归·transformer
trsoliu7 小时前
前端基于 TypeScript 使用 Mastra 来开发一个 AI 应用 / AI 代理(Agent)
前端·人工智能
白掰虾7 小时前
STM32N6&AI资料汇总
人工智能·stm32·嵌入式硬件·stm32n6·stm32ai
爱思德学术8 小时前
中国计算机学会(CCF)推荐学术会议-C(软件工程/系统软件/程序设计语言):MSR 2026
人工智能·机器学习·软件工程·数据科学