模型評估 import pickle from matplotlib import pyplot as plt from sklearn.externals import joblib from sklearn.metrics import accuracy_score, recall_score, f1_score, roc_auc_score, roc_curve
path = "E:/mypython/Machine_learning_GoGoGo/" """===================================================================================================================== 1 讀取特徵 """ print("0 讀取特徵") f = open(path + 'feature/feature_V1.pkl', 'rb') train, test, y_train,y_test= pickle.load(f) f.close()
"""===================================================================================================================== 2 讀取模型 """ print("1 讀取模型") SVM_linear = joblib.load( path + "model/SVM_linear.pkl")
SVM_rbf = joblib.load( path + "model/SVM_rbf.pkl") SVM_sigmoid = joblib.load( path + "model/SVM_sigmoid.pkl") lg_120 = joblib.load( path + "model/lg_120.pkl") DT = joblib.load( path + "model/DT.pkl") xgb_sklearn = joblib.load( path + "model/xgb_sklearn.pkl") lgb_sklearn = joblib.load( path + "model/lgb_sklearn.pkl") xgb = joblib.load( path + "model/xgb.pkl") lgb = joblib.load( path + "model/lgb.pkl")
"""===================================================================================================================== 3 模型評估 """
def model_evalua(clf, X_train, X_test, y_train, y_test,name): y_train_pred = clf.predict(X_train) y_test_pred = clf.predict(X_test) y_train_pred_proba = clf.predict_proba(X_train)[:, 1] y_test_pred_proba = clf.predict_proba(X_test)[:, 1] """【AUC Score】""" print(name+'_AUC Score') print(name+"_Train_AUC Score :{:.4f}".format(roc_auc_score(y_train, y_train_pred))) print(name+"_Test_AUC Score :{:.4f}".format(roc_auc_score(y_test, y_test_pred)))
"""【準確性】""" print(name+'_準確性:') print(name+'_Train_準確性:{:.4f}'.format(accuracy_score(y_train, y_train_pred))) print(name+'_Test_準確性:{:.4f}'.format(accuracy_score(y_test, y_test_pred)))
"""【召回率】""" print(name+'_召回率:') print(name+'_Train_召回率:{:.4f}'.format(recall_score(y_train, y_train_pred))) print(name+'_Test_召回率:{:.4f}'.format(recall_score(y_test, y_test_pred)))
"""【f1_score】""" print(name+'_f1_score:') print(name+'_Train_f1_score:{:.4f}'.format(f1_score(y_train, y_train_pred))) print(name+'_Test_f1_score:{:.4f}'.format(f1_score(y_test, y_test_pred)))
#描繪 ROC 曲線 fpr_tr, tpr_tr, _ = roc_curve(y_train, y_train_pred_proba) fpr_te, tpr_te, _ = roc_curve(y_test, y_test_pred_proba) # KS print(name+'_KS:') print(name+'_Train:{:.4f}'.format(max(abs((fpr_tr - tpr_tr))))) print(name+'_Test:{:.4f}'.format(max(abs((fpr_te - tpr_te))))) plt.plot(fpr_tr, tpr_tr, 'r-', label = name+"_Train:AUC: {:.3f} KS:{:.3f}".format(roc_auc_score(y_train, y_train_pred_proba), max(abs((fpr_tr - tpr_tr))))) plt.plot(fpr_te, tpr_te, 'g-', label=name+"_Test:AUC: {:.3f} KS:{:.3f}".format(roc_auc_score(y_test, y_test_pred_proba), max(abs((fpr_tr - tpr_tr))))) plt.plot([0, 1], [0, 1], 'd--') plt.legend(loc='best') plt.title(name+"_ROC curse") plt.savefig(path +'picture/'+name+'.jpg') plt.show() print('-------------------SVM_linear-------------------') model_evalua(SVM_linear, train, test, y_train, y_test,'SVM_linear')
print('-------------------SVM_rbf-------------------:') model_evalua(SVM_rbf, train, test, y_train, y_test,'SVM_rbf')
print('-------------------SVM_sigmoid-------------------:') model_evalua(SVM_sigmoid, train, test, y_train, y_test,'SVM_sigmoid')
print('-------------------lg_120-------------------') model_evalua(lg_120, train, test, y_train, y_test,'lg_120')
print('-------------------DT-------------------') model_evalua(DT, train, test, y_train, y_test,'DT')
print('-------------------xgb_sklearn-------------------') model_evalua(xgb_sklearn, train, test, y_train, y_test,'xgb_sklearn')
# print('-------------------xgb-------------------') # model_evalua(xgb, train, test, y_train, y_test)
print('-------------------lgb_sklearn-------------------') model_evalua(lgb_sklearn, train, test, y_train, y_test,'lgb_sklearn') # print('-------------------lgb-------------------') # model_evalua(lgb, train, test, y_train, y_test) --------------------- 作者:lgy54321 來源:CSDN 原文:blog.csdn.net/lgy54321/ar… 版權宣告:本文為博主原創文章,轉載請附上博文連結! |
|