-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutiple_model_test.py
More file actions
59 lines (50 loc) · 1.75 KB
/
mutiple_model_test.py
File metadata and controls
59 lines (50 loc) · 1.75 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
# 📦 Imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import StratifiedKFold
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
# 📥 Load dataset
url = "https://raw.githubusercontent.com/omairaasim/machine-learning-datasets/main/heart.csv"
df = pd.read_csv(url)
X = df.drop("target", axis=1)
y = df["target"]
# 📊 Define models to evaluate
models = {
"Logistic Regression": LogisticRegression(max_iter=1000),
"Random Forest": RandomForestClassifier(),
"Support Vector Machine": SVC(),
"Naive Bayes": GaussianNB(),
"K-Nearest Neighbors": KNeighborsClassifier(),
"Decision Tree": DecisionTreeClassifier()
}
# ⚙️ Cross-validation setup
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
# 📈 Store results
results = {}
for name, model in models.items():
pipe = Pipeline([
('scaler', StandardScaler()), # Scale the features
('classifier', model)
])
scores = cross_val_score(pipe, X, y, cv=cv, scoring='accuracy')
results[name] = scores
print(f"{name}: Mean Accuracy = {scores.mean():.4f}, Std = {scores.std():.4f}")
# 📊 Plot the results
plt.figure(figsize=(10, 6))
sns.boxplot(data=pd.DataFrame(results))
plt.title("Model Comparison (Accuracy using 5-Fold CV)")
plt.ylabel("Accuracy")
plt.xticks(rotation=45)
plt.grid(True)
plt.tight_layout()
plt.show()