Python数据分析学习笔记(四):模型评估

各种评估指标,混淆矩阵,准确率,召回率,f1值

import numpy as np

import pandas as pd

import matplotlib

import matplotlib.pyplot as plt

%matplotlib inline

from sklearn.metrics import confusion_matrix,accuracy_score,recall_score,precision_score,f1_score,roc_curve

df = pd.read_csv('data-1.csv')

df['rf']=(df.model_RF>=0.5).astype('int')

df['lr']=(df.model_LR>=0.5).astype('int')

confusion_matrix(df.actual_label.values,df.rf)

recall_score(df.actual_label,df.rf)

precision_score(df.actual_label,df.rf)

f1_score(df.actual_label,df.rf)

roc_curve & auc值

from sklearn.metrics import roc_auc_score

auc_RF = roc_auc_score(df.actual_label.values, df.model_RF.values)

auc_LR = roc_auc_score(df.actual_label.values, df.model_LR.values)

print('AUC RF:%.3f'% auc_RF)

print('AUC LR:%.3f'% auc_LR)

import matplotlib.pyplot as plt

plt.plot(fpr_RF, tpr_RF,'r-',label = 'RF AUC: %.3f'%auc_RF)

plt.plot(fpr_LR,tpr_LR,'b-', label= 'LR AUC: %.3f'%auc_LR)

plt.plot([0,1],[0,1],'k-',label='random')

plt.plot([0,0,1,1],[0,1,1,1],'g-',label='perfect')

plt.legend()

plt.xlabel('False Positive Rate')

plt.ylabel('True Positive Rate')

plt.show()


你可能感兴趣的:(Python数据分析学习笔记(四):模型评估)