omm7 commited on
Commit
d908a7f
·
verified ·
1 Parent(s): ad95cbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -18
app.py CHANGED
@@ -5,7 +5,6 @@ import numpy as np
5
 
6
  # --- 1. CONFIGURATION ---
7
  MODEL_FILE = 'hotel_cancellation_prediction_model_v1_0.joblib'
8
-
9
  # The list of features (columns) the model was trained on, in order.
10
  FEATURE_NAMES = [
11
  'lead_time',
@@ -28,13 +27,12 @@ def load_model():
28
  except Exception as e:
29
  st.error(f"Error loading model: {e}. Check if '{MODEL_FILE}' exists.")
30
  return None
31
-
32
  model = load_model()
33
 
34
  # --- 3. PREDICTION LOGIC ---
35
  def predict_cancellation(inputs, loaded_model):
36
- """Prepares data and gets the model's prediction."""
37
-
38
  # Map user inputs to the format the model expects
39
  input_data = {
40
  'lead_time': inputs['lead_time'],
@@ -44,32 +42,40 @@ def predict_cancellation(inputs, loaded_model):
44
  'no_of_weekend_nights': inputs['no_of_weekend_nights'],
45
  'no_of_week_nights': inputs['no_of_week_nights'],
46
  'arrival_month': inputs['arrival_month'],
47
-
48
  # Binary encoding for categorical features
49
  'market_segment_type_Online': 1.0 if inputs['market_segment_type'] == 'Online' else 0.0,
50
  'required_car_parking_space': 1.0 if inputs['required_car_parking_space'] == "Yes" else 0.0,
51
  }
52
-
53
  # Create a DataFrame with the correct column order
54
  input_df = pd.DataFrame([input_data], columns=FEATURE_NAMES)
55
-
56
  # Make prediction (0=Not Cancelled, 1=Cancelled)
57
  prediction = loaded_model.predict(input_df)[0]
 
 
 
 
 
 
 
 
 
 
58
 
59
- return prediction
60
 
61
  # --- 4. STREAMLIT INTERFACE ---
62
-
63
  st.title("Hotel Booking Cancellation Predictor")
64
 
65
  if model is None:
66
  st.stop()
67
-
68
  st.markdown("Enter booking details to predict if the reservation will be cancelled.")
69
  st.markdown("---")
70
 
71
  # --- Input Fields (Single Column) ---
72
-
73
  # Simple number inputs for basic data types
74
  lead_time = st.number_input("1. Lead Time (Days before arrival)", min_value=0, value=82, step=1)
75
  arrival_month = st.selectbox("2. Arrival Month (1=Jan to 12=Dec)", list(range(1, 13)), index=6) # Default to July (7)
@@ -83,11 +89,9 @@ no_of_special_requests = st.number_input("7. Number of Special Requests", min_va
83
  market_segment_type = st.selectbox("8. Market Segment Type", ["Online", "Offline"])
84
  required_car_parking_space = st.selectbox("9. Required Car Parking Space", ["Yes", "No"])
85
 
86
-
87
  # --- 5. PREDICTION BUTTON AND OUTPUT ---
88
-
89
  if st.button("Get Prediction Result", type="primary"):
90
-
91
  # Dictionary to pass inputs easily
92
  user_inputs = {
93
  'lead_time': lead_time,
@@ -101,12 +105,17 @@ if st.button("Get Prediction Result", type="primary"):
101
  'required_car_parking_space': required_car_parking_space,
102
  }
103
 
104
- prediction = predict_cancellation(user_inputs, model)
105
-
 
106
  st.markdown("---")
107
  st.subheader("Prediction Result")
108
-
 
109
  if prediction == 1:
110
  st.error("The model predicts the booking will be **CANCELLED**.")
111
  else:
112
- st.success("The model predicts the booking will be **Not Cancelled**.")
 
 
 
 
5
 
6
  # --- 1. CONFIGURATION ---
7
  MODEL_FILE = 'hotel_cancellation_prediction_model_v1_0.joblib'
 
8
  # The list of features (columns) the model was trained on, in order.
9
  FEATURE_NAMES = [
10
  'lead_time',
 
27
  except Exception as e:
28
  st.error(f"Error loading model: {e}. Check if '{MODEL_FILE}' exists.")
29
  return None
 
30
  model = load_model()
31
 
32
  # --- 3. PREDICTION LOGIC ---
33
  def predict_cancellation(inputs, loaded_model):
34
+ """Prepares data and gets the model's prediction and confidence score."""
35
+
36
  # Map user inputs to the format the model expects
37
  input_data = {
38
  'lead_time': inputs['lead_time'],
 
42
  'no_of_weekend_nights': inputs['no_of_weekend_nights'],
43
  'no_of_week_nights': inputs['no_of_week_nights'],
44
  'arrival_month': inputs['arrival_month'],
45
+
46
  # Binary encoding for categorical features
47
  'market_segment_type_Online': 1.0 if inputs['market_segment_type'] == 'Online' else 0.0,
48
  'required_car_parking_space': 1.0 if inputs['required_car_parking_space'] == "Yes" else 0.0,
49
  }
50
+
51
  # Create a DataFrame with the correct column order
52
  input_df = pd.DataFrame([input_data], columns=FEATURE_NAMES)
53
+
54
  # Make prediction (0=Not Cancelled, 1=Cancelled)
55
  prediction = loaded_model.predict(input_df)[0]
56
+
57
+ # Get probability scores for each class (0 and 1)
58
+ # The output is typically [P(Class 0), P(Class 1)]
59
+ probabilities = loaded_model.predict_proba(input_df)[0]
60
+
61
+ # The confidence score for the predicted class
62
+ if prediction == 1:
63
+ confidence_score = probabilities[1] # Probability of being Cancelled (Class 1)
64
+ else:
65
+ confidence_score = probabilities[0] # Probability of being Not Cancelled (Class 0)
66
 
67
+ return prediction, confidence_score
68
 
69
  # --- 4. STREAMLIT INTERFACE ---
 
70
  st.title("Hotel Booking Cancellation Predictor")
71
 
72
  if model is None:
73
  st.stop()
74
+
75
  st.markdown("Enter booking details to predict if the reservation will be cancelled.")
76
  st.markdown("---")
77
 
78
  # --- Input Fields (Single Column) ---
 
79
  # Simple number inputs for basic data types
80
  lead_time = st.number_input("1. Lead Time (Days before arrival)", min_value=0, value=82, step=1)
81
  arrival_month = st.selectbox("2. Arrival Month (1=Jan to 12=Dec)", list(range(1, 13)), index=6) # Default to July (7)
 
89
  market_segment_type = st.selectbox("8. Market Segment Type", ["Online", "Offline"])
90
  required_car_parking_space = st.selectbox("9. Required Car Parking Space", ["Yes", "No"])
91
 
 
92
  # --- 5. PREDICTION BUTTON AND OUTPUT ---
 
93
  if st.button("Get Prediction Result", type="primary"):
94
+
95
  # Dictionary to pass inputs easily
96
  user_inputs = {
97
  'lead_time': lead_time,
 
105
  'required_car_parking_space': required_car_parking_space,
106
  }
107
 
108
+ # Get both the prediction and the confidence score
109
+ prediction, confidence_score = predict_cancellation(user_inputs, model)
110
+
111
  st.markdown("---")
112
  st.subheader("Prediction Result")
113
+
114
+ # Display the result based on the prediction
115
  if prediction == 1:
116
  st.error("The model predicts the booking will be **CANCELLED**.")
117
  else:
118
+ st.success("The model predicts the booking will be **Not Cancelled**.")
119
+
120
+ # Display the confidence score formatted as a percentage
121
+ st.info(f"Confidence Score: **{confidence_score * 100:.2f}%**")