-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheart_attack.py
More file actions
86 lines (70 loc) · 2.63 KB
/
heart_attack.py
File metadata and controls
86 lines (70 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, roc_curve, auc
# Load dataset
data = pd.read_csv("Heart Attack.csv")
# Rename columns and create new features
data.rename(columns={'impluse': 'pulse', 'pressurehight': 'sys', 'pressurelow': 'dias'}, inplace=True)
data['glu_mmol'] = data['glucose'] / 18
# Recoding categorical variables
data['gender'] = data['gender'].map({1: 'Male', 0: 'Female'}).astype('category')
data['class'] = data['class'].astype('category')
# Select relevant features
features = ['age', 'gender', 'pulse', 'sys', 'dias', 'kcm', 'glu_mmol', 'troponin']
target = 'class'
X = data[features]
y = data[target]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=125)
# Preprocessing pipeline
numeric_features = ['age', 'pulse', 'sys', 'dias', 'kcm', 'glu_mmol', 'troponin']
categorical_features = ['gender']
numeric_transformer = StandardScaler()
categorical_transformer = OneHotEncoder(drop='first')
preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, numeric_features),
('cat', categorical_transformer, categorical_features)
]
)
# Build logistic regression model
log_model = Pipeline([
('preprocessor', preprocessor),
('classifier', LogisticRegression(solver='liblinear'))
])
# Cross-validation
scores = cross_val_score(log_model, X_train, y_train, cv=5, scoring='accuracy')
print(f'Cross-validation accuracy: {scores.mean():.3f}')
# Fit model
log_model.fit(X_train, y_train)
# Predictions
y_pred = log_model.predict(X_test)
y_pred_prob = log_model.predict_proba(X_test)[:, 1]
# Model evaluation
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.3f}')
print(classification_report(y_test, y_pred))
# Confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
# ROC Curve
fpr, tpr, _ = roc_curve(y_test.cat.codes, y_pred_prob)
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, label=f'AUC = {roc_auc:.3f}')
plt.plot([0, 1], [0, 1], linestyle='--', color='grey')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve')
plt.legend()
plt.show()