<jupyter_start><jupyter_text>Business Understanding  Describe the purpose of the data set you selected:&emsp; This data is the collection of two datasets, one which is from a resort hotel and the other which is from a city hotel. The datasets have 40,060 observations and 79,330 obeservations respectively with both sharing 31 variables. The observations time range from July 1st 2015 until August 31st 2017. The data was orginally collected from "Hotel Booking Demand Datasets", written by <NAME>, <NAME>, and <NAME> for Data in Brief, Volume 22, February 2019. The data was consolidated from bookings, change logs, meal, transaction, and customer profiles.    ![title](dataset_origin_image.jpg)   &emsp; The primary purpose for the selection of this dataset is to research revenue management,such as the forecast of demand and prediction of problems such the likelihood of a hotel booking to be cancelled. Additionally this data could be used to generate models to see when is the ideal time to book a hotel room, or which days and for how long could you get the best rates.           Data Understanding Describe the meaning and type of data:&emsp; The full description of the data can be found in https://www.sciencedirect.com/science/article/pii/S2352340918315191:| Variable | Type | Description | Source/Engineering || --- | --- | --- | --- || ADR | Numeric | Average Daily Rate as defined by | BO, BL and TR / Calculated by dividing the sum of all lodging transactions by the total number of staying nights || Adults | Integer | Number of adults | BO and BL || Agent | Categorical | ID of the travel agency that made the bookinga | BO and BL || ArrivalDateDayOfMonth | Integer | Day of the month of the arrival date | BO and BL || ArrivalDateMonth | Categorical | Month of arrival date with 12 categories: “January” to “December”| BO and BL || ArrivalDateWeekNumber	| Integer | Week number of the arrival date | BO and BL || ArrivalDateYear | Integer | Year of arrival date | BO and BL || AssignedRoomType | Categorical | Code for the type of room assigned to the booking. Sometimes the assigned room type differs from the reserved room type due to hotel operation reasons (e.g. overbooking) or by customer request. Code is presented instead of designation for anonymity reasons | BO and BL || Babies | Integer | Number of babies | BO and BL || BookingChanges | Integer | Number of changes/amendments made to the booking from the moment the booking was entered on the PMS until the moment of check-in or cancellation | BO and BL/Calculated by adding the number of unique iterations that change some of the booking attributes, namely: persons, arrival date, nights, reserved room type or meal || Children | Integer | Number of children | BO and BL/Sum of both payable and non-payable children || Company | Categorical | ID of the company/entity that made the booking or responsible for paying the booking. ID is presented instead of designation for anonymity reasons	BO and BL. || Country | Categorical | Country of origin. Categories are represented in the ISO 3155–3:2013 format [6] | BO, BL and NT || CustomerType | Categorical | Type of booking, assuming one of four categories:  Contract - when the booking has an allotment or other type of contract associated to it;  Group – when the booking is associated to a group;  Transient – when the booking is not part of a group or contract, and is not associated to other transient booking;  Transient-party – when the booking is transient, but is associated to at least other transient booking  | BO and BL || DaysInWaitingList | Integer | Number of days the booking was in the waiting list before it was confirmed to the customer | BO/Calculated by subtracting the date the booking was confirmed to the customer from the date the booking entered on the PMS || DepositType | Categorical | Indication on if the customer made a deposit to guarantee the booking. This variable can assume three categories:  No Deposit – no deposit was made;  Non Refund – a deposit was made in the value of the total stay cost;  Refundable – a deposit was made with a value under the total cost of stay. | BO and TR/Value calculated based on the payments identified for the booking in the transaction (TR) table before the booking׳s arrival or cancellation date. || DistributionChannel | Categorical | Booking distribution channel. The term “TA” means “Travel Agents” and “TO” means “Tour Operators” | BO, BL and DC || IsCanceled | Categorical | Value indicating if the booking was canceled (1) or not (0) | BO || IsRepeatedGuest | Categorical | Value indicating if the booking name was from a repeated guest (1) or not (0) | BO, BL and C/ Variable created by verifying if a profile was associated with the booking customer. If so, and if the customer profile creation date was prior to the creation date for the booking on the PMS database it was assumed the booking was from a repeated guest || LeadTime | Integer | Number of days that elapsed between the entering date of the booking into the PMS and the arrival date | BO and BL/ Subtraction of the entering date from the arrival date || MarketSegment | Categorical | Market segment designation. In categories, the term “TA” means “Travel Agents” and “TO” means “Tour Operators” | BO, BL and MS || Meal | Categorical | Type of meal booked. | Categories are presented in standard hospitality meal packages:  Undefined/SC – no meal package;  BB – Bed & Breakfast;  HB – Half board (breakfast and one other meal – usually dinner);  FB – Full board (breakfast, lunch and dinner) | BO, BL and ML || PreviousBookingsNotCanceled | Integer | Number of previous bookings not cancelled by the customer prior to the current booking | BO and BL / In case there was no customer profile associated with the booking, the value is set to 0. Otherwise, the value is the number of bookings with the same customer profile created before the current booking and not canceled. || PreviousCancellations | Integer | Number of previous bookings that were cancelled by the customer prior to the current booking | BO and BL/ In case there was no customer profile associated with the booking, the value is set to 0. Otherwise, the value is the number of bookings with the same customer profile created before the current booking and canceled. || RequiredCardParkingSpaces | Integer | Number of car parking spaces required by the customer | BO and BL || ReservationStatus | Categorical | Reservation last status, assuming one of three categories:  Canceled – booking was canceled by the customer;  Check-Out – customer has checked in but already departed;  No-Show – customer did not check-in and did inform the hotel of the reason why | BO || ReservationStatusDate | Date | Date at which the last status was set. This variable can be used in conjunction with the ReservationStatus to understand when was the booking canceled or when did the customer checked-out of the hotel | BO || ReservedRoomType | Categorical | Code of room type reserved. Code is presented instead of designation for anonymity reasons | BO and BL || StaysInWeekendNights | Integer | Number of weekend nights (Saturday or Sunday) the guest stayed or booked to stay at the hotel | BO and BL/ Calculated by counting the number of weekend nights from the total number of nights || StaysInWeekNights | Integer | Number of week nights (Monday to Friday) the guest stayed or booked to stay at the hotel | BO and BL/Calculated by counting the number of week nights from the total number of nights || TotalOfSpecialRequests | Integer | Number of special requests made by the customer (e.g. twin bed or high floor) | BO and BL/Sum of all special requests |  Verify Data Quality&emsp; Upon initial inspection of the 31 attributes 4 appear to have some missing data. However 3 additional attributes also appear to have missing data but instead of appearing as 'nan' they are labeled as 'Undefined'. Below is a list of all the attributes with missing values and the counts:agent- 16340 company- 112593children- 4country- 488market_segment - 2 meal - 1169distribution_channel - 5 &emsp; There's an association between the distribution channel type and the agent and company attributes. If the distribution channel is from a Traveling Agent or Tour Operator, then the agency company is supplied in the agent column. Similarly if the distribution channel is Corporate, then the company associated with the booking is populated under the company attribute. If the distribution is neither Corporate, TA or TO than both the agent and company attributes will be null. To simplify our data exploration we have decided to remove the agent and company attributes as they don't provide any additional information beyond what already is provided in the distribution channel. For the meal attribute during our data investigation we have found out that 'Undefined' and 'SC' are synonymous so we have decided to replace 'Undefined' with 'SC'. For the remainder they appear to have been ommitted accidentally and we have to decided to populate the missing values with the mode. Additionally, the following attributes were classified as objects, however they're categorical in nature so we have decided to encode each of these attributes with a corresponding number according to their alphabetical order:hotel - ['City Hotel', 'Resort Hotel']market_segment - ['Aviation', 'Complementary', 'Corporate', 'Direct', 'Groups',       'Offline TA/TO', 'Online TA']meal - ['BB', 'FB', 'HB', 'SC']reservation_status - ['Canceled', 'Check-Out', 'No-Show']distribution_channel - ['Corporate', 'Direct', 'GDS', 'TA/TO']reserved_room_type - ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'L', 'P']assigned_room_type - ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'P']deposit_type - ['No Deposit', 'Non Refund', 'Refundable']country - [['ABW','AGO','AIA','ALB','AND', 'ARE', 'ARG', 'ARM', 'ASM', 'ATA', 'ATF', 'AUS', 'AUT', 'AZE', 'BDI', 'BEL', 'BEN', 'BFA', 'BGD', 'BGR', 'BHR', 'BHS', 'BIH', 'BLR', 'BOL', 'BRA', 'BRB', 'BWA', 'CAF', 'CHE', 'CHL', 'CHN', 'CIV', 'CMR', 'CN', 'COL', 'COM', 'CPV', 'CRI', 'CUB', 'CYM', 'CYP', 'CZE', 'DEU', 'DJI', 'DMA', 'DNK', 'DOM', 'DZA', 'ECU', 'EGY', 'ESP', 'EST', 'ETH', 'FIN', 'FJI', 'FRA', 'FRO', 'GAB', 'GBR', 'GEO', 'GGY', 'GHA', 'GIB', 'GLP', 'GNB', 'GRC', 'GTM', 'GUY', 'HKG', 'HND', 'HRV', 'HUN', 'IDN', 'IMN', 'IND', 'IRL', 'IRN', 'IRQ', 'ISL', 'ISR', 'ITA', 'JAM', 'JEY', 'JOR', 'JPN', 'KAZ', 'KEN', 'KHM', 'KIR', 'KNA', 'KOR', 'KWT', 'LAO', 'LBN', 'LBY', 'LCA', 'LIE', 'LKA', 'LTU', 'LUX', 'LVA', 'MAC', 'MAR', 'MCO' 'MDG', 'MDV', 'MEX', 'MKD', 'MLI', 'MLT', 'MMR', 'MNE', 'MOZ', 'MRT', 'MUS', 'MWI', 'MYS', 'MYT', 'NAM', 'NCL', 'NGA', 'NIC', 'NLD', 'NOR', 'NPL', 'NZL', 'OMN', 'PAK', 'PAN', 'PER', 'PHL', 'PLW', 'POL', 'PRI', 'PRT', 'PRY', 'PYF', 'QAT', 'ROU', 'RUS', 'RWA', 'SAU', 'SDN', 'SEN', 'SGP', 'SLE', 'SLV', 'SMR', 'SRB', 'STP', 'SUR', 'SVK', 'SVN', 'SWE', 'SYC', 'SYR', 'TGO', 'THA', 'TJK', 'TMP', 'TUN', 'TUR', 'TWN', 'TZA', 'UGA', 'UKR', 'UMI', 'URY', 'USA', 'UZB', 'VEN', 'VGB', 'VNM', 'ZAF', 'ZMB', 'ZWE']]customer_type - ['Contract', 'Group', 'Transient', 'Transient-Party']    Lastly, for the arrival_date_month we decided to encode according to their correspoding month number in the gregorian calendar and construct two new columns that correspond to the generated timestamp from the concatenated arrival date and the reservation_status_date to help out with regression later on.<jupyter_code>import pandas as pd
import numpy as np

hotel_bookings = pd.read_csv('~/Desktop/SMU/DS 7331 - Machine Learning 1/Lab 1/Data/hotel_bookings.csv')  # read in the csv file

hotel_bookings.head()
hotel_bookings.isnull().sum()
hotel_bookings.hotel.unique()
hotel_bookings.market_segment.unique()
hotel_bookings.meal.unique()
hotel_bookings.reservation_status.unique()
hotel_bookings.distribution_channel.unique()
hotel_bookings.reserved_room_type.unique()
hotel_bookings.assigned_room_type.unique()
hotel_bookings.deposit_type.unique()
hotel_bookings.country.unique()
hotel_bookings.customer_type.unique()
hotel_bookings.loc[hotel_bookings['market_segment'] == 'Undefined']['market_segment'].count()
hotel_bookings.loc[hotel_bookings['meal'] == 'Undefined']['meal'].count()
hotel_bookings.loc[hotel_bookings['distribution_channel'] == 'Undefined']['distribution_channel'].count()

hotel_bookings[['children','country','market_segment','meal','distribution_channel']].mode() 
import math
import numpy as np
hotel_bookings_transform = hotel_bookings
hotel_bookings_transform['children']= hotel_bookings_transform['children'].apply(lambda x: 0 if  math.isnan(x)  else x)
hotel_bookings_transform['country']= hotel_bookings_transform['country'].apply(lambda x: 'PRT' if  pd.isna(x)  else x)
hotel_bookings_transform['market_segment']= hotel_bookings_transform['market_segment'].apply(lambda x: 'Online TA' if  x == 'Undefined'  else x)
hotel_bookings_transform['meal']= hotel_bookings_transform['meal'].apply(lambda x: 'SC' if  x == 'Undefined'  else x)
hotel_bookings_transform['distribution_channel']= hotel_bookings_transform['distribution_channel'].apply(lambda x: 'TA/TO' if  x == 'Undefined'  else x)




# hotel - ['Resort Hotel', 'City Hotel']
# market_segment - ['Direct', 'Corporate', 'Online TA', 'Offline TA/TO',
#        'Complementary', 'Groups', 'Aviation']
# meal - ['BB', 'FB', 'HB', 'SC']
# reservation_status - ['Check-Out', 'Canceled', 'No-Show']
# distribution_channel - ['Direct', 'Corporate', 'TA/TO', 'GDS']
# reserved_room_type - ['C', 'A', 'D', 'E', 'G', 'F', 'H', 'L', 'P', 'B']
# assigned_room_type - ['C', 'A', 'D', 'E', 'G', 'F', 'I', 'B', 'H', 'P', 'L', 'K']
# deposit_type - ['No Deposit', 'Refundable', 'Non Refund']
# country - ['PRT', 'GBR', 'USA', 'ESP', 'IRL', 'FRA', nan, 'ROU', 'NOR', 'OMN',
#        'ARG', 'POL', 'DEU', 'BEL', 'CHE', 'CN', 'GRC', 'ITA', 'NLD',
#        'DNK', 'RUS', 'SWE', 'AUS', 'EST', 'CZE', 'BRA', 'FIN', 'MOZ',
#        'BWA', 'LUX', 'SVN', 'ALB', 'IND', 'CHN', 'MEX', 'MAR', 'UKR',
#        'SMR', 'LVA', 'PRI', 'SRB', 'CHL', 'AUT', 'BLR', 'LTU', 'TUR',
#        'ZAF', 'AGO', 'ISR', 'CYM', 'ZMB', 'CPV', 'ZWE', 'DZA', 'KOR',
#        'CRI', 'HUN', 'ARE', 'TUN', 'JAM', 'HRV', 'HKG', 'IRN', 'GEO',
#        'AND', 'GIB', 'URY', 'JEY', 'CAF', 'CYP', 'COL', 'GGY', 'KWT',
#        'NGA', 'MDV', 'VEN', 'SVK', 'FJI', 'KAZ', 'PAK', 'IDN', 'LBN',
#        'PHL', 'SEN', 'SYC', 'AZE', 'BHR', 'NZL', 'THA', 'DOM', 'MKD',
#        'MYS', 'ARM', 'JPN', 'LKA', 'CUB', 'CMR', 'BIH', 'MUS', 'COM',
#        'SUR', 'UGA', 'BGR', 'CIV', 'JOR', 'SYR', 'SGP', 'BDI', 'SAU',
#        'VNM', 'PLW', 'QAT', 'EGY', 'PER', 'MLT', 'MWI', 'ECU', 'MDG',
#        'ISL', 'UZB', 'NPL', 'BHS', 'MAC', 'TGO', 'TWN', 'DJI', 'STP',
#        'KNA', 'ETH', 'IRQ', 'HND', 'RWA', 'KHM', 'MCO', 'BGD', 'IMN',
#        'TJK', 'NIC', 'BEN', 'VGB', 'TZA', 'GAB', 'GHA', 'TMP', 'GLP',
#        'KEN', 'LIE', 'GNB', 'MNE', 'UMI', 'MYT', 'FRO', 'MMR', 'PAN',
#        'BFA', 'LBY', 'MLI', 'NAM', 'BOL', 'PRY', 'BRB', 'ABW', 'AIA',
#        'SLV', 'DMA', 'PYF', 'GUY', 'LCA', 'ATA', 'GTM', 'ASM', 'MRT',
#        'NCL', 'KIR', 'SDN', 'ATF', 'SLE', 'LAO']
# customer_type - ['Transient', 'Contract', 'Transient-Party', 'Group']

scat = hotel_bookings['country'].astype('category').cat.categories

scat.tolist()
hotel_bookings['hotel'].astype('category').cat.categories
hotel_bookings['market_segment'].astype('category').cat.categories
hotel_bookings['meal'].astype('category').cat.categories
hotel_bookings['reservation_status'].astype('category').cat.categories
hotel_bookings['distribution_channel'].astype('category').cat.categories
hotel_bookings['reserved_room_type'].astype('category').cat.categories
hotel_bookings['assigned_room_type'].astype('category').cat.categories
hotel_bookings['deposit_type'].astype('category').cat.categories
hotel_bookings['customer_type'].astype('category').cat.categories
hotel_bookings_transform['hotel']=hotel_bookings_transform['hotel'].astype('category').cat.codes
hotel_bookings_transform['market_segment']=hotel_bookings_transform['market_segment'].astype('category').cat.codes
hotel_bookings_transform['meal']=hotel_bookings_transform['meal'].astype('category').cat.codes
hotel_bookings_transform['reservation_status']=hotel_bookings_transform['reservation_status'].astype('category').cat.codes
hotel_bookings_transform['distribution_channel']=hotel_bookings_transform['distribution_channel'].astype('category').cat.codes
hotel_bookings_transform['reserved_room_type']=hotel_bookings_transform['reserved_room_type'].astype('category').cat.codes
hotel_bookings_transform['assigned_room_type']=hotel_bookings_transform['assigned_room_type'].astype('category').cat.codes
hotel_bookings_transform['deposit_type']=hotel_bookings_transform['deposit_type'].astype('category').cat.codes
hotel_bookings_transform['customer_type']=hotel_bookings_transform['customer_type'].astype('category').cat.codes
hotel_bookings_transform= hotel_bookings_transform.drop(columns=['agent','company'])
hotel_bookings_transform
assign_month = {"arrival_date_month": {"January": 1, "February": 2, "March": 3, "April": 4,"May": 5,"June": 6,"July": 7,"August": 8,"September": 9,"October": 10,"November": 11,"December": 12}}
#assign_cust = {"customer_type": {"Transient": 1, "Contract": 2, "Transient-Party": 3, "Group": 4 }}
hotel_bookings_transform = hotel_bookings_transform.replace(assign_month)
hotel_bookings_transform.head()



#TODO: modify this code with hotel_bookings
hotel_bookings_transform=hotel_bookings_transform.assign(booking_date = hotel_bookings_transform["arrival_date_year"].astype(str) + '-' + 
hotel_bookings_transform["arrival_date_month"].astype(str) + '-' +hotel_bookings_transform["arrival_date_day_of_month"].astype(str))
hotel_bookings_transform['booking_date']=pd.to_datetime(hotel_bookings_transform.booking_date)
hotel_bookings_transform['booking_date']
hotel_bookings_transform.isnull().sum()
hotel_bookings.mode()
hotel_bookings.loc[:,"agent"].mode()
hotel_bookings.loc[:,"company"].mode()
hotel_bookings.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 119390 entries, 0 to 119389
Data columns (total 32 columns):
 #   Column                          Non-Null Count   Dtype  
---  ------                          --------------   -----  
 0   hotel                           119390 non-null  int8   
 1   is_canceled                     119390 non-null  int64  
 2   lead_time                       119390 non-null  int64  
 3   arrival_date_year               119390 non-null  int64  
 4   arrival_date_month              119390 non-null  object 
 5   arrival_date_week_number        119390 non-null  int64  
 6   arrival_date_day_of_month       119390 non-null  int64  
 7   stays_in_weekend_nights         119390 non-null  int64  
 8   stays_in_week_nights            119390 non-null  int64  
 9   adults                          119390 non-null  int64  
 10  children                        119390 non-null  float64
 11  babies                          119390 non-null  int64  
 12  meal            [...]<jupyter_text>Simple Stastics&emsp; Below you'll see some simple stastics for each and everyone of the values generated by the describe function. We decided to do check outliers for all the non-categorical values. As you'll  Categoriacal Values:<jupyter_code>hotel_bookings_transform.describe()
hotel_bookings_transform.head()
from scipy import stats

hotel_bookings_transform[(np.abs(stats.zscore(hotel_bookings_transform[['adr']])) > 3).all(axis=1)]['adr']

hotel_bookings_transform[(np.abs(stats.zscore(hotel_bookings_transform[['lead_time']])) > 3).all(axis=1)]['lead_time']
hotel_bookings_transform[(np.abs(stats.zscore(hotel_bookings_transform[['adults']])) > 3).all(axis=1)]['adults']
hotel_bookings_transform[(np.abs(stats.zscore(hotel_bookings_transform[['previous_cancellations']])) > 3).all(axis=1)]['previous_cancellations']
hotel_bookings_transform[(np.abs(stats.zscore(hotel_bookings_transform[['booking_changes']])) > 3).all(axis=1)]['booking_changes']
hotel_bookings_transform[(np.abs(stats.zscore(hotel_bookings_transform[['days_in_waiting_list']])) > 3).all(axis=1)]['days_in_waiting_list']
hotel_bookings_transform[(np.abs(stats.zscore(hotel_bookings_transform[['total_of_special_requests']])) > 3).all(axis=1)]['total_of_special_requests']
hotel_bookings_transform[(np.abs(stats.zscore(hotel_bookings_transform[['required_car_parking_spaces']])) > 3).all(axis=1)]['required_car_parking_spaces']
import matplotlib.pyplot as plt
import warnings
warnings.simplefilter('ignore', DeprecationWarning)
%matplotlib inline
plt.style.use('ggplot')
fig=plt.figure(figsize=(12,12), dpi= 100, facecolor='w', edgecolor='k')

df_grouped = hotel_bookings.groupby(by=['arrival_date_year','arrival_date_week_number'])
visit_by_week = df_grouped.hotel.count()
b = visit_by_week.plot(kind='bar')
import matplotlib.pyplot as plt
import warnings
warnings.simplefilter('ignore', DeprecationWarning)
%matplotlib inline
plt.style.use('ggplot')
fig=plt.figure(figsize=(12,30), dpi= 100, facecolor='w', edgecolor='k')

hotel_bookings2 = hotel_bookings
hotel_bookings2["arrival_date_year"] = hotel_bookings["arrival_date_year"].astype(str)
hotel_bookings2["arrival_date_week_number"] = hotel_bookings["arrival_date_week_number"].astype(str)

hotel_bookings2["week-date"] = hotel_bookings["arrival_date_year"].str.cat(hotel_bookings["arrival_date_week_number"],sep="-")
hotel_bookings2

#f = hotel_bookings2.groupby(by=['week-date','customer_type'])
hotel_bookings2[["week-date","customer_type","hotel"]]
f = hotel_bookings2.groupby(by=['week-date','customer_type'])
f.hotel.count()

f2 = hotel_bookings2.groupby(by=['customer_type'])

f2.hotel.count()


# sample = pd.crosstab([ hotel_bookings2['week-date'],hotel_bookings2['customer_type'] ], 
#                        hotel_bookings2.hotel.astype(bool))
# sample



#date_contract.info()
#f.groups()
#res = f.pivot(index='week-date', columns='category', values='quantity')


hotel_bookings.hist(figsize=(14,14), xrot=45)
hotel_bookings2= hotel_bookings
hotel_bookings2["arrival_date_year"] = hotel_bookings["arrival_date_year"].astype(str)
hotel_bookings2["arrival_date_week_number"] = hotel_bookings["arrival_date_month"].astype(str)
hotel_bookings2["arrival_date_day_of_month"] = hotel_bookings["arrival_date_day_of_month"].astype(str)

hotel_bookings2["booking-date-temp"] = hotel_bookings["arrival_date_year"].str.cat(hotel_bookings["arrival_date_month"],sep="-")

hotel_bookings2["booking-date"]  = hotel_bookings["booking-date-temp"].str.cat(hotel_bookings["arrival_date_day_of_month"],sep="-")

hotel_bookings2.head()



##iso timestamp/ unix timestamp

hotel_bookings[["market_segment","distribution_channel","reservation_status","reservation_status_date"]]

hotel_bookings.reservation_status.unique()
hotel_bookings.hotel.unique()
#customer type encoded
hotel_bookings.customer_type.unique()
hotel_bookings.market_segment.unique()

hotel_bookings.distribution_channel.unique()
hotel_bookings.reserved_room_type.unique()
hotel_bookings.assigned_room_type.unique()
#find if room type changed
hotel_bookings.deposit_type.unique()
hotel_bookings.agent.unique()
hotel_bookings.company.unique()
hotel_bookings.meal.unique()
hotel_bookings.country.unique()
import statsmodels.api as sm

hotel_bookings['hotel'] = hotel_bookings['hotel'].astype('category')
hotel_bookings['arrival_date_month'] = hotel_bookings['arrival_date_month'].astype('category')

Xtrain = hotel_bookings[['lead_time','arrival_date_year','arrival_date_month','arrival_date_week_number','stays_in_weekend_nights','stays_in_week_nights']]

Ytrain = hotel_bookings[['is_canceled']]

#hotel, arrival_date_month change to category
#replace nans of children


# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 119390 entries, 0 to 119389
# Data columns (total 32 columns):
#  #   Column                          Non-Null Count   Dtype  
# ---  ------                          --------------   -----  
#  0   hotel                           119390 non-null  object 
#  1   is_canceled                     119390 non-null  int64  
#  2   lead_time                       119390 non-null  int64  
#  3   arrival_date_year               119390 non-null  int64  
#  4   arrival_date_month              119390 non-null  object 
#  5   arrival_date_week_number        119390 non-null  int64  
#  6   arrival_date_day_of_month       119390 non-null  int64  
#  7   stays_in_weekend_nights         119390 non-null  int64  
#  8   stays_in_week_nights            119390 non-null  int64  
#  9   adults                          119390 non-null  int64  
#  10  children                        119386 non-null  float64
#  11  babies                          119390 non-null  int64  
#  12  meal                            119390 non-null  object 
#  13  country                         118902 non-null  object 
#  14  market_segment                  119390 non-null  object 
#  15  distribution_channel            119390 non-null  object 
#  16  is_repeated_guest               119390 non-null  int64  
#  17  previous_cancellations          119390 non-null  int64  
#  18  previous_bookings_not_canceled  119390 non-null  int64  
#  19  reserved_room_type              119390 non-null  object 
#  20  assigned_room_type              119390 non-null  object 
#  21  booking_changes                 119390 non-null  int64  
#  22  deposit_type                    119390 non-null  object 
#  23  agent                           103050 non-null  float64
#  24  company                         6797 non-null    float64
#  25  days_in_waiting_list            119390 non-null  int64  
#  26  customer_type                   119390 non-null  object 
#  27  adr                             119390 non-null  float64
#  28  required_car_parking_spaces     119390 non-null  int64  
#  29  total_of_special_requests       119390 non-null  int64  
#  30  reservation_status              119390 non-null  object 
#  31  reservation_status_date         119390 non-null  object 
# dtypes: float64(4), int64(16), object(12)
# memory usage: 29.1+ MB

#hotel encode
#arrival_date_month
#customer type encoded
#reservation_status encoded
#reservation_status_date 

#replace agent with binary value
#replace company with binary value



log_reg = sm.Logit(hotel_bookings['is_canceled'], sm.add_constant(hotel_bookings[['arrival_date_year']+['lead_time']
                                                                                 +['arrival_date_week_number']
                                                                                 +['stays_in_weekend_nights']
                                                                                 +['arrival_date_day_of_month']
                                                                                 +['stays_in_week_nights']
                                                                                 +['adults']+['babies']
                                                                                 +['is_repeated_guest']
                                                                                 +['previous_cancellations']
                                                                                 +['previous_bookings_not_canceled']
                                                                                 +['booking_changes']
                                                                                +['days_in_waiting_list']
                                                                                 +['adr']
                                                                                 +['required_car_parking_spaces']
                                                                                 +['total_of_special_requests']
                                                                                ])).fit()

log_reg.summary2()
#hotel_bookings.info()


#log_reg.aic






log_reg = sm.Logit(hotel_bookings['is_canceled'], sm.add_constant(hotel_bookings[['arrival_date_year']+['lead_time']
                                                                                 +['arrival_date_week_number']
                                                                                 +['stays_in_weekend_nights']
                                                                                 +['arrival_date_day_of_month']
                                                                                 +['stays_in_week_nights']
                                                                                 +['adults']+['babies']
                                                                                 +['is_repeated_guest']
                                                                                 +['previous_cancellations']
                                                                                 +['previous_bookings_not_canceled']
                                                                                 +['booking_changes']
                                                                                +['days_in_waiting_list']
                                                                                 +['adr']
#                                                                                  +['required_car_parking_spaces']
                                                                                 +['total_of_special_requests']
                                                                                ])).fit()
log_reg.aic
log_reg.summary2()



hotel_bookings['hotel'] = hotel_bookings['hotel'].astype('category')
#hotel_bookings['arrival_date_month'] = hotel_bookings['arrival_date_month'].astype('category')

hotel_bookings['hotel']  = hotel_bookings['hotel'].cat.codes



#hotel_bookings['arrival_date_month'].cat.categories
hotel_bookings['agent']
import math
hotel_bookings['agent']
x = math.nan
if math.isnan(x) :
    print("true")
else :
    print("false")

hotel_bookings['agent']= hotel_bookings['agent'].apply(lambda x: False if math.isnan(x) else True)

hotel_bookings['agent']
hotel_bookings['company']= hotel_bookings['company'].apply(lambda x: False if math.isnan(x) else True)
hotel_bookings['company']

hotel_bookings['agent'] = hotel_bookings['agent'].astype('category').cat.codes
hotel_bookings['agent']
hotel_bookings['company'] = hotel_bookings['company'].astype('category').cat.codes
hotel_bookings['company']
hotel_bookings['market_segment']= hotel_bookings['market_segment'].astype('category').cat.codes
hotel_bookings['market_segment']
hotel_bookings['meal']= hotel_bookings['meal'].astype('category').cat.codes
hotel_bookings['meal']
hotel_bookings['hotel']

log_reg = sm.Logit(hotel_bookings['is_canceled'], sm.add_constant(hotel_bookings[['hotel']
                                                                                 +['arrival_date_year']
                                                                                 +['lead_time']
                                                                                 +['agent']
                                                                                 +['company']
                                                                                 +['market_segment']
                                                                                 +['meal']
                                                                                 +['arrival_date_week_number']
                                                                                 +['stays_in_weekend_nights']
                                                                                 +['arrival_date_day_of_month']
                                                                                 +['stays_in_week_nights']
                                                                                 +['adults']+['babies']
                                                                                 +['is_repeated_guest']
                                                                                 +['previous_cancellations']
                                                                                 +['previous_bookings_not_canceled']
                                                                                 +['booking_changes']
                                                                                +['days_in_waiting_list']
                                                                                 +['adr']
                                                                                 +['required_car_parking_spaces']
                                                                                 +['total_of_special_requests']
                                                                                ])).fit()

log_reg.summary2()
#hotel_bookings.info()



#TODO: modify this code with hotel_bookings
=


# Concatenate three columns into one column and iso timestamp
#TODO: modify this code with hotel_bookings
hotel_bookings=hotel_bookings.assign(booking_date = hotel_bookings["arrival_date_year"].astype(str) + '-' + 
hotel_bookings["arrival_date_month"].astype(str) + '-' +hotel_bookings["arrival_date_day_of_month"].astype(str))
hotel_bookings['booking_date']=pd.to_datetime(hotel_bookings.booking_date)
hotel_bookings['booking_date']<jupyter_output><empty_output><jupyter_text>https://jakevdp.github.io/PythonDataScienceHandbook/05.09-principal-component-analysis.html<jupyter_code>hotel_bookings.company.unique()

hotel_bookings.distribution_channel.unique()
hotel_bookings.children.unique()

hotel_bookings.loc[hotel_bookings['company'] == 270]['distribution_channel']

hotel_bookings.agent.unique()

hotel_bookings.loc[hotel_bookings['company'] == 240]['distribution_channel']
hotel_bookings.distribution_channel.unique()

hotel_bookings['country'].mode() 
hotel_bookings['country'].unique()
hotel_bookings['children'].mode()
import pandas as pd
import numpy as np

hotel_bookings = pd.read_csv('~/Desktop/SMU/DS 7331 - Machine Learning 1/Lab 1/Data/hotel_bookings.csv')  # read in the csv file
del hotel_bookings['agent']
del hotel_bookings['country']
del hotel_bookings['company']<jupyter_output><empty_output><jupyter_text>Heatmap for both categorical and numerical variables<jupyter_code>from dython.nominal import associations

#hotel_bookings.loc[:, hotel_bookings.dtypes == 'object'] =\
#    hotel_bookings.select_dtypes(['object'])\
#    .apply(lambda x: x.astype('category'))
hotel_bookings.info()
import matplotlib.pyplot as plt
figure.figsize=associations(hotel_bookings, nominal_columns=['hotel','is_canceled','arrival_date_year',
                                                      'arrival_date_month','arrival_date_week_number',
                                                      'is_repeated_guest','arrival_date_day_of_month',
                                                      'distribution_channel','meal','market_segment',
                                                      'reservation_status_date','week-date', 
                                                     'previous_cancellations','reserved_room_type','deposit_type',
                                                     'market_segment','meal', 'assigned_room_type','customer_type',
                                                     'reservation_status','booking_date','booking-date'])
plt.rcParams['figure.figsize'] = [20, 20]<jupyter_output><empty_output><jupyter_text>Dimensionality reduction using Factor analysis of mixed data (FAMD) This technique is useful for a datset that contains both categorical and numerical features.<jupyter_code>#import pandas as pd
#import numpy as np

#hotel_bookings = pd.read_csv('~/Desktop/SMU/DS 7331 - Machine Learning 1/Lab 1/Data/hotel_bookings.csv')  # read in the csv file
import pandas as pd
import prince
import pprint
from prince import FAMD
# delete features with any missing values : 'country','agent' and 'company' 
# del hotel_bookings['country']
# del hotel_bookings['agent']
# del hotel_bookings['company']
# Encoding some features
hotel_bookings["is_canceled"]=hotel_bookings["is_canceled"].astype(str)
hotel_bookings["is_repeated_guest"]=hotel_bookings["is_repeated_guest"].astype(str)
hotel_bookings["distribution_channel"]=hotel_bookings["distribution_channel"].astype(str)
hotel_bookings["previous_cancellations"]=hotel_bookings["previous_cancellations"].astype(str)
hotel_bookings["reserved_room_type"]=hotel_bookings["reserved_room_type"].astype(str)
hotel_bookings["deposit_type"]=hotel_bookings["deposit_type"].astype(str)
hotel_bookings["market_segment"]=hotel_bookings["market_segment"].astype(str)
hotel_bookings["meal"]=hotel_bookings["meal"].astype(str)
hotel_bookings["assigned_room_type"]=hotel_bookings["assigned_room_type"].astype(str)
hotel_bookings["customer_type"]=hotel_bookings["customer_type"].astype(str)
hotel_bookings["reservation_status"]=hotel_bookings["reservation_status"].astype(str)
hotel_bookings["booking_date"]=hotel_bookings["booking_date"].astype(str)
# Turning all the categorical features into object features for FAMD processing

hotel_bookings.loc[:, hotel_bookings.dtypes == 'category'] =\
    hotel_bookings.select_dtypes(['category'])\
    .apply(lambda x: x.astype('object'))

hotel_bookings.info()
import time
# Instantiate FAMD object
famd = FAMD(n_components =29, n_iter = 5, random_state = 101)


## Fit FAMD to the data
famd = famd.fit(hotel_bookings.drop('is_canceled', axis=1)) # Exclude target variable "is_canceled"
famd.transform(hotel_bookings)
# FAMD plot
ax = famd.plot_row_coordinates(hotel_bookings,color_labels=['is_canceled {}'.format(t) for t in hotel_bookings['is_canceled']] )
## Inspect principal dimensions
pp = pprint.PrettyPrinter()
pp.pprint(famd.explained_inertia_) 
famd.partial_row_coordinates(hotel_bookings)
ax = famd.plot_partial_row_coordinates(hotel_bookings, color_labels=['is_canceled {}'.format(t) for t in tips['is_canceled']])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11981082501313715, 'fraction_numerical': 0.016868102995270626, 'mean_word_length': 2.989622641509434, 'pattern_counts': {'":': 18, '<': 25, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 163, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '61768', 'license': 'MIT', 'n_tokens_mistral': 11660, 'n_tokens_neox': 10926, 'n_words': 2930, 'path': 'lab1-backup-files/Lab_1_notebook_pca.ipynb', 'repo_name': 'olmosjorge28/DS7331_ML1_ASSINGMENTS', 'size': '38074'}