《呆板学习》 逻辑回归 大批量数据的过采样 <9>

[复制链接]
发表于 2026-2-2 12:58:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目次
一、案例文件
 二、过采样流程
1、流程图示
2、详细流程先容
1)数据切分
2)数据增强
3)创建大数据集
4)数据集切分
5)k折交织验证
6)模子训练
7)模子测试
8)最优概率阈值
三、完备代码实现
其打印结果如下:
1)过采样后的数据布局
2)肴杂矩阵可视化
3)结果的打印
四、总结


一、案例文件

同样使用上节课的银行贷款案例,其文件内容大抵如下:(共28万多条,31列)


现在要继承接着上节课的内容对模子举行优化




 二、过采样流程


1、流程图示



2、详细流程先容

1)数据切分

        将原始数据切分为70%的训练集和30%的测试集。
  1. from sklearn.model_selection import train_test_split
  2. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
复制代码

2)数据增强

        对训练会集的class=1的数据举行人工拟合,扩充为与class=0的数据量相称。
  1. # 假设augmented_data是在class=1的数据上进行增强得到的新数据
  2. # 假设class_0_data是class=0的数据
  3. # 将class=1的数据量扩充为class=0的数据量
  4. augmented_data = generate_augmented_data(class_1_data, len(class_0_data))
  5. # 人工拟合后,得到与class=0数据量相等的augmented_data
复制代码

3)创建大数据集

        将数据增强后的训练集与原测试聚集并,得到一个新的大数据集。
  1. # 将增强后的训练集与原测试集合并
  2. X_train_augmented = np.concatenate((X_train, augmented_data))
  3. y_train_augmented = np.concatenate((y_train, np.ones(len(augmented_data))))
复制代码

4)数据集切分

        将新的大数据集切分为训练集和测试集。
  1. X_train_big, X_test_big, y_train_big, y_test_big = train_test_split(X_train_augmented, y_train_augmented, test_size=0.3, random_state=0)
复制代码

5)k折交织验证

        对训练集举行k折交织验证,用于模子训练的选择和调优。
  1. from sklearn.model_selection import KFold
  2. kf = KFold(n_splits=5, shuffle=True, random_state=0)
  3. for train_index, val_index in kf.split(X_train_big):
  4.     X_train_fold, X_val_fold = X_train_big[train_index], X_train_big[val_index]
  5.     y_train_fold, y_val_fold = y_train_big[train_index], y_train_big[val_index]
  6.    
  7.     # 在每个fold上进行模型训练和选择
  8.     model = LogisticRegression()
  9.     model.fit(X_train_fold, y_train_fold)
  10.     # 在验证集上进行模型选择和调优
复制代码

6)模子训练

        创建模子并对大数据集的训练集举行训练,得到训练完成的模子。
  1. model = LogisticRegression()
  2. model.fit(X_train_big, y_train_big)
复制代码

7)模子测试

        使用之前切分出来的测试集和大数据集本身作为测试集,导入模子举行测试。

8)最优概率阈值

        根据测试结果,调解阈值以得到最优的推测概率。
  1. from sklearn.metrics import precision_score, recall_score
  2. # 假设y_pred_prob是模型预测的概率值
  3. threshold = 0.5 # 初始阈值
  4. best_threshold = threshold
  5. best_f1_score = 0
  6. for threshold in np.arange(0.1, 1.0, 0.1):
  7.     y_pred_test_threshold = (y_pred_prob > threshold).astype(int)
  8.     f1_score_test = f1_score(y_test_big, y_pred_test_threshold)
  9.    
  10.     if f1_score_test > best_f1_score:
  11.         best_f1_score = f1_score_test
  12.         best_threshold = threshold
  13. print("Best threshold:", best_threshold)
复制代码



三、完备代码实现

  1. import time
  2. import pandas as pd
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from pylab import mpl
  6. # 可视化混淆矩阵,网上都是包装好的,可以直接复制使用
  7. def cm_plot(y, yp):
  8.     from sklearn.metrics import confusion_matrix
  9.     import matplotlib.pyplot as plt
  10.     cm = confusion_matrix(y, yp)
  11.     plt.matshow(cm, cmap=plt.cm.Blues)
  12.     plt.colorbar()
  13.     for x in range(len(cm)):
  14.         for y in range(len(cm)):
  15.             plt.annotate(cm[x, y], xy=(y, x), horizontalalignment='center',verticalalignment='center')
  16.             plt.ylabel('True label')
  17.             plt.xlabel('Predicted label')
  18.     return plt
  19. data = pd.read_csv(r'./creditcard.csv')
  20. data.head()  # 默认输出前5行,这里用来提示防止忘记代码了
  21. # 设置字体,用来显示中文
  22. mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']
  23. mpl.rcParams['axes.unicode_minus'] = False
  24. # 同级class列中每个类型的数据个数,(这里的样本极度不均衡)
  25. labels_count = pd.value_counts(data['Class'])
  26. # 可视化上述分类数据的个数
  27. # plt.title("正负例样本数")
  28. # plt.xlabel('类别')
  29. # plt.ylabel('频数')
  30. # labels_count.plot(kind='bar')
  31. # plt.show()
  32. # 将Amount列的数据进行Z标准化,因为其余列的值有负值,所以不能使用0-1归一化
  33. # 导入库的用法
  34. from sklearn.preprocessing import StandardScaler
  35. # z标准化
  36. scaler = StandardScaler()  # 这是一个类,专门用来处理z标准化,适合处理大量数据的类,还有一种叫scale,适合对小部分数据进行z标准化
  37. data['Amount'] = scaler.fit_transform(data[['Amount']])  # 对Amount整列数据进行z标准化后将其传入原data内,覆盖原来的列
  38. data.head()
  39. # 删除无用的列,Time列,axis=1表示列,axis=0表示行,然后再传入data文件
  40. data = data.drop(['Time'],axis=1)
  41. from sklearn.model_selection import train_test_split
  42. x_whole = data.drop('Class',axis=1)  # 取出原始数据集特征值和标签
  43. y_whole = data.Class
  44. x_train,x_test,y_train,y_test = train_test_split(x_whole,y_whole,test_size=0.2,random_state=0)  # 对随机取出百分之20当做测试集
  45. from imblearn.over_sampling import SMOTE  # 导入过采样方法
  46. oversampler = SMOTE(random_state=0)  # 保证数据闭合效果,随机种子
  47. os_x_train,os_y_train = oversampler.fit_resample(x_train,y_train)  # 对上述取出的百分之80原始数据的训练集进行过采样
  48. labels_count = pd.value_counts(os_y_train)  # 计算过采样后的数据中各类别的数目
  49. plt.title("正负例样本数")
  50. plt.xlabel('类别')
  51. plt.ylabel('频数')
  52. labels_count.plot(kind='bar')
  53. plt.show()
  54. os_x_train_w,os_x_test_w,os_y_train_w,os_y_test_w = (   # 对过采样的数随机取出20%当做测试集,80%当做训练集
  55.     train_test_split(os_x_train,os_y_train,test_size=0.2,random_state=0))
  56. from sklearn.linear_model import LogisticRegression  # 导入逻辑回归模型
  57. from sklearn.model_selection import cross_val_score  # k折交叉验证
  58. scores = []  # 定义一个空列表,用来存放后面更改正则化强度后的每个C值计算结果对应的score值
  59. c_param_range = [0.01,0.1,1,10,100] # 循环更改的C值
  60. z = 1
  61. for i in c_param_range:
  62.     start_time = time.time()  # 开始时间
  63.     lr = LogisticRegression(C=i,penalty='l2',solver='lbfgs',max_iter=1000)  # 循环遍历C值
  64.     score = cross_val_score(lr,os_x_train_w,os_y_train_w,cv=5,scoring='recall')  # k折运算
  65.     score_mean = sum(score)/len(score)
  66.     scores.append(score_mean)
  67.     end_time = time.time()   # 结束时间
  68.     print("第{}次。。。".format(z))
  69.     print("time spend:{:.2f}".format(end_time-start_time))
  70.     print("recall:{}".format(score_mean))
  71.     z+=1
  72.     # print(score_mean)
  73. best_c = c_param_range[np.argmax(scores)]  # 最优C值
  74. lr = LogisticRegression(C=best_c,penalty='l2',max_iter=1000)
  75. lr.fit(os_x_train_w,os_y_train_w)   # 对过采样随机取出的训练集数据进行训练
  76. from sklearn import metrics   # 打印分类报告
  77. train_predict_big = lr.predict(os_x_train_w)  # 将训练集自己导入模型进行测试
  78. print(metrics.classification_report(os_y_train_w,train_predict_big))
  79. test_predict_big = lr.predict(os_x_test_w)   # 对测试集进行测试
  80. print(metrics.classification_report(os_y_test_w,test_predict_big))
  81. train_predict = lr.predict(x_train)  # 对原始数据训练集进行测试
  82. print(metrics.classification_report(y_train,train_predict))
  83. cm_plot(y_train,train_predict).show()
  84. test_predict = lr.predict(x_test)  # 对原始数据测试集进行测试
  85. print(metrics.classification_report(y_test,test_predict))
  86. cm_plot(y_test,test_predict).show()
  87. thresholds = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]   # 此处同样更改阈值
  88. recalls = []
  89. for i in thresholds:
  90.     y_predict_proba = lr.predict_proba(x_test)
  91.     y_predict_proba = pd.DataFrame(y_predict_proba)
  92.     y_predict_proba = y_predict_proba.drop([0],axis=1)
  93.     y_predict_proba[y_predict_proba[[1]] > i] = 1
  94.     y_predict_proba[y_predict_proba[[1]] <= i] =0
  95.     recall = metrics.recall_score(y_test,y_predict_proba[1])
  96.     recalls.append(recall)
  97.     print("{}Recall metric in the testing dataset:{:.3f}".format(i,recall))
复制代码
其打印结果如下:

1)过采样后的数据布局


2)肴杂矩阵可视化


3)结果的打印



四、总结

        大批量数据的过采样是一种数据预处置处罚技能,用于办理数据不均衡标题。在数据会集,某些种别的样本数目显着少于其他种别,这会导致模子在训练时对少数种别的推测本领较差。过采样的目标是通过增长少数种别的样本数目,使得各个种别之间的样本数目更加均衡。
大批量数据的过采样可以通过以下步调实现:

  • 选择过采样方法:常见的过采样方法包罗随机复制、SMOTE(合成少数类过采样技能)、ADASYN(自顺应合成过采样技能)等。每种方法有差异的原理和实用场景,选择恰当标题标方法是关键。
  • 实行过采样:根据选择的过采样方法,对少数种别的样本举行复制或天生新样本。复制样本可以直接复制原始样本,天生样本则是根据少数种别样本的特性合成新样本。
  • 调解过采样比例:过采样大概会导致样本数目过多,导致模子训练时间增长大概太过拟合。可以通过控制过采样比例来均衡样本数目,确保各个种别之间的样本数目靠近而不外于失衡。


回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表