机器学习:交叉验证、下采样、过采样(银行贷款案例)

发布时间:2026/7/31 4:31:06

机器学习:交叉验证、下采样、过采样(银行贷款案例) 一、前言今天系统学习了逻辑回归在金融风控中的实战应用核心解决银行贷款预测中最经典的问题数据集类别不平衡 模型评估不准。在银行贷款数据中绝大多数用户正常还款负样本/多数类极少数用户违约坏账正样本/少数类如果直接训练模型逻辑回归会“摆烂”直接预测全部为不违约准确率很高、但是完全没有业务价值。因此今天重点掌握了三个核心工具下采样对多数样本做减法过采样SMOTE对少数样本做加法交叉验证稳定评估模型、选择最优参数二、业务场景为什么贷款数据必须做采样1. 类别不平衡问题银行贷款是极度不平衡二分类任务。逻辑回归的损失函数是全局平均损失样本数量多的类别会主导训练过程导致模型严重偏向多数类预测不违约违约样本识别率极低召回率差看似准确率很高实际风控完全失效2. 普通划分数据集的缺陷单纯 train_test_split 一次划分结果随机性大无法代表模型真实水平所以必须引入K 折交叉验证。三、三大核心技术原理1. 交叉验证K 折 CV核心作用避免单次数据集划分的偶然性稳定评估模型泛化能力自动筛选逻辑回归最优正则参数 C有效检测过拟合2. 下采样欠采样—— 减少多数类样本原理随机抽取部分正常用户样本让正负样本比例 1:1。优点训练速度快、彻底平衡样本、解决模型偏置。缺点丢失大量正常客户信息容易欠拟合。适用大数据量场景。3. 过采样SMOTE—— 扩增少数类样本原理不是简单复制违约样本而是根据近邻特征合成全新违约样本。优点不丢失任何原始数据、缓解过拟合、提升少数类识别能力。缺点极少数样本时容易生成虚假数据。适用金融风控主流首选方案。四、注意严禁全局采样正确流程先划分训练集、测试集只对训练集做下采样/过采样测试集保持原始真实分布原因测试集必须模拟真实业务场景不能人为平衡否则评估结果虚高、上线翻车。五、完整代码下采样 交叉验证import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split, cross_val_score from sklearn.linear_model import LogisticRegression from sklearn.metrics import confusion_matrix, classification_report 绘制混淆矩阵 def cm_plot(y, yp): cm confusion_matrix(y, yp) plt.matshow(cm, cmapplt.cm.Blues) plt.colorbar() for x in range(len(cm)): for y in range(len(cm)): plt.annotate(cm[x, y], xy(y, x), hacenter, vacenter) plt.ylabel(True label) plt.xlabel(Predicted label) return plt 1. 读取数据与预处理 data pd.read_csv(r./creditcard.csv) scaler StandardScaler() data[Amount] scaler.fit_transform(data[[Amount]]) data data.drop([Time], axis1) 2. 划分训练集、测试集 X data.drop(Class, axis1) y data[Class] x_train, x_test, y_train, y_test train_test_split(X, y, test_size0.2, random_state0) 3. 下采样平衡训练集 train_data x_train.copy() train_data[Class] y_train major train_data[train_data[Class] 0] minor train_data[train_data[Class] 1] major_down major.sample(len(minor), random_state0) train_bal pd.concat([major_down, minor]) x_train_bal train_bal.drop(Class, axis1) y_train_bal train_bal[Class] 4. 交叉验证选最优 C c_list [0.01, 0.1, 1, 10, 100] scores [] for c in c_list: lr LogisticRegression(Cc, max_iter1000) res cross_val_score(lr, x_train_bal, y_train_bal, cv8, scoringrecall) scores.append(res.mean()) print(fC{c}, 召回率均值: {res.mean():.4f}) best_c c_list[np.argmax(scores)] print(最优惩罚因子 C:, best_c) 5. 训练模型 lr_final LogisticRegression(Cbest_c, max_iter1000) lr_final.fit(x_train_bal, y_train_bal) 评估 train_pred lr_final.predict(x_train_bal) print(训练集报告) print(classification_report(y_train_bal, train_pred)) cm_plot(y_train_bal, train_pred).show() test_pred lr_final.predict(x_test) print(测试集报告) print(classification_report(y_test, test_pred, digits6)) cm_plot(y_test, test_pred).show()六、完整代码过采样 SMOTE 交叉验证import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split, cross_val_score from sklearn.linear_model import LogisticRegression from sklearn.metrics import confusion_matrix, classification_report from imblearn.over_sampling import SMOTE def cm_plot(y, yp): cm confusion_matrix(y, yp) plt.matshow(cm, cmapplt.cm.Blues) plt.colorbar() for x in range(len(cm)): for y in range(len(cm)): plt.annotate(cm[x, y], xy(y, x), hacenter, vacenter) plt.ylabel(True label) plt.xlabel(Predicted label) return plt 数据预处理 data pd.read_csv(r./creditcard.csv) scaler StandardScaler() data[Amount] scaler.fit_transform(data[[Amount]]) data data.drop([Time], axis1) X data.drop(Class, axis1) y data[Class] x_train, x_test, y_train, y_test train_test_split(X, y, test_size0.2, random_state0) SMOTE 过采样 smote SMOTE(random_state0) os_x_train, os_y_train smote.fit_resample(x_train, y_train) 交叉验证调参 c_list [0.01, 0.1, 1, 10, 100] scores [] for c in c_list: lr LogisticRegression(Cc, max_iter1000) res cross_val_score(lr, os_x_train, os_y_train, cv8, scoringrecall) scores.append(res.mean()) print(fC{c}, 召回率均值: {res.mean():.4f}) best_c c_list[np.argmax(scores)] print(最优惩罚因子 C:, best_c) 训练评估 lr_final LogisticRegression(Cbest_c, max_iter1000) lr_final.fit(os_x_train, os_y_train) train_pred lr_final.predict(os_x_train) print(过采样训练集报告) print(classification_report(os_y_train, train_pred)) cm_plot(os_y_train, train_pred).show() test_pred lr_final.predict(x_test) print(原始测试集报告) print(classification_report(y_test, test_pred, digits6)) cm_plot(y_test, test_pred).show()七、下采样 VS 过采样 对比方法核心思路优点缺点贷款场景推荐下采样删减多数类训练快、分布均衡丢失样本信息、易欠拟合大数据量可用SMOTE 过采样合成少数类不丢数据、效果更好少量样本易造伪数据金融场景首选交叉验证多折训练取平均评估稳定、精准调参计算量增大必用八、今日学习总结银行贷款预测是典型的不平衡二分类问题直接建模只会追求高准确率毫无业务意义。下采样通过删减多数样本平衡数据过采样 SMOTE通过合成少数样本优化模型。交叉验证解决模型评估不稳定、参数选择盲目问题是建模的标准流程。工业级最重要准则先划分数据集、再训练集采样、测试集绝对保真杜绝数据泄露。

相关新闻