深度学习模型进行指标评价代码
时间: 2023-08-24 12:07:24 浏览: 155
深度学习代码
以下是一个使用Python和Scikit-learn库进行模型评价的示例代码:
```python
# 导入需要的库
import numpy as np
import pandas as pd
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, confusion_matrix
# 读取数据集
data = pd.read_csv('data.csv')
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(data.drop('target', axis=1), data['target'], test_size=0.2, random_state=42)
# 训练模型
model = LogisticRegression()
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)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
confusion = confusion_matrix(y_test, y_pred)
# 输出结果
print('Accuracy: {:.2f}%'.format(accuracy*100))
print('Precision: {:.2f}%'.format(precision*100))
print('Recall: {:.2f}%'.format(recall*100))
print('F1 Score: {:.2f}%'.format(f1*100))
print('Confusion Matrix:')
print(confusion)
```
以上代码演示了如何使用逻辑回归模型对数据集进行训练,然后在测试集上进行预测,并计算准确率、精确率、召回率、F1得分和混淆矩阵等评价指标。在实际应用中,可以根据具体的需求选择适当的模型和评价指标。
阅读全文