输出模型AUC、准确度、精确度、召回率、F1-score等参数以及ROC曲线、混淆矩阵等图形的代码
时间: 2024-09-09 22:09:59 浏览: 50
机器学习基础概念:查准率、查全率、ROC、混淆矩阵、F1-Score 机器学习实战:分类器
5星 · 资源好评率100%
在Python中,特别是在机器学习库如scikit-learn中,评估模型性能通常涉及以下几个步骤:
1. **计算AUC (Area Under the Curve)**:
```python
from sklearn.metrics import roc_auc_score
y_true = ... # 真实标签
y_pred_proba = ... # 预测概率
auc = roc_auc_score(y_true, y_pred_proba)
```
2. **准确度 (Accuracy)**:
```python
accuracy = accuracy_score(y_true, y_pred) # y_pred 是预测的类别
```
3. **精确度 (Precision)**:
```python
precision = precision_score(y_true, y_pred)
```
4. **召回率 (Recall, Sensitivity or True Positive Rate)**:
```python
recall = recall_score(y_true, y_pred)
```
5. **F1-score**:
```python
f1 = f1_score(y_true, y_pred)
```
6. **绘制ROC曲线 (Receiver Operating Characteristic curve)**:
```python
import matplotlib.pyplot as plt
from sklearn.metrics import plot_roc_curve
plot_roc_curve(model, X_test, y_test)
plt.show()
```
7. **混淆矩阵 (Confusion Matrix)**:
```python
conf_mat = confusion_matrix(y_true, y_pred)
print(conf_mat)
```
以上代码假设`y_true`是实际标签列表,`y_pred`是预测结果,而`model`是你训练好的分类模型,`X_test`和`y_test`是测试集数据。
阅读全文