日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第10章 评价分类结果

發布時間:2025/4/5 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第10章 评价分类结果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

分類算法的評價

分類準確度的問題

?

?精準率和召回率

?

?

?

?

?

?

?Precision和Recall的平衡

?

?

?

?

?

,??,,?,

?

?

?ROC,AUC用來比較兩個模型的優劣

import numpy as np from sklearn import datasets digits=datasets.load_digits() X=digits.data y=digits.target.copy()y[digits.target==9]=1 y[digits.target!=9]=0 #由此變成了極度偏斜的數據問題(skewed data)from sklearn.model_selection import train_test_split X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=666)from sklearn.linear_model import LogisticRegression log_reg=LogisticRegression() log_reg.fit(X_train,y_train) log_reg.score(X_test,y_test)y_log_predict=log_reg.predict(X_test)def TN(y_true,y_predict):assert len(y_true)==len(y_predict)return np.sum((y_true==0)&(y_predict==0)) TN(y_test,y_log_predict)def FP(y_true,y_predict):assert len(y_true)==len(y_predict)return np.sum((y_true==0)&(y_predict==1)) FP(y_test,y_log_predict)def FN(y_true,y_predict):assert len(y_true)==len(y_predict)return np.sum((y_true==1)&(y_predict==0)) FN(y_test,y_log_predict)def TP(y_true,y_predict):assert len(y_true)==len(y_predict)return np.sum((y_true==1)&(y_predict==1)) TP(y_test,y_log_predict)def confusion_matrix(y_true,y_predict):return np.array([[TN(y_true,y_predict),FP(y_true,y_predict)],[FN(y_true,y_predict),TP(y_true,y_predict)]]) confusion_matrix(y_test,y_log_predict)def precision_score(y_true,y_predict):tp=TP(y_true,y_predict)fp=FP(y_true,y_predict)try:return tp/(tp+fp)except:return 0.0 precision_score(y_test,y_log_predict)def recall_score(y_true,y_predict):tp=TP(y_true,y_predict)fn=FN(y_true,y_predict)try:return tp/(tp+fn)except:return 0.0 recall_score(y_test,y_log_predict)# scikit-learn中的混淆矩陣,精準率和召回率 from sklearn.metrics import confusion_matrix confusion_matrix(y_test,y_log_predict)from sklearn.metrics import precision_score precision_score(y_test,y_log_predict)from sklearn.metrics import recall_score recall_score(y_test,y_log_predict)### 兩者兼顧:(召回率和精準率)F1 Score def f1_score(precision,recall):try:return 2*precision*recall/(precision+recall)except:return 0.0precision=0.5 recall=0.5 f1_score(precision,recall)# scikit-learn中的F1 Score from sklearn.metrics import f1_score f1_score(y_test,y_log_predict)# 精準率和召回率的平衡 decision_scores=log_reg.decision_function(X_test) y_predict_2=np.array(decision_scores>=5,dtype='int')confusion_matrix(y_test,y_predict_2) recall_score(y_test,y_predict_2)from sklearn.metrics import precision_score from sklearn.metrics import recall_score precisions=[] recalls=[] thresholds=np.arange(np.min(decision_scores),np.max(decision_scores),0.1) for threshold in thresholds:y_predict=np.array(decision_scores>=threshold,dtype='int')precisions.append(precision_score(y_test,y_predict))recalls.append(recall_score(y_test,y_predict)plt.plot(thresholds,precisions) plt.plot(thresholds,recalls) plt.show()### Precision-Recall 曲線 plt.plot(precisions, recalls) plt.show()### scikit-learn中的Precision-Recall曲線 from sklearn.metrics import precision_recall_curve precisions,recalls,thresholds=precision_recall_curve(y_test,decision_scores)plt.plot(thresholds,precisions[:-1]) plt.plot(thresholds,recalls[:-1]) plt.show()plt.plot(precisions,recalls) plt.show()# ROC曲線 from playML.metrics import FPR,TPR fprs=[] tprs=[] thresholds=np.arange(np.min(decision_scores),np.max(decision_scores),0.1) for threshold in thresholds:y_predict=np.array(decision_scores>=threshold,dtype='int')fprs.append(FPR(y_test,y_predict))tprs.append(TPR(y_test,y_predict))plt.plot(fprs, tprs) plt.show()### scikit-learn中的ROC曲線 from sklearn.metrics import roc_curve fprs, tprs, thresholds = roc_curve(y_test, decision_scores)plt.plot(fprs,tprs) plt.show()from sklearn.metrics import roc_auc_score # 求面積 roc_auc_score(y_test,decision_scores)多分類問題中的混淆矩陣 import numpy as np import matplotlib.pyplot as plt from sklearn import datasets digits=datasets.load_digits() X=digits.data y=digits.targetfrom sklearn.model_selection import train_test_split X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.8,random_state=666)from sklearn.linear_model import LogisticRegression log_reg=LogisticRegression() log_reg.fit(X_train,y_train) log_reg.score(X_test,y_test) y_predict=log_reg.predict(X_test)from sklearn.metrics import precision_score precision_score(y_test,y_predict,average="micro") # 解決多分類問題from sklearn.metrics import confusion_matrix confusion_matrix(y_test,y_predict)cfm=confusion_matrix(y_test,y_predict) plt.matshow(cfm,cmap=plt.cm.gray) plt.show()row_sums=np.sum(cfm,axis=1) err_matrix=cfm/row_sums np.fill_diagonal(err_matrix,0) err_matrixplt.matshow(err_matrix,cmap=plt.cm.gray) plt.show() 評價分類結果

總結

以上是生活随笔為你收集整理的第10章 评价分类结果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。