Arya99/Predict_Reservation_Cancellation
0
1import streamlit as st2import pandas as pd3import numpy as np4import matplotlib.pyplot as plt5import plotly.express as px6from PIL import Image7import joblib8import json9import datetime10import pickle11from CustomTransformer import OutletTypeEncoder12 13# Load All Files14with open('model_decision_tree.pkl', 'rb') as file_1:15 dc = joblib.load(file_1)16 17with open('model_scaler_skewed.pkl', 'rb') as file_2:18 scaler_skewed = joblib.load(file_2)19 20with open('model_scaler_normal.pkl', 'rb') as file_3:21 scaler_normal = joblib.load(file_3)22 23with open('model_encoder.pkl', 'rb') as file_4:24 encoder = joblib.load(file_4)25 26with open('list_num_normal.txt', 'r') as file_5:27 normal_dist = json.load(file_5)28 29with open('list_num_skewed.txt', 'r') as file_6:30 skewed_dist = json.load(file_6)31 32with open('list_cat_cols.txt', 'r') as file_7:33 cat_columns = json.load(file_7)34 35with open('list_drop_cols.txt', 'r') as file_8:36 kolom_drop = json.load(file_8)37 38num_n_pipeline = pickle.load(open('num_n_pipeline.pkl', 'rb'))39 40num_s_pipeline = pickle.load(open('num_s_pipeline.pkl', 'rb'))41 42preprocess_pipeline = pickle.load(open('preprocess_pipeline.pkl', 'rb'))43 44model_pipeline = pickle.load(open('model_pipeline.pkl', 'rb'))45 46 47 48def run(): # Agar bisa dipanggil oleh main.49 # Membuat Form50 with st.form(key='form_data_konsumen'):51 52 country = st.text_input('Negara Asal', max_chars=3, help= 'Di isi menggunakan standard ALpha-3 Code ISO-3166-1 (ref = `https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes`) ')53 customer_type = st.selectbox('Tipe Reservasi',('Contract','Group','Transient','Transient-party'), help = 'Contract = Bila tipe reservasi nya ada sebuah kontrak khusus, Group = Reservasi sekelompok orang, Transient = Booking yang tidak masuk ke kontrak atau group, Transient-party = Sekelompok reservasi tipe transient. ' )54 deposit_type = st.selectbox('Jenis Pembayaran Deposit', ('No Deposit','Non Refund', 'Refundable'), help = 'No Deposit = Konsumen belum membayar, Non Refund = Deposit telah dibayar sesuai dengan harga inap, Refundable = Deposit telah dibayar namun masih dibawah harga inap.')55 hotel = st.selectbox('Jenis Hotel', ('Resort Hotel','City Hotel')) # Value = nilai default56 is_repeated_guest = st.selectbox('Pernah Menginap di Hotel', (1 , 0) , help='1 = Pernah Menginap, 0 = Belum Pernah Menginap')57 meal = st.selectbox('Paket meal yang dipesan', ('SC','BB','HB','FB'), help = 'SC = Hanya pesan kamar, BB = Breakfast, HB = Breakfast + Lunch/Dinner, FB = Full Meal')58 market_segment = st.selectbox('Segmentasi Pasar Hotel', ('Groups','Corporate','Offline TA/TO', 'Online TA','Direct','Complementary','Aviation','Undefined'), help = 'TA = Travel Agents, TO = Tour Operators')59 distribution_channel = st.selectbox('Channel distribusi', ('TA/TO','Corporate','Dircet','GDS','Undefined'), help = 'TA = Travel Agents, TO = Tour Operators')60 reservation_status = st.selectbox('Status Reservasi', ('Canceled','Check-Out','No-Show'), help = 'Canceled = Reservasi dibatalkan, Check-Out = Customer sudah check out, No-Show = Tidak ada kabar')61 reservation_status_date = st.date_input('Tanggal Input Status Reservasi', value=datetime.date(2022,11,25))62 63 st.markdown('---')64 65 adults = st.number_input('Jumlah Orang Dewasa', min_value=1, max_value=99, value=1)66 children = st.number_input('Jumlah Anak-anak', min_value=0, max_value=99, value=0)67 babies = st.number_input('Jumlah Bayi', min_value=0, max_value=99, value=0)68 69 70 st.markdown('---')71 72 agent = st.number_input('Agent ID', min_value = 0, max_value = 999, value = 0, help = '0 = Bukan dari Agent')73 company = st.number_input('Company ID', min_value = 0, max_value = 999, value = 0, help = '0 = Bukan dari Perusahaan')74 days_in_waiting_list = st.number_input('Jumlah Hari di Waiting List', min_value = 0, max_value =999) 75 reserved_room_type = st.selectbox('Jenis Kamar Reservasi', ('A','B','C','D','E','F','G','H','L','P'), help = 'Tidak dicantumkan deskripsi untuk privasi customer')76 assigned_room_type = st.selectbox('Jenis Kamar yang Didapat', ('A','B','C','D','E','F','G','H','I','K','L','P'), help = 'Tidak dicantumkan deskripsi untuk privasi customer')77 78 79 st.markdown('---')80 81 adr = st.number_input('Tarif harian kamar rata-rata ', min_value=0, max_value = 999, value = 100)82 lead_time = st.number_input('Lead Time', min_value=0, max_value=700, value=14, step=1, help='Jarak hari dari mulai reservasi hingga tanggal kedatangan') # Help deskripsi kolom form83 arrival_date_year = st.number_input('Tahun Kedatangan', min_value=2000, max_value=2090, value=2022, help = 'Tahun kedatangan')84 arrival_date_month = st.selectbox('Bulan Kedatangan',('January','February','March','April','May','June','July','August','September','October','November','December'), help = 'Bulan Kedatangan')85 arrival_date_week_number = st.number_input('Minggu Kedatangan',min_value=1, max_value=52, value = 1, help = 'Minggu Kedatangan')86 arrival_date_day_of_month = st.number_input('Hari Kedatangan', min_value=1, max_value=3, value=1, help = 'Hari Kedatangan')87 booking_changes = st.number_input('Jumlah perubahan pada reservasi pada satu transaksi', min_value=0, max_value=99, value=0)88 previous_cancellations = st.number_input('Riwayat Jumlah Reservasi yang Pernah Dibatalkan', min_value=0, max_value=99, value=0)89 previous_bookings_not_canceled = st.number_input('Riwayat Jumlah Reservasi yang tidak Dibatalkan', min_value= 0, max_value = 99, value = 0)90 required_car_parking_spaces = st.number_input('Kebutuhan Lahan Parkir', min_value= 0, max_value=10, value = 0)91 stays_in_weekend_nights = st.number_input('Jumlah Hari Weekend yang direservasi', min_value=0, max_value= 2, value=0)92 stays_in_week_nights = st.number_input('Jumlah Hari Weekday yang direservasi', min_value=0, max_value=5, value=0) 93 total_of_special_requests = st.number_input('Jumlah Permintaan Khusus', min_value=0, max_value=10, value=0)94 95 # Tombol submit96 submitted = st.form_submit_button('Predict')97 98 99 100 data_inf = { # Label harus sama dengan CSV, variabel bebas.101 'hotel' : hotel,102 'lead_time' : lead_time,103 'arrival_date_year' : arrival_date_year,104 'arrival_date_month' : arrival_date_month,105 'arrival_date_week_number' : arrival_date_week_number,106 'arrival_date_day_of_month' : arrival_date_day_of_month,107 'stays_in_weekend_nights' : stays_in_weekend_nights,108 'stays_in_week_nights' : stays_in_week_nights,109 'adults' : adults,110 'children' : children,111 'babies' : babies,112 'meal' : meal,113 'country' : country,114 'market_segment' : market_segment,115 'distribution_channel' : distribution_channel,116 'is_repeated_guest' : is_repeated_guest,117 'previous_cancellations' : previous_cancellations,118 'previous_bookings_not_canceled' : previous_bookings_not_canceled,119 'reserved_room_type' : reserved_room_type,120 'assigned_room_type' : assigned_room_type,121 'booking_changes' : booking_changes,122 'deposit_type' : deposit_type,123 'agent' : agent,124 'company' : company,125 'days_in_waiting_list' : days_in_waiting_list,126 'customer_type' : customer_type,127 'adr' : adr,128 'required_car_parking_spaces' : required_car_parking_spaces,129 'total_of_special_requests' : total_of_special_requests,130 'reservation_status' : reservation_status,131 'reservation_status_date' : reservation_status_date132 133 }134 135 data_inf = pd.DataFrame([data_inf])136 st.dataframe(data_inf)137 138 if submitted:139 140 # Predict Menggunakan Pipeline141 y_pred_inf = model_pipeline.predict(data_inf)142 143 if y_pred_inf == 1 :144 st.write('Konsumen akan membatalkan reservasi')145 else :146 st.write('Konsumen tidak akan membatalkan reservasi')147 148if __name__ == '__main__': # Agar python bisa dibuka standalone tanpa membuka main.149 run()