赞
踩
Scikit-learn 提供了多种度量指标(metrics)来评估分类、回归、聚类等机器学习任务的性能。这些度量指标有助于判断模型的表现和优化模型参数。下面将详细介绍一些常用的度量指标及其适用情况。
1、分类任务的度量指标
准确率(Accuracy)
- from sklearn.metrics import accuracy_score
- y_true = [0, 1, 1, 0, 1]
- y_pred = [0, 1, 0, 0, 1]
- accuracy = accuracy_score(y_true, y_pred)
- print(f"Accuracy: {accuracy}")
精确率(Precision)
- from sklearn.metrics import precision_score
- precision = precision_score(y_true, y_pred)
- print(f"Precision: {precision}")
召回率(Recall)
- from sklearn.metrics import recall_score
- recall = recall_score(y_true, y_pred)
- print(f"Recall: {recall}")
F1分数(F1 Score)
- from sklearn.metrics import f1_score
- f1 = f1_score(y_true, y_pred)
- print(f"F1 Score: {f1}")
ROC曲线和AUC(ROC Curve and AUC)
- from sklearn.metrics import roc_auc_score
- y_scores = [0.1, 0.4, 0.35, 0.8, 0.7]
- auc = roc_auc_score(y_true, y_scores)
- print(f"AUC: {auc}")
2、回归任务的度量指标
均方误差(Mean Squared Error, MSE)
- from sklearn.metrics import mean_squared_error
- y_true = [3, -0, 2, 7]
- y_pred = [2.5, 0.0, 2, 8]
- mse = mean_squared_error(y_true, y_pred)
- print(f"MSE: {mse}")
均绝对误差(Mean Absolute Error, MAE)
- from sklearn.metrics import mean_absolute_error
- mae = mean_absolute_error(y_true, y_pred)
- print(f"MAE: {mae}")
R²分数(R² Score)
- from sklearn.metrics import r2_score
- r2 = r2_score(y_true, y_pred)
- print(f"R² Score: {r2}")
3、聚类任务的度量指标
调整兰德指数(Adjusted Rand Index, ARI)
- from sklearn.metrics import adjusted_rand_score
- labels_true = [0, 0, 1, 1]
- labels_pred = [0, 0, 0, 1]
- ari = adjusted_rand_score(labels_true, labels_pred)
- print(f"Adjusted Rand Index: {ari}")
轮廓系数(Silhouette Coefficient)
- from sklearn.metrics import silhouette_score
- from sklearn.cluster import KMeans
- import numpy as np
-
- X = np.array([[1, 2], [1, 4], [1, 0],
- [4, 2], [4, 4], [4, 0]])
- kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
- labels = kmeans.labels_
- silhouette = silhouette_score(X, labels)
- print(f"Silhouette Coefficient: {silhouette}")
互信息(Mutual Information)
- from sklearn.metrics import normalized_mutual_info_score
- nmi = normalized_mutual_info_score(labels_true, labels_pred)
- print(f"Normalized Mutual Information: {nmi}")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。