CoolFace
Apppublic

D-Khalid/GeneScout_AI_Predictive_Pathologist

sourceHugging Faceupdated 10mo agoView on Hugging Face
1likes
train_model.py86 linesDownload Raw Back to root
1# train_model.py2 3import pandas as pd4import joblib  # Used to save the model for the app later5from sklearn.model_selection import train_test_split6from sklearn.preprocessing import StandardScaler7from sklearn.linear_model import LogisticRegression8from sklearn.ensemble import RandomForestClassifier, VotingClassifier9from sklearn.svm import SVC10from sklearn.metrics import accuracy_score, classification_report, confusion_matrix11import seaborn as sns12import matplotlib.pyplot as plt13 14# --- Step 1: Load Data ---15print("1. Loading data...")16df = pd.read_csv('genetic_disease_dataset.csv')17 18# Separate Features (X) and Target (y)19X = df.drop('Disease', axis=1)20y = df['Disease']21 22# --- Step 2: Split Data ---23# We keep 20% of data hidden to test the doctors later.24# stratify=y ensures we have an equal number of sick people in both sets.25print("2. Splitting data...")26X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)27 28# --- Step 3: Scaling (Crucial) ---29# SVM and Logistic Regression fail if numbers are too big/small.30# We scale everything to have a mean of 0 (Standard Deviation).31print("3. Scaling features...")32scaler = StandardScaler()33X_train_scaled = scaler.fit_transform(X_train)34X_test_scaled = scaler.transform(X_test)35 36# --- Step 4: Define the "Doctors" ---37print("4. Initializing the Board of Doctors...")38 39# Doctor 1: Logistic Regression (The Statistician)40# Good for finding simple linear relationships.41clf1 = LogisticRegression(random_state=1)42 43# Doctor 2: Random Forest (The Specialist)44# Good for complex rules (if Age > 50 and Gene = Mutated...)45clf2 = RandomForestClassifier(n_estimators=50, random_state=1)46 47# Doctor 3: SVM (The Mathematician)48# Draws complex geometric boundaries between diseases.49# probability=True is required so it can "vote" with confidence scores.50clf3 = SVC(kernel='linear', probability=True, random_state=1)51 52# --- Step 5: The Voting Classifier ---53# voting='soft' means we average the probabilities (e.g., 90% + 80% + 70%)54# instead of just counting "Yes/No" votes. It's more accurate.55ensemble_model = VotingClassifier(56    estimators=[('lr', clf1), ('rf', clf2), ('svc', clf3)], 57    voting='soft'58)59 60# --- Step 6: Train ---61print("5. Training models (this might take a second)...")62ensemble_model.fit(X_train_scaled, y_train)63 64# --- Step 7: Evaluate ---65print("6. Evaluating performance...")66y_pred = ensemble_model.predict(X_test_scaled)67 68accuracy = accuracy_score(y_test, y_pred)69print(f"\n✅ Final Accuracy: {accuracy*100:.2f}%")70print("\nClassification Report:\n")71print(classification_report(y_test, y_pred))72 73# --- Step 8: Save the Brain ---74# We save the Model AND the Scaler. We need both for the App.75joblib.dump(ensemble_model, 'genetic_disease_model.pkl')76joblib.dump(scaler, 'scaler.pkl')77print("✅ Model and Scaler saved to disk!")78 79# Optional: Save Confusion Matrix Image80plt.figure(figsize=(8,6))81sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues')82plt.title('Confusion Matrix: Board of Doctors')83plt.ylabel('Actual Disease')84plt.xlabel('Predicted Disease')85plt.savefig('4_confusion_matrix.png')86print("✅ Confusion Matrix saved as image.")