cc-module-python / model_utils.py
LioD19's picture
Upload 5 files
f4a61c0 verified
Raw
History Blame Contribute Delete
2.52 kB
import pickle
import pandas as pd
import numpy as np
import streamlit as st
import io
def load_model(uploaded_file):
"""
Load an uploaded model file
Args:
uploaded_file: Streamlit UploadedFile object containing the model
Returns:
The loaded model object
"""
try:
# Read the file
bytes_data = uploaded_file.getvalue()
# Load the model
model = pickle.loads(bytes_data)
return model
except Exception as e:
raise ValueError(f"Failed to load model: {str(e)}")
def predict_price(model, input_df, with_confidence_interval=False, confidence_level=0.95, error_margin=0.15):
"""
Make prediction using the loaded model with optional confidence interval
Args:
model: The loaded XGBoost model
input_df (pd.DataFrame): Processed input dataframe
with_confidence_interval (bool): Whether to return confidence interval
confidence_level (float): Confidence level (between 0 and 1)
error_margin (float): Estimated error margin as a proportion of the prediction
Returns:
If with_confidence_interval is False:
float: Predicted price
If with_confidence_interval is True:
tuple: (predicted_price, lower_bound, upper_bound)
"""
try:
# Make prediction
prediction = model.predict(input_df)
predicted_price = float(prediction[0])
if with_confidence_interval:
# Map confidence levels to z-scores
z_scores = {
0.50: 0.674,
0.80: 1.282,
0.90: 1.645,
0.95: 1.96,
0.99: 2.576,
0.999: 3.291
}
# Get the appropriate z-score (default to 95% if not found)
z_score = z_scores.get(confidence_level, 1.96)
# Calculate the margin of error
margin = predicted_price * error_margin
# Calculate the bounds of the confidence interval
lower_bound = max(0, predicted_price - (z_score * margin))
upper_bound = predicted_price + (z_score * margin)
return predicted_price, lower_bound, upper_bound
else:
return predicted_price
except Exception as e:
raise ValueError(f"Failed to make prediction: {str(e)}")