File size: 11,489 Bytes
2c2ffac
b965b34
 
 
 
 
 
2c2ffac
 
b965b34
 
 
 
aea9fdb
 
2c2ffac
9bd2d1f
 
2c2ffac
 
 
 
 
 
 
 
 
 
 
b965b34
a56815a
b965b34
 
 
 
 
 
 
 
 
aea9fdb
b965b34
2c2ffac
 
b965b34
2c2ffac
 
 
 
 
 
 
 
 
 
 
 
b965b34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bd2d1f
 
b965b34
 
 
 
 
 
 
 
 
 
 
 
 
aea9fdb
b965b34
9bd2d1f
b965b34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c2ffac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bd2d1f
 
 
aea9fdb
 
 
2c2ffac
 
 
 
 
aea9fdb
 
 
2c2ffac
aea9fdb
9bd2d1f
2c2ffac
 
 
aea9fdb
2c2ffac
 
aea9fdb
2c2ffac
 
 
 
 
aea9fdb
2c2ffac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bd2d1f
2c2ffac
 
 
 
 
 
 
 
 
b965b34
2c2ffac
 
 
 
 
 
 
 
 
 
 
b965b34
 
 
9bd2d1f
2c2ffac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bd2d1f
 
 
2c2ffac
aea9fdb
9bd2d1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b965b34
 
 
9bd2d1f
2c2ffac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bd2d1f
 
 
aea9fdb
 
9bd2d1f
 
 
 
 
 
 
 
 
2c2ffac
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import matplotlib.pyplot as plt
import seaborn as sns
import boto3
from io import StringIO
from datetime import timedelta
from pandas.tseries.offsets import MonthEnd
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.stattools import adfuller
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score,classification_report, confusion_matrix
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
import joblib


os.environ['AWS_ACCESS_KEY_ID'] = os.getenv("getdata")
os.environ['AWS_SECRET_ACCESS_KEY'] = os.getenv("getdatake")

def load_data_from_s3(bucket_name, file_key):
    s3 = boto3.client('s3')
    obj = s3.get_object(Bucket=bucket_name, Key=file_key)
    data = pd.read_csv(StringIO(obj['Body'].read().decode('utf-8')))
    return data

st.title('Accomodation Booking Analysis')

st.sidebar.title('Navigation')
options = st.sidebar.radio('Select a page:', ['Overview', 'Revenue Forecasting', 'Predict Booking Cancellations', 'Market Segmentation', 'Customer Lifetime Value'])

if options == 'Overview':
    st.header('Overview')
    st.write('This app provides insights and predictions for hotel bookings.')

elif options == 'Revenue Forecasting':
    st.header('Hotel Booking Revenue Forecasting with SARIMA')

    # Option to choose data source
    data_source = st.radio("Choose data source:", ["Upload CSV", "Load from AWS S3"])

    if data_source == "Upload CSV":
        uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
        if uploaded_file is not None:
            data = pd.read_csv(uploaded_file)
    else:
        bucket_name = st.text_input("Enter S3 bucket name:", "iitj-ap-south-1-mayank")
        file_key = st.text_input("Enter S3 file key:", "clean/hotel_booking/hotel_booking.csv")
        if st.button("Load Data"):
            data = load_data_from_s3(bucket_name, file_key)

    if 'data' in locals():
        # Display the first few rows of the dataset
        st.write("## Dataset Preview")
        st.write(data.head())
        
        data['arrival_date'] = pd.to_datetime(data['arrival_date_year'].astype(str) + '-' + 
                                              data['arrival_date_month'].astype(str) + '-01')
        data['arrival_date'] += MonthEnd(0)
        
        monthly_revenue = data[data['is_canceled'] == 0].groupby('arrival_date')['adr'].sum().reset_index()
        
        st.write("## Monthly Revenue")
        plt.figure(figsize=(12, 6))
        sns.lineplot(x='arrival_date', y='adr', data=monthly_revenue)
        plt.title('Monthly Revenue')
        plt.xlabel('Month')
        plt.ylabel('Revenue')
        plt.xticks(rotation=45)
        plt.tight_layout()
        st.pyplot(plt)

        result = adfuller(monthly_revenue['adr'])
        st.write(f'## ADF Statistic: {result[0]}')
        st.write(f'## p-value: {result[1]}')

        if result[1] > 0.05:
            monthly_revenue['adr_diff'] = monthly_revenue['adr'].diff().dropna()

        p = st.slider('AR order (p)', 0, 5, 1)
        d = st.slider('Differencing order (d)', 0, 2, 1)
        q = st.slider('MA order (q)', 0, 5, 1)
        P = st.slider('Seasonal AR order (P)', 0, 2, 1)
        D = st.slider('Seasonal differencing order (D)', 0, 2, 1)
        Q = st.slider('Seasonal MA order (Q)', 0, 2, 1)

        model = SARIMAX(monthly_revenue['adr'], 
                        order=(p, d, q), 
                        seasonal_order=(P, D, Q, 12))
        model_fit = model.fit(disp=False)

        forecast_steps = 12
        forecast = model_fit.get_forecast(steps=forecast_steps)
        forecast_index = pd.date_range(start=monthly_revenue['arrival_date'].max() + pd.DateOffset(months=1), 
                                       periods=forecast_steps, freq='M')

        forecast_df = pd.DataFrame({'arrival_date': forecast_index, 
                                    'forecast': forecast.predicted_mean})

        st.write("## Revenue Forecast")
        plt.figure(figsize=(12, 6))
        sns.lineplot(x='arrival_date', y='adr', data=monthly_revenue, label='Historical Revenue')
        sns.lineplot(x='arrival_date', y='forecast', data=forecast_df, label='Forecasted Revenue')
        plt.title('Revenue Forecast')
        plt.xlabel('Month')
        plt.ylabel('Revenue')
        plt.xticks(rotation=45)
        plt.legend()
        plt.tight_layout()
        st.pyplot(plt)
        
        st.write("## Forecasted Revenue for the Next 12 Months")
        st.write(forecast_df.set_index('arrival_date'))

elif options == 'Predict Booking Cancellations':
    st.header('Predict Booking Cancellations')

    # Option to choose data source
    data_source = st.radio("Choose data source:", ["Upload CSV", "Load from AWS S3"])

    if data_source == "Upload CSV":
        uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
        if uploaded_file is not None:
            data = pd.read_csv(uploaded_file)
    else:
        bucket_name = st.text_input("Enter S3 bucket name:", "iitj-ap-south-1-mayank")
        file_key = st.text_input("Enter S3 file key:", "clean/hotel_booking/hotel_booking.csv")
        if st.button("Load Data"):
            data = load_data_from_s3(bucket_name, file_key)

    if 'data' in locals():
        # Display the first few rows of the dataset
        st.write("## Dataset Preview")
        st.write(data.head())

        features = ['lead_time', 'arrival_date_year', 'arrival_date_week_number', 
                    'arrival_date_day_of_month', 'stays_in_weekend_nights', 
                    'stays_in_week_nights', 'adults', 'children', 'babies', 
                    'previous_cancellations', 'previous_bookings_not_canceled', 
                    'booking_changes', 'days_in_waiting_list', 'adr', 
                    'required_car_parking_spaces', 'total_of_special_requests']
        
        data = data.dropna(subset=features + ['is_canceled'])
        X = data[features]
        y = data['is_canceled']

        # Split the data into training and testing sets
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

        # Train the Random Forest Classifier
        rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
        rf_model.fit(X_train, y_train)

        # Make predictions
        y_pred = rf_model.predict(X_test)

        # Display model performance metrics
        st.write("## Model Performance Metrics")
        st.write("### Confusion Matrix")
        cm = confusion_matrix(y_test, y_pred)
        st.write(cm)
        
        st.write("### Classification Report")
        cr = classification_report(y_test, y_pred, output_dict=True)
        st.write(pd.DataFrame(cr).transpose())

        st.write("### Accuracy Score")
        acc = accuracy_score(y_test, y_pred)
        st.write(acc)

        # Save the model to a file
        joblib.dump(rf_model, 'rf_model.pkl')
        st.write("Model saved as rf_model.pkl")

        st.write("## Predict Booking Cancellation")
        st.write("Enter the details to predict if a booking will be canceled:")

        # Collect user input for prediction
        input_data = {}
        for feature in features:
            input_data[feature] = st.number_input(f"Enter {feature}:", min_value=0.0)

        if st.button("Predict"):
            input_df = pd.DataFrame([input_data])

            # Ensure the input data has the correct data types
            for feature in features:
                input_df[feature] = input_df[feature].astype(X[feature].dtype)

            prediction = rf_model.predict(input_df)
            prediction_proba = rf_model.predict_proba(input_df)

            st.write(f"Input Data: {input_df}")
            st.write(f"Prediction: {prediction}")
            st.write(f"Prediction Probability: {prediction_proba}")

            if prediction[0] == 1:
                st.write("Prediction: The booking is likely to be canceled.")
            else:
                st.write("Prediction: The booking is not likely to be canceled.")

elif options == 'Market Segmentation':
    st.header('Market Segmentation')

    # Option to choose data source
    data_source = st.radio("Choose data source:", ["Upload CSV", "Load from AWS S3"])

    if data_source == "Upload CSV":
        uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
        if uploaded_file is not None:
            data = pd.read_csv(uploaded_file)
    else:
        bucket_name = st.text_input("Enter S3 bucket name:", "iitj-ap-south-1-mayank")
        file_key = st.text_input("Enter S3 file key:", "clean/hotel_booking/hotel_booking.csv")
        if st.button("Load Data"):
            data = load_data_from_s3(bucket_name, file_key)

    if 'data' in locals():
        # Display the first few rows of the dataset
        st.write("## Dataset Preview")
        st.write(data.head())


        data['total_guests'] = data['adults'] + data['children'] + data['babies']
        segmentation_features = data[['total_guests', 'total_of_special_requests', 'lead_time', 'is_repeated_guest']]
        scaler = StandardScaler()
        segmentation_features_scaled = scaler.fit_transform(segmentation_features)
        
        kmeans = KMeans(n_clusters=4, random_state=42)
        data['customer_segment'] = kmeans.fit_predict(segmentation_features_scaled)
        
        st.write("## Customer Segmentation Results")
        st.write(data[['customer_segment']].head())

        plt.figure(figsize=(10, 5))
        sns.scatterplot(x=segmentation_features_scaled[:, 0], y=segmentation_features_scaled[:, 1], hue=data['customer_segment'], palette='viridis')
        plt.title('Customer Segmentation')
        plt.xlabel('Total Guests (Standardized)')
        plt.ylabel('Total Special Requests (Standardized)')
        st.pyplot(plt)

elif options == 'Customer Lifetime Value':
    st.header('Customer Lifetime Value')

    # Option to choose data source
    data_source = st.radio("Choose data source:", ["Upload CSV", "Load from AWS S3"])

    if data_source == "Upload CSV":
        uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
        if uploaded_file is not None:
            data = pd.read_csv(uploaded_file)
    else:
        bucket_name = st.text_input("Enter S3 bucket name:", "iitj-ap-south-1-mayank")
        file_key = st.text_input("Enter S3 file key:", "clean/hotel_booking/hotel_booking.csv")
        if st.button("Load Data"):
            data = load_data_from_s3(bucket_name, file_key)

    if 'data' in locals():
        # Display the first few rows of the dataset
        st.write("## Dataset Preview")
        st.write(data.head())

        data['customer_id'] = data['lead_time'].astype(str) + '_' + data['arrival_date_year'].astype(str) + '_' + data['arrival_date_month'].astype(str) + '_' + data['arrival_date_day_of_month'].astype(str)
        clv_df = data.groupby('customer_id')['adr'].sum().reset_index()
        clv_df.columns = ['customer_id', 'lifetime_value']
        
        st.write("## Customer Lifetime Value Distribution")
        plt.figure(figsize=(10, 5))
        sns.histplot(clv_df['lifetime_value'], kde=True)
        plt.title('Customer Lifetime Value Distribution')
        plt.xlabel('Lifetime Value')
        plt.ylabel('Frequency')
        st.pyplot(plt)