Kaggle比赛:成人人口收入分类

拿到数据首先查看数据信息和描述

python 复制代码
import pandas as pd  
import seaborn as sns  
import matplotlib.pyplot as plt  
  
# 加载数据(保留原路径,但在实际应用中建议使用相对路径或环境变量)  
data = pd.read_csv(r"C:\Users\11794\Desktop\收入分类\training.csv", encoding='utf-8', encoding_errors='replace')  
  
# 查看数据信息和描述  
data.info()

选择数值列进行相关性分析计算相关性矩阵绘制热力图

python 复制代码
import pandas as pd  
import seaborn as sns  
import matplotlib.pyplot as plt  
  
# 加载数据(保留原路径,但在实际应用中建议使用相对路径或环境变量)  
data = pd.read_csv(r"C:\Users\11794\Desktop\收入分类\training.csv", encoding='utf-8', encoding_errors='replace')  
  
# 绘制热力图  
# 选择数值列进行相关性分析  
numerical_columns = data.select_dtypes(include=['int64', 'float64']).columns
# 计算相关性矩阵  
correlation_matrix = data[numerical_columns].corr()  
# 绘制热力图  
plt.figure(figsize=(12, 10))  
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', linewidths=0.5)  
plt.title('Correlation Heatmap')  
plt.savefig('correlation_heatmap.png', bbox_inches='tight')  # 保存热力图到当前目录

随后就是数据分割 ,创建并训练模型,这里我选择用决策树分类器

python 复制代码
import pandas as pd    
from sklearn.model_selection import train_test_split    
from sklearn.tree import DecisionTreeClassifier  # 导入决策树分类器  
from sklearn.metrics import classification_report    
import matplotlib.pyplot as plt    
from sklearn.metrics import roc_curve, auc  
import numpy as np  
  
# 加载数据(假设数据保存在CSV文件中)    
data = pd.read_csv(r"C:\Users\11794\Desktop\收入分类\training.csv", encoding='utf-8', encoding_errors='replace')   
test_data = pd.read_csv(r"C:\Users\11794\Desktop\收入分类\testing.csv", encoding='utf-8', encoding_errors='replace')    
  
# 选择特征和目标变量    
X = data.drop(['id', 'Class'], axis=1)   
y = data['Class']  # 目标变量是'Class'列    
    
# 数据分割    
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.01, random_state=42)    
    
# 创建并训练模型    
# 使用决策树分类器  
model = DecisionTreeClassifier(max_depth=30, random_state=42)  # 修改此行  
model.fit(X_train, y_train)    
   
      
# 预测测试集并评估模型    
y_pred = model.predict(X_test)    
print(classification_report(y_test, y_pred))  # 打印分类报告  
  
# 选择test_data中的特征列    
test_X = test_data.drop(['id'], axis=1)    
# 使用训练好的模型进行预测    
test_y_pred = model.predict(test_X)

准确率直接1.0 我没在验证集验证,比赛的文件也分享在csdn里了。

相关推荐
艾莉丝努力练剑6 分钟前
hixl vs NCCL:昇腾生态通信库的独特优势分析
运维·c++·人工智能·cann
梦帮科技7 分钟前
Node.js配置生成器CLI工具开发实战
前端·人工智能·windows·前端框架·node.js·json
程序员泠零澪回家种桔子8 分钟前
Spring AI框架全方位详解
java·人工智能·后端·spring·ai·架构
Echo_NGC223711 分钟前
【FFmpeg 使用指南】Part 3:码率控制策略与质量评估体系
人工智能·ffmpeg·视频·码率
纤纡.21 分钟前
PyTorch 入门精讲:从框架选择到 MNIST 手写数字识别实战
人工智能·pytorch·python
大大大反派22 分钟前
CANN 生态中的自动化部署引擎:深入 `mindx-sdk` 项目构建端到端 AI 应用
运维·人工智能·自动化
程序猿追23 分钟前
深度解读 AIR (AI Runtime):揭秘 CANN 极致算力编排与调度的核心引擎
人工智能
2601_9495936528 分钟前
深入解析CANN-acl应用层接口:构建高效的AI应用开发框架
数据库·人工智能
●VON30 分钟前
CANN安全与隐私:从模型加固到数据合规的全栈防护实战
人工智能·安全
刘大大Leo36 分钟前
GPT-5.3-Codex 炸了:第一个「自己造自己」的 AI 编程模型,到底意味着什么?
人工智能·gpt