复习日
仔细回顾一下之前14天的内容,没跟上进度的同学补一下进度。
作业:
尝试找到一个kaggle或者其他地方的结构化数据集,用之前的内容完成一个全新的项目,这样你也是独立完成了一个专属于自己的项目。
要求:
- 有数据地址的提供数据地址,没有地址的上传网盘贴出地址即可。
- 尽可能与他人不同,优先选择本专业相关数据集
- 探索一下开源数据的网站有哪些?
我找的数据集:UCI Machine Learning Repository
该数据集来自 UCI Machine Learning Repository,旨在评估汽车的质量,并根据特定的特征为汽车分类。它的目标是通过一些汽车的属性来判断其在市场中的接受度。这个数据集常用于分类问题,特别适合用于学习和测试分类算法。
开源数据网站推荐:
1、OpenML,开源的机器学习平台,很多实验数据集
2、Find Open Datasets and Machine Learning Projects | Kaggle,全球最活跃的数据竞赛社区,数据集丰富
3、UCI Machine Learning Repository,最早的机器学习数据仓库,结构化数据居多
4、https://datasetsearch.research.google.com/,谷歌做的开源数据集搜索引擎
5、天池数据集_阿里系唯一对外开放数据分享平台-阿里云天池,阿里巴巴天池平台,有很多工业场景数据
实战训练:
1、数据预处理
import pandas as pd # 用于数据处理和分析,可处理表格数据。
import numpy as np # 用于数值计算,提供了高效的数组操作。
import matplotlib.pyplot as plt # 用于绘制各种类型的图表
import seaborn as sns # 基于matplotlib的高级绘图库,能绘制更美观的统计图形。
import warnings
warnings.filterwarnings("ignore")
# 设置中文字体(解决中文显示问题)
plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows系统常用黑体字体
plt.rcParams['axes.unicode_minus'] = False # 正常显示负号
column_names = ["buying", "maint", "doors", "persons", "lug_boot", "safety", "class"]
data = pd.read_csv('car.data', names=column_names) # 读取数据
# buying v-high, high, med, low
# maint v-high, high, med, low
# doors 2, 3, 4, 5-more
# persons 2, 4, more
# lug_boot small, med, big
# safety low, med, high
# class unacc, acc, good, v-good
#print(data.info())
# 先筛选字符串变量
discrete_features = data.select_dtypes(include=['object']).columns.tolist()
#print(discrete_features)
print(data['class'].unique())
buying_mapping = {
'low': 1,
'med': 2,
'high': 3,
'vhigh': 4
}
data['buying'] = data['buying'].map(buying_mapping)
maint_mapping = {
'low': 1,
'med': 2,
'high': 3,
'vhigh': 4
}
data['maint'] = data['maint'].map(maint_mapping)
lug_boot_mapping = {
'small': 1,
'med': 2,
'big': 3,
}
data['lug_boot'] = data['lug_boot'].map(lug_boot_mapping)
safety_mapping = {
'low': 1,
'med': 2,
'high': 3,
}
data['safety'] = data['safety'].map(safety_mapping)
class_mapping = {
'unacc': 0,
'acc': 1,
'good': 2,
'vgood': 3
}
data['class'] = data['class'].map(class_mapping)
doors_mapping = {
'2': 2,
'3': 3,
'4': 4,
'5more': 5
}
data['doors'] = data['doors'].map(doors_mapping)
persons_mapping = {
'2': 2,
'4': 4,
'more': 5
}
data['persons'] = data['persons'].map(persons_mapping)
print(data.head())
print(data.isnull().sum())
2、划分数据集
# 划分数据集
from sklearn.model_selection import train_test_split
X = data.drop(['class'], axis=1) # 特征,axis=1表示按列删除
y = data['class'] # 标签
# 按照8:2划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 80%训练集,20%测试集
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) # (1382, 6) (346, 6) (1382,) (346,)
3、默认参数模型vs带权重模型
import numpy as np # 引入 numpy 用于计算平均值等
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import StratifiedKFold, cross_validate # 引入分层 K 折和交叉验证工具
from sklearn.metrics import make_scorer, accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report
import time
import warnings
warnings.filterwarnings("ignore")
warnings.filterwarnings("ignore") # 忽略所有警告信息
# --- 1. 默认参数的随机森林 ---
# 评估基准模型,这里确实不需要验证集
print("--- 1. 默认参数随机森林 (训练集 -> 测试集) ---")
import time # 这里介绍一个新的库,time库,主要用于时间相关的操作,因为调参需要很长时间,记录下会帮助后人知道大概的时长
start_time = time.time() # 记录开始时间
rf_model = RandomForestClassifier(random_state=42)
rf_model.fit(X_train, y_train) # 在训练集上训练
rf_pred = rf_model.predict(X_test) # 在测试集上预测
end_time = time.time() # 记录结束时间
print(f"训练与预测耗时: {end_time - start_time:.4f} 秒")
print("\n默认随机森林 在测试集上的分类报告:")
print(classification_report(y_test, rf_pred))
print("默认随机森林 在测试集上的混淆矩阵:")
print(confusion_matrix(y_test, rf_pred))
from imblearn.over_sampling import SMOTE
smote = SMOTE(random_state=42)
X_train_smote, y_train_smote = smote.fit_resample(X_train, y_train)
print("SMOTE过采样后训练集的形状:", X_train_smote.shape, y_train_smote.shape)
# --- 2. 带权重的随机森林 + 交叉验证 (在训练集上进行CV) ---
print("--- 2. 带权重随机森林 + 交叉验证 (在训练集上进行) ---")
# 确定少数类标签 (非常重要!)
# 假设是二分类问题,我们需要知道哪个是少数类标签才能正确解读 recall, precision, f1
# 例如,如果标签是 0 和 1,可以这样查看:
counts = np.bincount(y_train)
minority_label = np.argmin(counts) # 找到计数最少的类别的标签
majority_label = np.argmax(counts)
print(f"训练集中各类别数量: {counts}")
print(f"少数类标签: {minority_label}, 多数类标签: {majority_label}")
# !!下面的 scorer 将使用这个 minority_label !!
# 定义带权重的模型
rf_model_weighted = RandomForestClassifier(
random_state=42,
class_weight='balanced' # 关键:自动根据类别频率调整权重
# class_weight={minority_label: 10, majority_label: 1} # 或者可以手动设置权重字典
)
# 设置交叉验证策略 (使用 StratifiedKFold 保证每折类别比例相似)
cv_strategy = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) # 5折交叉验证
# 定义用于交叉验证的评估指标
# 特别关注少数类的指标,使用 make_scorer 指定 pos_label
# 注意:如果你的少数类标签不是 1,需要修改 pos_label
scoring = {
'accuracy': 'accuracy',
'precision_minority': make_scorer(precision_score, average='macro', zero_division=0),
'recall_minority': make_scorer(recall_score, average='macro'),
'f1_minority': make_scorer(f1_score, average='macro')
}
print(f"开始进行 {cv_strategy.get_n_splits()} 折交叉验证...")
start_time_cv = time.time()
# 执行交叉验证 (在 X_train, y_train 上进行)
# cross_validate 会自动完成训练和评估过程
cv_results = cross_validate(
estimator=rf_model_weighted,
X=X_train_smote,
y=y_train_smote,
cv=cv_strategy,
scoring=scoring,
n_jobs=-1, # 使用所有可用的 CPU 核心
return_train_score=False # 通常我们更关心测试折的得分
)
end_time_cv = time.time()
print(f"交叉验证耗时: {end_time_cv - start_time_cv:.4f} 秒")
# 打印交叉验证结果的平均值
print("\n带权重随机森林 交叉验证平均性能 (基于训练集划分):")
for metric_name, scores in cv_results.items():
if metric_name.startswith('test_'): # 我们关心的是在验证折上的表现
# 提取指标名称(去掉 'test_' 前缀)
clean_metric_name = metric_name.split('test_')[1]
print(f" 平均 {clean_metric_name}: {np.mean(scores):.4f} (+/- {np.std(scores):.4f})")
print("-" * 50)
# --- 3. 使用权重训练最终模型,并在测试集上评估 ---
print("--- 3. 训练最终的带权重模型 (整个训练集) 并在测试集上评估 ---")
start_time_final = time.time()
# 使用与交叉验证中相同的设置来训练最终模型
rf_model_weighted_final = RandomForestClassifier(
random_state=42,
class_weight='balanced'
)
rf_model_weighted_final.fit(X_train_smote, y_train_smote) # 在整个训练集上训练
rf_pred_weighted = rf_model_weighted_final.predict(X_test) # 在测试集上预测
end_time_final = time.time()
print(f"最终带权重模型训练与预测耗时: {end_time_final - start_time_final:.4f} 秒")
print("\n带权重随机森林 在测试集上的分类报告:")
# 确保 classification_report 也关注少数类 (可以通过 target_names 参数指定标签名称)
# 或者直接查看报告中少数类标签对应的行
print(classification_report(y_test, rf_pred_weighted)) # , target_names=[f'Class {majority_label}', f'Class {minority_label}'] 如果需要指定名称
print("带权重随机森林 在测试集上的混淆矩阵:")
print(confusion_matrix(y_test, rf_pred_weighted))
print("-" * 50)
# 对比总结 (简单示例)
print("性能对比 (测试集上的少数类召回率 Recall):")
recall_default = recall_score(y_test, rf_pred, average='macro')
recall_weighted = recall_score(y_test, rf_pred_weighted, average='macro')
print(f" 默认模型: {recall_default:.4f}")
print(f" 带权重模型: {recall_weighted:.4f}")
4、SHAP
import shap
import matplotlib.pyplot as plt
# 初始化 SHAP 解释器
explainer = shap.TreeExplainer(rf_model)
# 计算 SHAP 值(基于测试集),这个shap_values是一个numpy数组,表示每个特征对每个样本的贡献值
# 这里大家先知道这是个numpy数组即可,我们后面学习完numpy在来回头解读这个值
shap_values = explainer.shap_values(X_test) # 这个计算耗时
print(shap_values)
#print("shap_values shape:", shap_values.shape)
print("shap_values[0] shape:", shap_values[0].shape)
#print("shap_values[:, :, 0] shape:", shap_values[:, :, 0].shape)
print("X_test shape:", X_test.shape)
# --- 1. SHAP 特征重要性条形图 (Summary Plot - Bar) ---
print("--- 1. SHAP 特征重要性条形图 ---")
shap.summary_plot(shap_values[0], X_test, plot_type="bar",show=False) # 这里的show=False表示不直接显示图形,这样可以继续用plt来修改元素,不然就直接输出了
plt.title("SHAP Feature Importance (Bar Plot)")
plt.show()
# --- 2. SHAP 特征重要性蜂巢图 (Summary Plot - Violin) ---
print("--- 2. SHAP 特征重要性蜂巢图 ---")
shap.summary_plot(shap_values[0], X_test,plot_type="violin",show=False,max_display=10) # 这里的show=False表示不直接显示图形,这样可以继续用plt来修改元素,不然就直接输出了
plt.title("SHAP Feature Importance (Violin Plot)")
plt.show()
# 注意下上面几个参数,plot_type可以是bar和violin,max_display表示显示前多少个特征,默认是20个
print("--- 3. SHAP 特征重要性dependence plot ---")
shap.dependence_plot('doors',shap_values[0], X_test,show=False) # 这里的show=False表示不直接显示图形,这样可以继续用plt来修改元素,不然就直接输出了
plt.title("SHAP Feature Importance (dependence plot)")
plt.show()