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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

机器学习模型评分总结(sklearn)

發(fā)布時(shí)間:2023/12/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习模型评分总结(sklearn) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 目錄
    • 模型評(píng)估
    • 評(píng)價(jià)指標(biāo)
    • 1.分類評(píng)價(jià)指標(biāo)
      • acc、recall、F1、混淆矩陣、分類綜合報(bào)告
        • 1.準(zhǔn)確率
          • 方式一:accuracy_score
          • 方式二:metrics
        • 2.召回率
        • 3.F1分?jǐn)?shù)
        • 4.混淆矩陣
        • 5.分類報(bào)告
        • 6.kappa score
      • ROC
        • 1.ROC計(jì)算
        • 2.ROC曲線
        • 3.具體實(shí)例
    • 2.回歸評(píng)價(jià)指標(biāo)
    • 3.聚類評(píng)價(jià)指標(biāo)
      • 1.Adjusted Rand index 調(diào)整蘭德系數(shù)
      • 2.Mutual Information based scores 互信息
      • 3.Homogeneity, completeness and V-measure
      • 4.Fowlkes-Mallows scores
      • 5.Silhouette Coefficient 輪廓系數(shù)
      • 6.Calinski-Harabaz Index
    • 4.其他

目錄

模型評(píng)估

有三種不同的方法來評(píng)估一個(gè)模型的預(yù)測(cè)質(zhì)量:

  • estimator的score方法:sklearn中的estimator都具有一個(gè)score方法,它提供了一個(gè)缺省的評(píng)估法則來解決問題。
  • Scoring參數(shù):使用cross-validation的模型評(píng)估工具,依賴于內(nèi)部的scoring策略。見下。
  • 通過測(cè)試集上評(píng)估預(yù)測(cè)誤差:sklearn Metric函數(shù)用來評(píng)估預(yù)測(cè)誤差。

評(píng)價(jià)指標(biāo)

評(píng)價(jià)指標(biāo)針對(duì)不同的機(jī)器學(xué)習(xí)任務(wù)有不同的指標(biāo),同一任務(wù)也有不同側(cè)重點(diǎn)的評(píng)價(jià)指標(biāo)。
主要有分類(classification)、回歸(regression)、排序(ranking)、聚類(clustering)、熱門主題模型(topic modeling)、推薦(recommendation)等。

1.分類評(píng)價(jià)指標(biāo)

acc、recall、F1、混淆矩陣、分類綜合報(bào)告

1.準(zhǔn)確率

方式一:accuracy_score
# 準(zhǔn)確率 import numpy as np from sklearn.metrics import accuracy_score y_pred = [0, 2, 1, 3,9,9,8,5,8] y_true = [0, 1, 2, 3,2,6,3,5,9]accuracy_score(y_true, y_pred) Out[127]: 0.33333333333333331accuracy_score(y_true, y_pred, normalize=False) # 類似海明距離,每個(gè)類別求準(zhǔn)確后,再求微平均 Out[128]: 3
方式二:metrics

宏平均比微平均更合理,但也不是說微平均一無是處,具體使用哪種評(píng)測(cè)機(jī)制,還是要取決于數(shù)據(jù)集中樣本分布

宏平均(Macro-averaging),是先對(duì)每一個(gè)類統(tǒng)計(jì)指標(biāo)值,然后在對(duì)所有類求算術(shù)平均值。
微平均(Micro-averaging),是對(duì)數(shù)據(jù)集中的每一個(gè)實(shí)例不分類別進(jìn)行統(tǒng)計(jì)建立全局混淆矩陣,然后計(jì)算相應(yīng)指標(biāo)。參考博客

from sklearn import metrics metrics.precision_score(y_true, y_pred, average='micro') # 微平均,精確率 Out[130]: 0.33333333333333331metrics.precision_score(y_true, y_pred, average='macro') # 宏平均,精確率 Out[131]: 0.375metrics.precision_score(y_true, y_pred, labels=[0, 1, 2, 3], average='macro') # 指定特定分類標(biāo)簽的精確率 Out[133]: 0.5

其中average參數(shù)有五種:(None, ‘micro’, ‘macro’, ‘weighted’, ‘samples’)

2.召回率

metrics.recall_score(y_true, y_pred, average='micro') Out[134]: 0.33333333333333331metrics.recall_score(y_true, y_pred, average='macro') Out[135]: 0.3125

3.F1分?jǐn)?shù)

metrics.f1_score(y_true, y_pred, average='weighted') Out[136]: 0.37037037037037035

4.混淆矩陣

# 混淆矩陣 from sklearn.metrics import confusion_matrix confusion_matrix(y_true, y_pred)Out[137]: array([[1, 0, 0, ..., 0, 0, 0],[0, 0, 1, ..., 0, 0, 0],[0, 1, 0, ..., 0, 0, 1],..., [0, 0, 0, ..., 0, 0, 1],[0, 0, 0, ..., 0, 0, 0],[0, 0, 0, ..., 0, 1, 0]])

橫為true label 豎為predict

5.分類報(bào)告

# 分類報(bào)告:precision/recall/fi-score/均值/分類個(gè)數(shù)from sklearn.metrics import classification_reporty_true = [0, 1, 2, 2, 0]y_pred = [0, 0, 2, 2, 0]target_names = ['class 0', 'class 1', 'class 2']print(classification_report(y_true, y_pred, target_names=target_names))

包含:precision/recall/fi-score/均值/分類個(gè)數(shù)

6.kappa score

kappa score是一個(gè)介于(-1, 1)之間的數(shù). score>0.8意味著好的分類;0或更低意味著不好(實(shí)際是隨機(jī)標(biāo)簽)

from sklearn.metrics import cohen_kappa_scorey_true = [2, 0, 2, 2, 0, 1]y_pred = [0, 0, 2, 2, 0, 2]cohen_kappa_score(y_true, y_pred)

ROC

1.ROC計(jì)算

import numpy as npfrom sklearn.metrics import roc_auc_scorey_true = np.array([0, 0, 1, 1])y_scores = np.array([0.1, 0.4, 0.35, 0.8])roc_auc_score(y_true, y_scores)

2.ROC曲線

y = np.array([1, 1, 2, 2])scores = np.array([0.1, 0.4, 0.35, 0.8])fpr, tpr, thresholds = roc_curve(y, scores, pos_label=2)

3.具體實(shí)例

import numpy as np import matplotlib.pyplot as plt from itertools import cyclefrom sklearn import svm, datasets from sklearn.metrics import roc_curve, auc from sklearn.model_selection import train_test_split from sklearn.preprocessing import label_binarize from sklearn.multiclass import OneVsRestClassifier from scipy import interp# Import some data to play with iris = datasets.load_iris() X = iris.data y = iris.target# 畫圖 all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))# Then interpolate all ROC curves at this points mean_tpr = np.zeros_like(all_fpr) for i in range(n_classes):mean_tpr += interp(all_fpr, fpr[i], tpr[i])# Finally average it and compute AUC mean_tpr /= n_classesfpr["macro"] = all_fpr tpr["macro"] = mean_tpr roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])# Plot all ROC curves plt.figure() plt.plot(fpr["micro"], tpr["micro"],label='micro-average ROC curve (area = {0:0.2f})'''.format(roc_auc["micro"]),color='deeppink', linestyle=':', linewidth=4)plt.plot(fpr["macro"], tpr["macro"],label='macro-average ROC curve (area = {0:0.2f})'''.format(roc_auc["macro"]),color='navy', linestyle=':', linewidth=4)colors = cycle(['aqua', 'darkorange', 'cornflowerblue']) for i, color in zip(range(n_classes), colors):plt.plot(fpr[i], tpr[i], color=color, lw=lw,label='ROC curve of class {0} (area = {1:0.2f})'''.format(i, roc_auc[i]))plt.plot([0, 1], [0, 1], 'k--', lw=lw) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Some extension of Receiver operating characteristic to multi-class') plt.legend(loc="lower right") plt.show()

2.回歸評(píng)價(jià)指標(biāo)

回歸是對(duì)連續(xù)的實(shí)數(shù)值進(jìn)行預(yù)測(cè),而分類中是離散值。

3.聚類評(píng)價(jià)指標(biāo)

參考博客

1.Adjusted Rand index 調(diào)整蘭德系數(shù)

>>> from sklearn import metrics >>> labels_true = [0, 0, 0, 1, 1, 1] >>> labels_pred = [0, 0, 1, 1, 2, 2]>>> metrics.adjusted_rand_score(labels_true, labels_pred)

2.Mutual Information based scores 互信息

>>> from sklearn import metrics >>> labels_true = [0, 0, 0, 1, 1, 1] >>> labels_pred = [0, 0, 1, 1, 2, 2]>>> metrics.adjusted_mutual_info_score(labels_true, labels_pred) 0.22504

3.Homogeneity, completeness and V-measure

同質(zhì)性homogeneity:每個(gè)群集只包含單個(gè)類的成員。
完整性completeness:給定類的所有成員都分配給同一個(gè)群集。

>>> from sklearn import metrics >>> labels_true = [0, 0, 0, 1, 1, 1] >>> labels_pred = [0, 0, 1, 1, 2, 2]>>> metrics.homogeneity_score(labels_true, labels_pred) 0.66...>>> metrics.completeness_score(labels_true, labels_pred)

4.Fowlkes-Mallows scores

5.Silhouette Coefficient 輪廓系數(shù)

6.Calinski-Harabaz Index

4.其他

總結(jié)

以上是生活随笔為你收集整理的机器学习模型评分总结(sklearn)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。