对心脏病数据进行模型评估代码
时间: 2023-08-22 17:07:48 浏览: 77
基于python的心脏病数据分析
5星 · 资源好评率100%
以下是对心脏病数据进行模型评估的Python代码示例:
```python
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# 加载数据
data = pd.read_csv('heart_disease.csv')
# 特征工程
X = data.drop('target', axis=1)
y = data['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# 模型训练
model = LogisticRegression()
model.fit(X_train, y_train)
# 模型评估
y_pred = model.predict(X_test)
acc = 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)
print('Accuracy:', acc)
print('Precision:', precision)
print('Recall:', recall)
print('F1:', f1)
```
其中,`heart_disease.csv`是包含心脏病数据的CSV文件,`train_test_split`函数用于将数据集划分为训练集和测试集,`StandardScaler`用于对数据进行标准化处理,`LogisticRegression`是逻辑回归模型,`accuracy_score`、`precision_score`、`recall_score`和`f1_score`用于计算模型评估指标。
阅读全文