<jupyter_start><jupyter_text>1. Business Understanding Cancellations are part of the everyday life of hotels. For some time now, hotels have been charging no/barely any cancellation fees. This increasingly attracts customers to make bookings that are then not taken at the last second.Is it possible to use machine learning approaches to determine the size by given parameters and to predict the cancellations by overbooking hotels accordingly in order to fill the hotel nonetheless in case of cancellations that are not perceived?  2. Data and Data Understanding The data sets contain all kinds of data recorded about the guests. Characteristics regarding children, bookings via travel agencies, etc. could provide information on whether they have a higher cancellation rate.  2.1. Import of Relevant Modules<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.metrics import confusion_matrix, classification_report<jupyter_output><empty_output><jupyter_text>2.2. Read Data<jupyter_code># my_file = project.get_file()

# my_file.seek(0)
df = pd.read_csv("https://storage.googleapis.com/ml-service-repository-datastorage/Prediction_cancellation_of_hotel_bookings_data.csv")

df.head()
def attribute_description(data):
    longestColumnName = len(max(np.array(data.columns), key=len))
    for col in data.columns:
        description = ''
        col_dropna = data[col].dropna()
        example = col_dropna.sample(1).values[0]
        if type(example) == str:
            description = 'str '
            if len(col_dropna.unique()) < 10:
                description += '['
                description += '; '.join([ f'"{name}"' for name in col_dropna.unique()])
                description += ']'
            else:
                description += '[ example: "'+ example + '" ]'
        else:
            description = str(type(example))
        print(col.ljust(longestColumnName)+ f':   {description}')
attribute_description(df)
df.describe(include='all')<jupyter_output><empty_output><jupyter_text>2.3. Data Cleaning<jupyter_code>f,ax=plt.subplots(figsize = (18,18))
sns.heatmap(df.corr(),annot= True,linewidths=0.5,fmt = ".1f",ax=ax)
plt.xticks(rotation=90)
plt.yticks(rotation=0)
plt.title('Correlation Map')
plt.show()
df.isnull().sum()
df = df.drop(['reservation_status'], axis=1)
df = df.drop(['stays_in_weekend_nights'], axis=1)
df = df.drop(['reservation_status_date'], axis=1)
df = df.drop(['arrival_date_day_of_month'], axis=1)
df = df.drop(['arrival_date_year'], axis=1)
df = df.drop(['arrival_date_month'], axis=1)
df = df.drop(['arrival_date_week_number'], axis=1)
df = df.drop(['required_car_parking_spaces'], axis=1)
df = df.drop(['previous_bookings_not_canceled'], axis=1)
df = df.drop(['total_of_special_requests'], axis=1)
df = df.drop(['agent'], axis=1)
df = df.drop(['company'], axis=1)
df = df.drop(['adr'], axis=1)
df = df.dropna(axis=0)<jupyter_output><empty_output><jupyter_text>2.4. Test for Multicollinearity<jupyter_code>from statsmodels.stats.outliers_influence import variance_inflation_factor
variables = df[['lead_time', 'is_repeated_guest', 'adults', 'booking_changes', 'previous_cancellations', 'is_canceled', 'stays_in_week_nights', 'babies', 'days_in_waiting_list']]
vif = pd.DataFrame()
vif['VIF'] = [variance_inflation_factor(variables.values, i) for i in range(variables.shape[1])]
vif['Features'] = variables.columns
vif<jupyter_output><empty_output><jupyter_text>2.5. Descriptive Analysis<jupyter_code>df.hist(figsize=(25,25), bins=50)<jupyter_output><empty_output><jupyter_text>3. Data Preparation  3.1. Recoding of Categorical Variables<jupyter_code>df_dummies = pd.get_dummies(df, drop_first=True) # 0-1 encoding for categorical values
df_dummies.head()
#df_dummies.to_csv('train_dummies.csv', index = False) 
df_dummies.axes[0]
df_dummies.axes[1]<jupyter_output><empty_output><jupyter_text>4. Modelling and Evaluation  4.1. Test and Train Data<jupyter_code>target = df_dummies['is_canceled'] # feature to be predicted
predictors = df_dummies.drop(['is_canceled'], axis = 1) # all other features are used as predictors
predictors.head()
X_train, X_test, y_train, y_test = train_test_split(predictors, target, test_size=0.2, random_state=123)<jupyter_output><empty_output><jupyter_text>4.2. DecisionTree<jupyter_code>tree = DecisionTreeClassifier()
tree.fit(X_train, y_train)
tn, fp, fn, tp = confusion_matrix(y_test, tree.predict(X_test)).ravel() 
print(tn, fp, fn, tp)
print(classification_report(y_train, tree.predict(X_train)))
print(classification_report(y_test, tree.predict(X_test)))<jupyter_output>precision    recall  f1-score   support

           0       0.85      0.86      0.86     15024
           1       0.76      0.74      0.75      8756

    accuracy                           0.82     23780
   macro avg       0.80      0.80      0.80     23780
weighted avg       0.82      0.82      0.82     23780<jupyter_text>4.3. Logistic Regression<jupyter_code>logreg = LogisticRegression()
logreg.fit(X_train, y_train)
print(confusion_matrix(y_test, logreg.predict(X_test)))
conf_mat = confusion_matrix(y_test, logreg.predict(X_test))
df_cm = pd.DataFrame(conf_mat, index=['0','1'], columns=['0', '1'],)
fig = plt.figure(figsize=[10,7])
heatmap = sns.heatmap(df_cm, annot=True, fmt="d")
heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right', fontsize=14)
heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right', fontsize=14)
plt.ylabel('True label')
plt.xlabel('Predicted label')
print(classification_report(y_test, logreg.predict(X_test)))
print(classification_report(y_train, logreg.predict(X_train)))<jupyter_output>precision    recall  f1-score   support

           0       0.78      0.91      0.84     59721
           1       0.78      0.56      0.66     35397

    accuracy                           0.78     95118
   macro avg       0.78      0.74      0.75     95118
weighted avg       0.78      0.78      0.77     95118<jupyter_text>4.4. Random Forest<jupyter_code>tree_depth = [5, 10, 20]
for i in tree_depth:
    rf = RandomForestClassifier(max_depth=i)
    rf.fit(X_train, y_train)
    print('Max tree depth: ', i)
    print('Train results: ', classification_report(y_train, rf.predict(X_train)))
    print('Test results: ',classification_report(y_test, rf.predict(X_test)))
rf = RandomForestClassifier(max_depth=20)
rf.fit(X_train, y_train)
print('Max tree depth: ', i)
print('Train results: ', classification_report(y_train, rf.predict(X_train)))
print('Test results: ',classification_report(y_test, rf.predict(X_test)))<jupyter_output>Max tree depth:  20
Train results:                precision    recall  f1-score   support

           0       0.80      0.98      0.88     59721
           1       0.94      0.60      0.73     35397

    accuracy                           0.84     95118
   macro avg       0.87      0.79      0.81     95118
weighted avg       0.85      0.84      0.83     95118

Test results:                precision    recall  f1-score   support

           0       0.79      0.97      0.87     15024
           1       0.91      0.56      0.69      8756

    accuracy                           0.82     23780
   macro avg       0.85      0.76      0.78     23780
weighted avg       0.83      0.82      0.80     23780
{'fraction_non_alphanumeric': 0.11587699617262769, 'fraction_numerical': 0.04738022964233866, 'mean_word_length': 3.6094890510948905, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '425119', 'license': 'MIT', 'n_tokens_mistral': 2737, 'n_tokens_neox': 2537, 'n_words': 565, 'path': 'Tourism/Prediction cancellation of hotel bookings/notebook.ipynb', 'repo_name': 'AlexRossmann/machine-learning-services', 'size': '7577'}