<jupyter_start><jupyter_text>Hotel Reservation Cancellation Prediction<jupyter_code>import tensorflow as tf
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.pipeline import make_pipeline
from sklearn.compose import make_column_transformer
df = pd.read_csv('datasets/hotel.csv')
df.head()
X = df.copy()
y = X.pop('is_canceled')
X['arrival_date_month'] = \
    X['arrival_date_month'].map(
        {'January':1, 'February': 2, 'March':3,
         'April':4, 'May':5, 'June':6, 'July':7,
         'August':8, 'September':9, 'October':10,
         'November':11, 'December':12}
    )
features_num = [
    "lead_time", "arrival_date_week_number",
    "arrival_date_day_of_month", "stays_in_weekend_nights",
    "stays_in_week_nights", "adults", "children", "babies",
    "is_repeated_guest", "previous_cancellations",
    "previous_bookings_not_canceled", "required_car_parking_spaces",
    "total_of_special_requests", "adr",
]
features_cat = [
    "hotel", "arrival_date_month", "meal",
    "market_segment", "distribution_channel",
    "reserved_room_type", "deposit_type", "customer_type",
]
transformer_num = make_pipeline(
    SimpleImputer(strategy="constant"), # there are a few missing values
    StandardScaler(),
)
transformer_cat = make_pipeline(
    SimpleImputer(strategy="constant", fill_value="NA"),
    OneHotEncoder(handle_unknown='ignore'),
)
preprocessor = make_column_transformer(
    (transformer_num, features_num),
    (transformer_cat, features_cat),
)
X_train,X_valid, y_train, y_valid = train_test_split(X, y , stratify=y, train_size=0.75)
X_train = preprocessor.fit_transform(X_train)
X_valid = preprocessor.transform(X_valid)
input_shape = [X_train.shape[1]]<jupyter_output><empty_output><jupyter_text>Model Preparation<jupyter_code>from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([layers.BatchNormalization(input_shape=input_shape),
                             layers.Dense(256,activation='relu'),
                             layers.BatchNormalization(),
                             layers.Dropout(0.3),
                             layers.Dense(256,activation='relu'),
                             layers.BatchNormalization(),
                             layers.Dropout(0.3),
                             layers.Dense(1,activation='sigmoid')])
model.compile(optimizer=keras.optimizers.Adam(lr=0.001),loss='binary_crossentropy',metrics=['binary_accuracy'])
early_stopping = keras.callbacks.EarlyStopping(
    patience=5,
    min_delta=0.001,
    restore_best_weights=True,
)
history = model.fit(
    X_train, y_train,
    validation_data=(X_valid, y_valid),
    batch_size=512,
    epochs=200,
    callbacks=[early_stopping],
)

history_df = pd.DataFrame(history.history)
history_df.loc[:, ['loss', 'val_loss']].plot(title="Cross-entropy")
history_df.loc[:, ['binary_accuracy', 'val_binary_accuracy']].plot(title="Accuracy")<jupyter_output>Epoch 1/200
175/175 [==============================] - 1s 7ms/step - loss: 0.4796 - binary_accuracy: 0.7746 - val_loss: 0.4290 - val_binary_accuracy: 0.7942
Epoch 2/200
175/175 [==============================] - 1s 6ms/step - loss: 0.4221 - binary_accuracy: 0.8018 - val_loss: 0.4016 - val_binary_accuracy: 0.8111
Epoch 3/200
175/175 [==============================] - 1s 6ms/step - loss: 0.4099 - binary_accuracy: 0.8087 - val_loss: 0.3916 - val_binary_accuracy: 0.8150
Epoch 4/200
175/175 [==============================] - 1s 6ms/step - loss: 0.4023 - binary_accuracy: 0.8115 - val_loss: 0.3866 - val_binary_accuracy: 0.8187
Epoch 5/200
175/175 [==============================] - 1s 6ms/step - loss: 0.3967 - binary_accuracy: 0.8158 - val_loss: 0.3845 - val_binary_accuracy: 0.8216
Epoch 6/200
175/175 [==============================] - 1s 6ms/step - loss: 0.3929 - binary_accuracy: 0.8172 - val_loss: 0.3799 - val_binary_accuracy: 0.8229
Epoch 7/200
175/175 [==============================] - 1s [...]
{'fraction_non_alphanumeric': 0.17655713585090732, 'fraction_numerical': 0.06081412457086807, 'mean_word_length': 4.311197916666667, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '217351', 'license': 'CC0-1.0', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1384, 'n_words': 237, 'path': 'Deep_Learning/Hotel_Binary_Classification.ipynb', 'repo_name': 'ShivamBhosale/100DaysOfCode_V2', 'size': '4078'}