pytorch计算模型评价指标准确率、精确率、召回率、F1值、AUC的示例代码
时间: 2023-09-29 08:06:55 浏览: 144
以下是计算模型评价指标准确率、精确率、召回率、F1值、AUC的示例代码,使用的数据集是sklearn中的iris数据集:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score
# 加载数据集
iris = load_iris()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
# 训练模型
model = LogisticRegression(random_state=42)
model.fit(X_train, y_train)
# 预测测试集
y_pred = model.predict(X_test)
# 计算各项评价指标
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='macro')
recall = recall_score(y_test, y_pred, average='macro')
f1 = f1_score(y_test, y_pred, average='macro')
auc = roc_auc_score(y_test, model.predict_proba(X_test), multi_class='ovr')
# 打印评价指标的值
print("Accuracy: {}".format(accuracy))
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1 Score: {}".format(f1))
print("AUC Score: {}".format(auc))
```
输出结果如下:
```
Accuracy: 1.0
Precision: 1.0
Recall: 1.0
F1 Score: 1.0
AUC Score: 1.0
```
以上是示例代码,可以根据具体情况进行修改。
阅读全文