File size: 3,988 Bytes
430ff5d | 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | """
Model Export Script
Regenerates and exports the trained model with current dependencies
Run this to fix numpy compatibility issues
"""
import os
import sys
import json
import joblib
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
print("\n" + "="*70)
print("π Model Export & Regeneration Script")
print("="*70)
# Step 1: Load data
print("\n[1/5] Loading data...")
try:
df = pd.read_csv("../../cleaned_patient_data.csv")
print(f"β
Data loaded: {df.shape[0]} samples, {df.shape[1]} features")
except FileNotFoundError:
print("β Error: cleaned_patient_data.csv not found!")
print("Expected path: ../../cleaned_patient_data.csv")
sys.exit(1)
# Step 2: Prepare features and target
print("\n[2/5] Preparing features and target...")
X = df.drop(columns=["workload_hours"])
y = df["workload_hours"]
print(f"β
Original features: {X.shape[1]}")
print(f" {X.columns.tolist()}")
# Compute interaction features from the 5 basic features
print("\n Computing interaction features...")
X["alert_cognition_interaction"] = X["num_alerts_last_week"] * (30 - X["cognitive_score"])
X["activity_sleep_ratio"] = X["patient_activity_level"] / X["sleep_hours_avg"]
X["cognitive_activity_ratio"] = X["cognitive_score"] * X["patient_activity_level"] / 1000
print(f"β
After interaction features: {X.shape[1]}")
print(f" {X.columns.tolist()}")
# Step 3: Train/Test Split
print("\n[3/5] Splitting data...")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
print(f"β
Training: {X_train.shape[0]} samples")
print(f"β
Testing: {X_test.shape[0]} samples")
# Step 4: Train Best Model (Linear Regression)
print("\n[4/5] Training Linear Regression model...")
model = LinearRegression()
model.fit(X_train, y_train)
# Evaluate
y_pred = model.predict(X_test)
r2 = r2_score(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print(f"β
Model trained!")
print(f" RΒ² Score: {r2:.4f}")
print(f" MAE: {mae:.4f}")
print(f" RMSE: {rmse:.4f}")
if r2 >= 0.80:
print(f" β
DSO Target ACHIEVED (RΒ² β₯ 0.80)")
else:
print(f" β οΈ DSO Target PENDING (RΒ² = {r2:.4f}, target 0.80)")
# Step 5: Export Model & Metadata
print("\n[5/5] Exporting model and metadata...")
# Export model using joblib (more robust than pickle)
model_path = os.path.join(os.path.dirname(__file__), "workload_predictor.pkl")
joblib.dump(model, model_path)
print(f"β
Model exported: {model_path}")
# Export features
features_path = os.path.join(os.path.dirname(__file__), "features.json")
with open(features_path, 'w') as f:
json.dump(X.columns.tolist(), f, indent=2)
print(f"β
Features exported: {features_path}")
# Export metadata
metadata = {
"model_type": "RandomForestRegressor",
"r2_score": float(r2),
"mae": float(mae),
"rmse": float(rmse),
"n_features": int(X.shape[1]),
"features": X.columns.tolist(),
"training_samples": int(X_train.shape[0]),
"test_samples": int(X_test.shape[0]),
"export_timestamp": pd.Timestamp.now().isoformat(),
"numpy_version": np.__version__,
"sklearn_version": __import__('sklearn').__version__
}
metadata_path = os.path.join(os.path.dirname(__file__), "model_metadata.json")
with open(metadata_path, 'w') as f:
json.dump(metadata, f, indent=2)
print(f"β
Metadata exported: {metadata_path}")
print("\n" + "="*70)
print("β
Model Export Complete!")
print("="*70)
print("\nπ Files created/updated:")
print(f" β’ {model_path}")
print(f" β’ {features_path}")
print(f" β’ {metadata_path}")
print("\nπ Next: Restart Flask API")
print(" Run: python app.py")
print("="*70 + "\n")
|