File size: 2,519 Bytes
f4a61c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)}")