在机器学习中,评估模型的性能对于了解它们在特定任务上的表现是至关重要的。你提到的这些指标——准确率、精确度、召回率、F1 分数、支持度和提升度——是用于此目的的常用指标,特别是在分类问题中。让我们逐一理解它们各自衡量的是什么:
1. 混淆矩阵
混淆矩阵是一个表格,用于描述分类模型在一组已知真实值的测试数据上的性能。它特别适用于衡量召回率、精确率、准确率和其他指标。
二分类问题的混淆矩阵结构如下:
- 真正例 (TP): 模型正确预测正类的情况。
- 真反例 (TN): 模型正确预测负类的情况。
- 假正例 (FP): 模型错误地预测为正类的情况(又称为第一类错误)。
- 假反例 (FN): 模型错误地预测为负类的情况(又称为第二类错误)。
矩阵通常按以下方式排列:
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.datasets import make_classification
# Create a synthetic dataset
X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
# Train the classifier
clf = RandomForestClassifier(random_state=42)
clf.fit(X_train, y_train)
# Predict on the test set
y_pred = c