上文https://blog.csdn.net/2201_75573294/article/details/155826612?fromshareblogdetailsharetypeblogdetailsharerId155826612sharereferPCsharesource2201_75573294sharefromfrom_link上文我们讲到参数ClrLogisticRegression(C0.01)其实这里的参数不仅仅有还有很多参数1.其他参数Penalty正则化的方式上述我们讲逻辑函数的说过有两种L1和L2两种。使用L2时只有三种优化算法这里的优化是指优化逻辑回归支持L2lbfgsnewton-cgsag很多参数都是默认的所以平时使用的时候并不需要特意去写其中我们的C1/也就是上文损失函数中的所以C越大惩罚力度越小C越小惩罚力度越大。还有一个max_iter算法收敛最大迭代次数默认是100其实就是欠拟合的时候就设置大一点2.C值的选择平时我们写代码设定另一个C值运行代码并输出预测集的召回率然后在设置一个C值运行代码输出预测集召回率……多次这样去找到一个优化最好的C值想法是正确的但是用测试集一点一点的推测c值这就相当于把测试集当训练集了这样是不行的测试集只能用来最后测试结果是一锤定音的所以这时候我们需要一个验证集验证集可以来自于训练集的一部分用验证机去反复验证哪个c值更优所以代码中我们会用到循环语句。3.k折交叉验证如果训练集拿出一部分作为验证集这样我们训练集和之前比就会变少是否会导致训练不到位?所以在验证c最优c值的时候我们采用k折交叉验证k取值多少我们就把原来的训练集分为几份c取某个值的时候每一份依次做验证集这样就保证了分出去当验证集的数据也能当训练集训练模型分成几份就会得到几个结果把结果求平均值就可以作为该c值下的结果去多个c值把每一次c值得到的结果进行比较选出最优c值4.找出最优c值提高召回率的实现import pandas as pd import numpy as np from sklearn.preprocessing import scale from sklearn.model_selection import train_test_split#专门用来对数据集进行切分的函数 from sklearn.linear_model import LogisticRegression#逻辑回归的类所有的算法都封装再这个类里面 from sklearn import metrics from sklearn.model_selection import cross_val_score 绘制混淆矩阵#混淆矩阵的实现可以用大模型搜索不做讲述 def cm_plot(y, yp): from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt 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), horizontalalignmentcenter, verticalalignment center) plt.ylabel(True label) plt.xlabel(Predicted label) return plt 数据预处理 #把没用的信息删去保留矩阵在data中 datapd.read_csv(rE:\filedata\creditcard.csv) data[Amount]pd.DataFrame(scale(data[Amount]))#把Amount列也进行标准化 datadata.drop([Time],axis1)#用不到的Time列删去 训练集使用采样数据测试举使用原始数据集进行预测 X_datadata.drop(Class,axis1)#前面我们已经处理过数据了 Y_datadata.Class x_train, x_test, y_train, y_test train_test_split(X_data, Y_data, test_size0.3, random_state1000)#随机种子 scores []#不同的c参数在验证集下的评分 c_s [0.01,0.1,1,10,100]#参数 for i in c_s: lr LogisticRegression(C i, penalty l2, solverlbfgs, max_iter1000) #这里的cv8就是k8,分八份 score cross_val_score(lr,x_train, y_train, cv8,scoringrecall)#交叉验证 # scoring:可选accuracy(精度)、recall(召回率)、roc_auc(roc值)、neg_mean_squarel score_meansum(score)/len(score)#求平均值 scores.append(score_mean)#将不同的c参数的结果添加到scores列表中 print(score_mean)#输出该值 best_c c_s[np.argmax(scores)]#选出最优值 print(最优C值{}.format(best_c))#输出最优c值 lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000)#定义逻辑回归 lr.fit(x_train,y_train)#进行模型训练 train_prelr.predict(x_train)#训练集自测 print(metrics.classification_report(y_train,train_pre)) cm_plot(y_train,train_pre).show() testprelr.predict(x_test)#预测集预测 completelr.score(x_test,y_test) print(metrics.classification_report(y_test,testpre,digits6)) cm_plot(y_test,testpre).show()和上篇博客中代码用同样的数据但是更改了c值并找到了最优c值训练召回率和预测召回率都提升了。交叉验证不仅能用于逻辑回归有不确定值都可以使用交叉验证4.调整阈值增加召回率除了找到最优c值能增加我们的召回率我们也可以改变阈值进行本来值为0.3……被归为0类的在改变阈值后就属于1类了这也算是强制实施了“宁可错杀也不放过”的原则import pandas as pd import numpy as np from sklearn.preprocessing import scale from sklearn.model_selection import train_test_split#专门用来对数据集进行切分的函数 from sklearn.linear_model import LogisticRegression#逻辑回归的类所有的算法都封装再这个类里面 from sklearn import metrics from sklearn.model_selection import cross_val_score def cm_plot(y, yp):#混淆矩阵的绘制 from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt 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), horizontalalignmentcenter, verticalalignment center) plt.ylabel(True label) plt.xlabel(Predicted label) return plt datapd.read_csv(rE:\filedata\creditcard.csv) data[Amount]pd.DataFrame(scale(data[Amount]))#把Amount列也进行标准化 datadata.drop([Time],axis1)#用不到的Time列删去 X_datadata.drop(Class,axis1) Y_datadata.Class x_train, x_test, y_train, y_test train_test_split\ (X_data, Y_data, test_size0.3, random_state1000)#随机种子 scores []#不同的c参数在验证集下的评分 c_s [0.01,0.1,1,10,100]#参数 for i in c_s:#第1词循环的时候C0.015个逻辑回归模型 lr LogisticRegression(C i, penalty l2, solverlbfgs, max_iter1000) score cross_val_score(lr,x_train, y_train, cv8,scoringrecall)#交叉验证 # scoring:可选accuracy(精度)、recall(召回率)、roc_auc(roc值)、neg_mean_squarel score_meansum(score)/len(score)#交又验证后的值召回率#里面保存了所有的交叉验证召回率 scores.append(score_mean)#将不同的c参数分别传入模型分别看看哪个模型效果更好我们选c print(score_mean) best_c c_s[np.argmax(scores)] print(最优C值{}.format(best_c)) lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000) lr.fit(x_train,y_train) 修改逻辑回归中的阈值#先训练在进行修改阈值操作 thresholds[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9] recalls[] for i in thresholds: y_prelr.predict_proba(x_test) y_prepd.DataFrame(y_pre) y_prey_pre.drop([0],axis1) y_pre[y_pre[[1]]i]1#预测的概率大于i0.10.2预测的标签设置为1 y_pre[y_pre[[1]]i]0#当预测的概率小于等于i预测标签设置0 recallmetrics.recall_score(y_test,y_pre[1]) recalls.append(recall) print({} recall metric in the testing dataset:{:.3f}.format(i,recall)) #训练集自测 train_prelr.predict(x_train) print(metrics.classification_report(y_train,train_pre)) cm_plot(y_train,train_pre).show() #测试集预测 testprelr.predict(x_test) completelr.score(x_test,y_test) print(metrics.classification_report(y_test,testpre,digits6)) cm_plot(y_test,testpre).show()从结果也可以看出这样也能提高召回率5.将不均衡的数据处理均衡增加召回率1下采样import pandas as pd import numpy as np from sklearn.preprocessing import scale from sklearn.model_selection import train_test_split#专门用来对数据集进行切分的函数 from sklearn.linear_model import LogisticRegression#逻辑回归的类所有的算法都封装再这个类里面 from sklearn import metrics from sklearn.model_selection import cross_val_score def cm_plot(y, yp): from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt 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), horizontalalignmentcenter, verticalalignment center) plt.ylabel(True label) plt.xlabel(Predicted label) return plt datapd.read_csv(rE:\filedata\creditcard.csv) data[Amount]pd.DataFrame(scale(data[Amount]))#把Amount列也进行标准化 datadata.drop([Time],axis1)#用不到的Time列删去 value_countpd.value_counts(data[Class])#统计各类别的个数 print(value_count,type(value_count))#输出各类别个数 X_datadata.drop(Class,axis1) Y_datadata.Class x_train, x_test, y_train, y_test train_test_split\ (X_data, Y_data, test_size0.3, random_state1000)#随机种子 x_train[Class]y_train#将训练集加上class列 data_train1x_train把加上class列的数据集定义为训练集 datanumber0data_train1[data_train1[Class]0]#抽取所有类别为0的数据 datanumber1data_train1[data_train1[Class]1]#抽取所有类型为1的数据 datanumber0datanumber0.sample(len(datanumber1))#注意这里应该是类别0类别1 #因为类别数据少如果是类别1类别0那么将会报错显示数据集不够 data_train2pd.concat([datanumber0,datanumber1]) #将抽取出条数相等的0类和1类组合作为我们的训练集 Xdata_train2.drop(Class,axis1)#对处理好的数据集进行数据和类别的划分 Ydata_train2.Class scores []#加上交叉验证 c_s [0.01,0.1,1,10,100]#参数 for i in c_s: lr LogisticRegression(C i, penalty l2, solverlbfgs, max_iter1000) score cross_val_score(lr,X, Y, cv5,scoringrecall)#交叉验证 score_meansum(score)/len(score) scores.append(score_mean) print(score_mean) best_c c_s[np.argmax(scores)] print(最优C值{}.format(best_c)) lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000) lr.fit(X,Y) train_prelr.predict(X) print(metrics.classification_report(Y,train_pre)) cm_plot(Y,train_pre).show() testprelr.predict(x_test) completelr.score(x_test,y_test) print(metrics.classification_report(y_test,testpre,digits6)) cm_plot(y_test,testpre).show()很明显我们的召回率大幅度提升2过采样如何拟合数据我们需要用到一个函数smote类拟合数据无非就是在同一类中挑选俩个数据取中间的一些值就像我们knn算法中相同一类在一起那我们在同一类的内部拟合数据自然也就是这类的拟合数据就是这样一个过程。在使用之前我们需要下载一个第三方库imblearn然后调用smote类调用前先初始化一个对象import pandas as pd import numpy as np from sklearn.preprocessing import scale from sklearn.model_selection import train_test_split#专门用来对数据集进行切分的函数 from sklearn.linear_model import LogisticRegression#逻辑回归的类所有的算法都封装再这个类里面 from sklearn import metrics from sklearn.model_selection import cross_val_score from imblearn.over_sampling import SMOTE#第三方库imblearn def cm_plot(y, yp): from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt 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), horizontalalignmentcenter, verticalalignment center) plt.ylabel(True label) plt.xlabel(Predicted label) return plt datapd.read_csv(rE:\filedata\creditcard.csv) data[Amount]pd.DataFrame(scale(data[Amount]))#把Amount列也进行标准化 datadata.drop([Time],axis1)#用不到的Time列删去 X_datadata.drop(Class,axis1) Y_datadata.Class x_train, x_test, y_train, y_test train_test_split\ (X_data, Y_data, test_size0.3, random_state1000)#随机种子 oversamplerSMOTE(random_state0)#初始化smote对象 X,Yoversampler.fit_resample(x_train,y_train) #fit_resample会对数据中数据少的类型自动识别这里就不需要分别挑出1和0类了 #对原来的训练集特征和标签也就是这里的x_train,y_train进行识别拟合数据返回新的特征和标签 #这里也不需要组合起来了因为没有分开类1和类0这些数据始终都是在一起的 #注意这里拟合过整个训练集数据条数增加了一倍数据多训练自然就不会因为数据过少而欠拟合 #但是同时我们运行速度也会变慢 scores [] c_s [0.01,0.1,1,10,100,1000]#参数 for i in c_s: lr LogisticRegression(C i, penalty l2, solverlbfgs, max_iter1000) score cross_val_score(lr,X, Y, cv5,scoringrecall)#交叉验证 score_meansum(score)/len(score) scores.append(score_mean) print(score_mean) best_c c_s[np.argmax(scores)] print(最优C值{}.format(best_c)) lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000) lr.fit(X,Y) train_prelr.predict(X) print(metrics.classification_report(Y,train_pre)) cm_plot(Y,train_pre).show() testprelr.predict(x_test) completelr.score(x_test,y_test) print(metrics.classification_report(y_test,testpre,digits6)) cm_plot(y_test,testpre).show()过采样也能把召回率提升到零点九多
机器学习算法之逻辑回归下
上文https://blog.csdn.net/2201_75573294/article/details/155826612?fromshareblogdetailsharetypeblogdetailsharerId155826612sharereferPCsharesource2201_75573294sharefromfrom_link上文我们讲到参数ClrLogisticRegression(C0.01)其实这里的参数不仅仅有还有很多参数1.其他参数Penalty正则化的方式上述我们讲逻辑函数的说过有两种L1和L2两种。使用L2时只有三种优化算法这里的优化是指优化逻辑回归支持L2lbfgsnewton-cgsag很多参数都是默认的所以平时使用的时候并不需要特意去写其中我们的C1/也就是上文损失函数中的所以C越大惩罚力度越小C越小惩罚力度越大。还有一个max_iter算法收敛最大迭代次数默认是100其实就是欠拟合的时候就设置大一点2.C值的选择平时我们写代码设定另一个C值运行代码并输出预测集的召回率然后在设置一个C值运行代码输出预测集召回率……多次这样去找到一个优化最好的C值想法是正确的但是用测试集一点一点的推测c值这就相当于把测试集当训练集了这样是不行的测试集只能用来最后测试结果是一锤定音的所以这时候我们需要一个验证集验证集可以来自于训练集的一部分用验证机去反复验证哪个c值更优所以代码中我们会用到循环语句。3.k折交叉验证如果训练集拿出一部分作为验证集这样我们训练集和之前比就会变少是否会导致训练不到位?所以在验证c最优c值的时候我们采用k折交叉验证k取值多少我们就把原来的训练集分为几份c取某个值的时候每一份依次做验证集这样就保证了分出去当验证集的数据也能当训练集训练模型分成几份就会得到几个结果把结果求平均值就可以作为该c值下的结果去多个c值把每一次c值得到的结果进行比较选出最优c值4.找出最优c值提高召回率的实现import pandas as pd import numpy as np from sklearn.preprocessing import scale from sklearn.model_selection import train_test_split#专门用来对数据集进行切分的函数 from sklearn.linear_model import LogisticRegression#逻辑回归的类所有的算法都封装再这个类里面 from sklearn import metrics from sklearn.model_selection import cross_val_score 绘制混淆矩阵#混淆矩阵的实现可以用大模型搜索不做讲述 def cm_plot(y, yp): from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt 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), horizontalalignmentcenter, verticalalignment center) plt.ylabel(True label) plt.xlabel(Predicted label) return plt 数据预处理 #把没用的信息删去保留矩阵在data中 datapd.read_csv(rE:\filedata\creditcard.csv) data[Amount]pd.DataFrame(scale(data[Amount]))#把Amount列也进行标准化 datadata.drop([Time],axis1)#用不到的Time列删去 训练集使用采样数据测试举使用原始数据集进行预测 X_datadata.drop(Class,axis1)#前面我们已经处理过数据了 Y_datadata.Class x_train, x_test, y_train, y_test train_test_split(X_data, Y_data, test_size0.3, random_state1000)#随机种子 scores []#不同的c参数在验证集下的评分 c_s [0.01,0.1,1,10,100]#参数 for i in c_s: lr LogisticRegression(C i, penalty l2, solverlbfgs, max_iter1000) #这里的cv8就是k8,分八份 score cross_val_score(lr,x_train, y_train, cv8,scoringrecall)#交叉验证 # scoring:可选accuracy(精度)、recall(召回率)、roc_auc(roc值)、neg_mean_squarel score_meansum(score)/len(score)#求平均值 scores.append(score_mean)#将不同的c参数的结果添加到scores列表中 print(score_mean)#输出该值 best_c c_s[np.argmax(scores)]#选出最优值 print(最优C值{}.format(best_c))#输出最优c值 lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000)#定义逻辑回归 lr.fit(x_train,y_train)#进行模型训练 train_prelr.predict(x_train)#训练集自测 print(metrics.classification_report(y_train,train_pre)) cm_plot(y_train,train_pre).show() testprelr.predict(x_test)#预测集预测 completelr.score(x_test,y_test) print(metrics.classification_report(y_test,testpre,digits6)) cm_plot(y_test,testpre).show()和上篇博客中代码用同样的数据但是更改了c值并找到了最优c值训练召回率和预测召回率都提升了。交叉验证不仅能用于逻辑回归有不确定值都可以使用交叉验证4.调整阈值增加召回率除了找到最优c值能增加我们的召回率我们也可以改变阈值进行本来值为0.3……被归为0类的在改变阈值后就属于1类了这也算是强制实施了“宁可错杀也不放过”的原则import pandas as pd import numpy as np from sklearn.preprocessing import scale from sklearn.model_selection import train_test_split#专门用来对数据集进行切分的函数 from sklearn.linear_model import LogisticRegression#逻辑回归的类所有的算法都封装再这个类里面 from sklearn import metrics from sklearn.model_selection import cross_val_score def cm_plot(y, yp):#混淆矩阵的绘制 from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt 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), horizontalalignmentcenter, verticalalignment center) plt.ylabel(True label) plt.xlabel(Predicted label) return plt datapd.read_csv(rE:\filedata\creditcard.csv) data[Amount]pd.DataFrame(scale(data[Amount]))#把Amount列也进行标准化 datadata.drop([Time],axis1)#用不到的Time列删去 X_datadata.drop(Class,axis1) Y_datadata.Class x_train, x_test, y_train, y_test train_test_split\ (X_data, Y_data, test_size0.3, random_state1000)#随机种子 scores []#不同的c参数在验证集下的评分 c_s [0.01,0.1,1,10,100]#参数 for i in c_s:#第1词循环的时候C0.015个逻辑回归模型 lr LogisticRegression(C i, penalty l2, solverlbfgs, max_iter1000) score cross_val_score(lr,x_train, y_train, cv8,scoringrecall)#交叉验证 # scoring:可选accuracy(精度)、recall(召回率)、roc_auc(roc值)、neg_mean_squarel score_meansum(score)/len(score)#交又验证后的值召回率#里面保存了所有的交叉验证召回率 scores.append(score_mean)#将不同的c参数分别传入模型分别看看哪个模型效果更好我们选c print(score_mean) best_c c_s[np.argmax(scores)] print(最优C值{}.format(best_c)) lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000) lr.fit(x_train,y_train) 修改逻辑回归中的阈值#先训练在进行修改阈值操作 thresholds[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9] recalls[] for i in thresholds: y_prelr.predict_proba(x_test) y_prepd.DataFrame(y_pre) y_prey_pre.drop([0],axis1) y_pre[y_pre[[1]]i]1#预测的概率大于i0.10.2预测的标签设置为1 y_pre[y_pre[[1]]i]0#当预测的概率小于等于i预测标签设置0 recallmetrics.recall_score(y_test,y_pre[1]) recalls.append(recall) print({} recall metric in the testing dataset:{:.3f}.format(i,recall)) #训练集自测 train_prelr.predict(x_train) print(metrics.classification_report(y_train,train_pre)) cm_plot(y_train,train_pre).show() #测试集预测 testprelr.predict(x_test) completelr.score(x_test,y_test) print(metrics.classification_report(y_test,testpre,digits6)) cm_plot(y_test,testpre).show()从结果也可以看出这样也能提高召回率5.将不均衡的数据处理均衡增加召回率1下采样import pandas as pd import numpy as np from sklearn.preprocessing import scale from sklearn.model_selection import train_test_split#专门用来对数据集进行切分的函数 from sklearn.linear_model import LogisticRegression#逻辑回归的类所有的算法都封装再这个类里面 from sklearn import metrics from sklearn.model_selection import cross_val_score def cm_plot(y, yp): from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt 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), horizontalalignmentcenter, verticalalignment center) plt.ylabel(True label) plt.xlabel(Predicted label) return plt datapd.read_csv(rE:\filedata\creditcard.csv) data[Amount]pd.DataFrame(scale(data[Amount]))#把Amount列也进行标准化 datadata.drop([Time],axis1)#用不到的Time列删去 value_countpd.value_counts(data[Class])#统计各类别的个数 print(value_count,type(value_count))#输出各类别个数 X_datadata.drop(Class,axis1) Y_datadata.Class x_train, x_test, y_train, y_test train_test_split\ (X_data, Y_data, test_size0.3, random_state1000)#随机种子 x_train[Class]y_train#将训练集加上class列 data_train1x_train把加上class列的数据集定义为训练集 datanumber0data_train1[data_train1[Class]0]#抽取所有类别为0的数据 datanumber1data_train1[data_train1[Class]1]#抽取所有类型为1的数据 datanumber0datanumber0.sample(len(datanumber1))#注意这里应该是类别0类别1 #因为类别数据少如果是类别1类别0那么将会报错显示数据集不够 data_train2pd.concat([datanumber0,datanumber1]) #将抽取出条数相等的0类和1类组合作为我们的训练集 Xdata_train2.drop(Class,axis1)#对处理好的数据集进行数据和类别的划分 Ydata_train2.Class scores []#加上交叉验证 c_s [0.01,0.1,1,10,100]#参数 for i in c_s: lr LogisticRegression(C i, penalty l2, solverlbfgs, max_iter1000) score cross_val_score(lr,X, Y, cv5,scoringrecall)#交叉验证 score_meansum(score)/len(score) scores.append(score_mean) print(score_mean) best_c c_s[np.argmax(scores)] print(最优C值{}.format(best_c)) lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000) lr.fit(X,Y) train_prelr.predict(X) print(metrics.classification_report(Y,train_pre)) cm_plot(Y,train_pre).show() testprelr.predict(x_test) completelr.score(x_test,y_test) print(metrics.classification_report(y_test,testpre,digits6)) cm_plot(y_test,testpre).show()很明显我们的召回率大幅度提升2过采样如何拟合数据我们需要用到一个函数smote类拟合数据无非就是在同一类中挑选俩个数据取中间的一些值就像我们knn算法中相同一类在一起那我们在同一类的内部拟合数据自然也就是这类的拟合数据就是这样一个过程。在使用之前我们需要下载一个第三方库imblearn然后调用smote类调用前先初始化一个对象import pandas as pd import numpy as np from sklearn.preprocessing import scale from sklearn.model_selection import train_test_split#专门用来对数据集进行切分的函数 from sklearn.linear_model import LogisticRegression#逻辑回归的类所有的算法都封装再这个类里面 from sklearn import metrics from sklearn.model_selection import cross_val_score from imblearn.over_sampling import SMOTE#第三方库imblearn def cm_plot(y, yp): from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt 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), horizontalalignmentcenter, verticalalignment center) plt.ylabel(True label) plt.xlabel(Predicted label) return plt datapd.read_csv(rE:\filedata\creditcard.csv) data[Amount]pd.DataFrame(scale(data[Amount]))#把Amount列也进行标准化 datadata.drop([Time],axis1)#用不到的Time列删去 X_datadata.drop(Class,axis1) Y_datadata.Class x_train, x_test, y_train, y_test train_test_split\ (X_data, Y_data, test_size0.3, random_state1000)#随机种子 oversamplerSMOTE(random_state0)#初始化smote对象 X,Yoversampler.fit_resample(x_train,y_train) #fit_resample会对数据中数据少的类型自动识别这里就不需要分别挑出1和0类了 #对原来的训练集特征和标签也就是这里的x_train,y_train进行识别拟合数据返回新的特征和标签 #这里也不需要组合起来了因为没有分开类1和类0这些数据始终都是在一起的 #注意这里拟合过整个训练集数据条数增加了一倍数据多训练自然就不会因为数据过少而欠拟合 #但是同时我们运行速度也会变慢 scores [] c_s [0.01,0.1,1,10,100,1000]#参数 for i in c_s: lr LogisticRegression(C i, penalty l2, solverlbfgs, max_iter1000) score cross_val_score(lr,X, Y, cv5,scoringrecall)#交叉验证 score_meansum(score)/len(score) scores.append(score_mean) print(score_mean) best_c c_s[np.argmax(scores)] print(最优C值{}.format(best_c)) lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000) lr.fit(X,Y) train_prelr.predict(X) print(metrics.classification_report(Y,train_pre)) cm_plot(Y,train_pre).show() testprelr.predict(x_test) completelr.score(x_test,y_test) print(metrics.classification_report(y_test,testpre,digits6)) cm_plot(y_test,testpre).show()过采样也能把召回率提升到零点九多