0729训练模型涉及到的代码

1、sklearn.metrics.roc_curve参数

https://www.cnblogs.com/wzyuan/p/9440017.html
官方文档:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html

sklearn.metrics.roc_curve(y_true, y_score, pos_label=None, 
sample_weight=None, drop_intermediate=True)

y_true:数组,数据标签的真实值
y_score:数组,可以是对数据标签预测的概率值,置信度值或决策的非阈值度量(如SVM分类器上的‘decision_function’的返回值。)
pos_label:指定正类标签,默认1为正类。
sample_weight:采样权重,可选参数
drop_intermediate:丢掉一些次优阈值,不在ROC曲线上显示,默认为True。

2、seaborn.heatmap()参数

seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None,
 robust=False, annot=None, fmt='.2g', annotkws=None, linewidths=0, 
 linecolor='white', cbar=True, cbarkws=None, cbar_ax=None, square=False,
  ax=None, xticklabels=True, yticklabels=True, mask=None, **kwargs)

cmap:颜色
annot:默认为False,为True时会在格子上显示数字
fmt:数字格式

3、不平衡数据集的处理

SMOTEENN

from imblearn.combine import SMOTEENN
smote_enn = SMOTEENN(random_state=0)
X,y = smote_enn.fit_sample(X,y)
n_sample = y.shape[0]
n_pos_sample = y[y==0].shape[0]
n_neg_sample = y[y==1].shape[0]
print('样本个数:{};正样本占{:.2%};负样本占:{:.2%}'.format(n_sample,
n_pos_sample/n_sample,n_neg_sample/n_sample))

SMOTE

import imblearn
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=42)
X,y = sm.fit_sample(X,y)
n_sample = X.shape[0]
pd.Series(y).values_counts()  #不同值的计数 .value_counts()
n_1_sample = pd.Series(y).value_counts()[1]
n_0_sample = pd.Series(y).value_counts()[0]
print('样本个数:{};1占{:.2%};0占:{:.2%}'.format(n_sample,
n_1_sample/n_sample,n_0_sample/n_sample))

5、训练模型(以随机森林为例)

未使用交叉验证

分训练集和测试集
from sklearn.model_selection import train_test_split
Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,y,random_state=420)
不要打印输出的警告信息
import warnings
warnings.filterwarnings('ignore')

from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators=100).fit(Xtrain,Ytrain)  #实例化

from sklearn import metrics
pred = rfc.predict(Xtest)
#predict_proba() 预测测试集中样本点所归属于各个标签的概率,各个标签按词典顺序排列
y_score = rfc.predict_proba(Xtest)[:,1]

acc = metrics.accuracy_score(Ytest,pred)
precision = metrics.precision_score(Ytest,pred)
recall = metrics.recall_score(Ytest,pred)
f1 = metrics.f1_score(Ytest,pred)

auc = metrics.roc_auc_score(Ytest,y_score)

print('准确率:{.4f},精确率:{.4f},召回率:{.4f},f1-score:{:.4f},
auc:{:.4f}'.format(acc,precision,recall,f1,auc2))

使用交叉验证

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
rfc = rfc = RandomForestClassifier(n_estimators=100)

acc = cross_val_score(rfc,X,y,cv=5,scoring='accuracy').mean()
precision = cross_val_score(rfc,X,y,cv=5,scoring='average_precision').mean()
recall = cross_val_score(rfc,X,y,cv=5,scoring='recall').mean()
f1 = cross_val_score(rfc,X,y,cv=5,scoring='f1').mean()
auc = cross_val_score(rfc,X,y,cv=5,scoring='roc_auc').mean()

print('准确率:{.4f},精确率:{.4f},召回率:{.4f},f1-score:{:.4f},
auc:{:.4f}'.format(acc,precision,recall,f1,auc2))

6、混淆矩阵

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(ytest,pred,lables=[1,0])
np.set_printoptions(precision=2)
print(cm)

#可视化混淆矩阵
import seaborn as sns
import matplotlib.pyplot as plt
sns.heatmap(cm,annot=True,fmt='.2e',cmap='GnBu')
plt.show()

7、ROC曲线

from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score as AUC

#y_score是模型预测正例的概率
y_score = rfc.predict_proba(Xtest)[:,1]
#计算不同阈值下,fpr,recall(tpr)的组合值
fpr,recall,thresholds = roc_curve(ytest,y_score)
#计算auc值
area = AUC(ytest,y_score)

#绘制面积图
plt.stackplot(fpr,recall,color='steelblue',alpha=0.5,edgecolor='black')
#添加ROC曲线轮廓
plt.plot(fpr,recall,color='black',lw=1)
#添加对角线
plt.plot([0,1],[0,1],color='red',linestyle='--')
#添加文本信息
plt.text(0.5,0.3,'ROC curve (area=%0.2f)' % area)

plt.xlim([-0.05,1.05])
plt.ylim([-0.05,1.05])
#添加x轴,y轴标签
plt.xlabel('False Positive Rate')
plt.ylabel('Recall')
#显示图形
plt.show()

8、模型的评估

print(' 模型的准确率为: \n',metrics.accuracy_score(y_true,y_test_pred))
print(' 模型的评估报告: \n',metrics.classification_report(y_true,y_test_pred))

9、调参

学习曲线

scorel = []
for i in range(0,200,10):
	rfc = RandomForestClassifier(n_estimators=i+1,n_jobs=-1,random_state=90)
	score = cross_val_score(rfc,X,y,cv=5).mean()
	scorel.append(score)
print(max(scorel),(scorel.index(max(scorel))*10+1))
plt.figure(figsize=[20,5])
plt.plot(range(1,201,10),scorel)
plt.show()

#在确定好范围,进一步细化学习曲线
scorel = []
for i in range(35,45):
	rfc = RandomForestClassifier(n_estimators=i,n_jobs=-1,random_state=90)
	score = cross_val_score(rfc,X,y,cv=5).mean()
	scorel.append(score)
print(max(scorel),([*range(35,45)][scorel.index(max(scorel))]))
plt.figure(figsize=[20,5])
plt.plot(range(35,45),scorel)
plt.show()

网格搜索

from sklearn.model_selection import GridSearchCV

#预设各参数的不同选项值
max_depth = [2,3,4,5,6]
min_samples_split = [2,4,6,8]
min_samples_leaf = [2,4,8,10,12]

#将各参数以字典的形式组织起来
parameters = {'max_depth':max_depth,'min_samples_split':min_samples_split,
'min_samples_leaf',min_samples_leaf}
rfc = RandomForestClassifier(n_estimators=39,random_state=90)
GS = GridSearchCV(rfc,parameters,cv=10)
GS.fit(X,y)
GS.best_params_  #返回最佳组合的参数值
GS.best_score_ 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值