基于决策树和KNN算法的心脏病预测
时间: 2023-09-16 14:14:23 浏览: 136
分类决策树近邻算法JAVA实现.rar_java决策树算法_tearsyeq_分类决策树近邻算法Java
心脏病预测是一个非常常见的机器学习问题,可以使用决策树和KNN算法来解决。下面是基于决策树和KNN算法的心脏病预测的示例代码:
```python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# 加载数据
data = pd.read_csv('heart.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)
# 决策树模型训练和评估
dt_model = DecisionTreeClassifier(max_depth=3)
dt_model.fit(X_train, y_train)
dt_y_pred = dt_model.predict(X_test)
dt_acc = accuracy_score(y_test, dt_y_pred)
dt_precision = precision_score(y_test, dt_y_pred)
dt_recall = recall_score(y_test, dt_y_pred)
dt_f1 = f1_score(y_test, dt_y_pred)
print('Decision Tree Model:')
print('Accuracy:', dt_acc)
print('Precision:', dt_precision)
print('Recall:', dt_recall)
print('F1:', dt_f1)
# KNN模型训练和评估
knn_model = KNeighborsClassifier(n_neighbors=5)
knn_model.fit(X_train, y_train)
knn_y_pred = knn_model.predict(X_test)
knn_acc = accuracy_score(y_test, knn_y_pred)
knn_precision = precision_score(y_test, knn_y_pred)
knn_recall = recall_score(y_test, knn_y_pred)
knn_f1 = f1_score(y_test, knn_y_pred)
print('\nKNN Model:')
print('Accuracy:', knn_acc)
print('Precision:', knn_precision)
print('Recall:', knn_recall)
print('F1:', knn_f1)
```
在上述代码中,我们首先加载了`heart.csv`数据集,并对特征进行了标准化处理。然后使用`train_test_split`函数将数据集划分为训练集和测试集,分别训练了决策树模型和KNN模型,并对两个模型进行了评估。
需要注意的是,决策树模型和KNN模型的超参数需要根据实际情况进行调整,以获得更好的性能。
阅读全文