Repository navigation
Expand file tree
/
Copy pathtrain_models.py
More file actions
97 lines (74 loc) · 3.17 KB
/
Copy pathtrain_models.py
File metadata and controls
97 lines (74 loc) · 3.17 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
86
87
88
89
90
91
92
93
94
95
96
97
import os
import pandas as pd
import joblib
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.pipeline import Pipeline
import warnings
warnings.filterwarnings('ignore')
from utils.preprocess import clean_data, get_preprocessor
def train_for_disease(disease_name, data_path, target_col='target'):
print(f"\n{'='*50}\nTraining models for: {disease_name.upper()}\n{'='*50}")
if not os.path.exists(data_path):
print(f"Error: Data file not found at {data_path}")
return
df = pd.read_csv(data_path)
df = clean_data(df)
# Separate features and target
X = df.drop(columns=[target_col])
y = df[target_col]
# Identify numeric and categorical columns
numeric_features = X.select_dtypes(include=['int64', 'float64']).columns.tolist()
categorical_features = X.select_dtypes(include=['object', 'category']).columns.tolist()
print(f"Dataset shape: {df.shape}")
print(f"Numeric features: {len(numeric_features)}")
print(f"Categorical features: {len(categorical_features)}")
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
preprocessor = get_preprocessor(numeric_features, categorical_features)
models = {
'Logistic Regression': LogisticRegression(max_iter=1000, random_state=42),
'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42),
'Gradient Boosting': GradientBoostingClassifier(random_state=42),
'XGBoost': XGBClassifier(use_label_encoder=False, eval_metric='logloss', random_state=42)
}
best_model = None
best_accuracy = 0
best_name = ""
for name, model in models.items():
pipeline = Pipeline(steps=[
('preprocessor', preprocessor),
('classifier', model)
])
# Train
pipeline.fit(X_train, y_train)
# Predict
y_pred = pipeline.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print(f"{name} Accuracy: {acc:.4f}")
if acc > best_accuracy:
best_accuracy = acc
best_model = pipeline
best_name = name
print(f"\nBest Model: {best_name} with Accuracy: {best_accuracy:.4f}")
# Ensure models directory exists
os.makedirs('models', exist_ok=True)
# Save best model
model_path = f"models/{disease_name}_model.pkl"
joblib.dump(best_model, model_path)
print(f"Saved best model to {model_path}")
def train_all():
datasets = {
'heart': 'data/heart/heart.csv',
'diabetes': 'data/diabetes/diabetes.csv',
'breast_cancer': 'data/breast_cancer/breast_cancer.csv',
'kidney': 'data/kidney/kidney.csv',
'liver': 'data/liver/liver.csv'
}
for disease, path in datasets.items():
train_for_disease(disease, path)
if __name__ == "__main__":
train_all()