Update app.py
Browse files
app.py
CHANGED
|
@@ -15,24 +15,24 @@ MODEL_NAMES = [
|
|
| 15 |
'XGBoost Regressor',
|
| 16 |
'LGBM Regressor',
|
| 17 |
]
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
path = os.path.join(MODEL_DIR, f"{name.replace(' ', '')}.joblib")
|
| 24 |
-
try:
|
| 25 |
-
models[name] = joblib.load(path)
|
| 26 |
-
except Exception as e:
|
| 27 |
-
st.error(f"Error loading model {name}: {str(e)}")
|
| 28 |
-
return models
|
| 29 |
|
| 30 |
# Load models
|
| 31 |
models = load_models(MODEL_NAMES)
|
| 32 |
|
| 33 |
# Load dataset
|
| 34 |
data_path = os.path.join(DATA_DIR, DATA_FILE)
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Prepare features and target
|
| 38 |
X = df.drop(columns=['Salary'])
|
|
@@ -56,6 +56,7 @@ max_years = float(df.YearsOfExperience.max() * 1.5)
|
|
| 56 |
# Precompute predictions for training set
|
| 57 |
y_train_predictions = {name: model.predict(X_train) for name, model in models.items()}
|
| 58 |
|
|
|
|
| 59 |
def load_and_predict(sample: pd.DataFrame) -> pd.DataFrame:
|
| 60 |
"""Predict salary using loaded models and evaluate statistics."""
|
| 61 |
results = []
|
|
@@ -63,10 +64,14 @@ def load_and_predict(sample: pd.DataFrame) -> pd.DataFrame:
|
|
| 63 |
for name, model in models.items():
|
| 64 |
try:
|
| 65 |
salary_pred = model.predict(sample)[0]
|
|
|
|
|
|
|
| 66 |
results.append({
|
| 67 |
'Model': name,
|
| 68 |
'Predicted Salary': salary_pred,
|
| 69 |
'R2 Score (%)': r2_score(y_train, y_train_predictions[name]) * 100,
|
|
|
|
|
|
|
| 70 |
})
|
| 71 |
except Exception as e:
|
| 72 |
st.error(f"Error during prediction with model {name}: {str(e)}")
|
|
|
|
| 15 |
'XGBoost Regressor',
|
| 16 |
'LGBM Regressor',
|
| 17 |
]
|
| 18 |
+
|
| 19 |
+
@st.cache
|
| 20 |
+
def load_data(path: str) -> pd.DataFrame:
|
| 21 |
+
"""Load the dataset with caching."""
|
| 22 |
+
return pd.read_csv(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Load models
|
| 25 |
models = load_models(MODEL_NAMES)
|
| 26 |
|
| 27 |
# Load dataset
|
| 28 |
data_path = os.path.join(DATA_DIR, DATA_FILE)
|
| 29 |
+
|
| 30 |
+
# Load dataset with error handling
|
| 31 |
+
try:
|
| 32 |
+
df = load_data(data_path)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
st.error(f"Error loading dataset: {str(e)}")
|
| 35 |
+
st.stop()
|
| 36 |
|
| 37 |
# Prepare features and target
|
| 38 |
X = df.drop(columns=['Salary'])
|
|
|
|
| 56 |
# Precompute predictions for training set
|
| 57 |
y_train_predictions = {name: model.predict(X_train) for name, model in models.items()}
|
| 58 |
|
| 59 |
+
# Include more metrics for model performance
|
| 60 |
def load_and_predict(sample: pd.DataFrame) -> pd.DataFrame:
|
| 61 |
"""Predict salary using loaded models and evaluate statistics."""
|
| 62 |
results = []
|
|
|
|
| 64 |
for name, model in models.items():
|
| 65 |
try:
|
| 66 |
salary_pred = model.predict(sample)[0]
|
| 67 |
+
mae = mean_absolute_error(y_train, y_train_predictions[name]) # Example metric
|
| 68 |
+
mse = mean_squared_error(y_train, y_train_predictions[name]) # Example metric
|
| 69 |
results.append({
|
| 70 |
'Model': name,
|
| 71 |
'Predicted Salary': salary_pred,
|
| 72 |
'R2 Score (%)': r2_score(y_train, y_train_predictions[name]) * 100,
|
| 73 |
+
'Mean Absolute Error': mae,
|
| 74 |
+
'Mean Squared Error': mse,
|
| 75 |
})
|
| 76 |
except Exception as e:
|
| 77 |
st.error(f"Error during prediction with model {name}: {str(e)}")
|