text_plain_output_1.png
from imblearn.over_sampling import SMOTE

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import precision_score

from sklearn.model_selection import GridSearchCV

import matplotlib.pyplot as plt

import pandas as pd

import sklearn.model_selection as ms

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use('seaborn')
pd.set_option('display.max_columns', None)

data = pd.read_csv('../input/hotel-booking/hotel_booking.csv')
data

data.isna().sum()

data = data.drop(['agent', 'company', 'required_car_parking_spaces', 'reservation_status', 'reservation_status_date', 'country', 'name', 'email', 'phone-number', 'credit_card'], axis=1)
data

a_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}}
data.replace(a_month, inplace=True)

meal = {'meal': {'Undefined': 0, 'SC': 1, 'BB': 2, 'HB': 3, 'FB': 4}}
data.replace(meal, inplace=True)

segment = {'market_segment': {'Aviation': 1, 'Complementary': 2, 'Corporate': 3, 'Direct': 4, 'Groups': 5, 'Offline TA/TO': 6, 'Online TA': 7}}
data.replace(segment, inplace=True)

distribution = {'distribution_channel': {'GDS': 1, 'Corporate': 2, 'Direct': 3, 'TA/TO': 4}}
data.replace(distribution, inplace=True)

deposit = {'deposit_type': {'Refundable': 1, 'Non Refund': 0, 'No Deposit': 2}}
data.replace(deposit, inplace=True)

customer = {'customer_type': {'Contract': 2, 'Group': 3, 'Transient-Party': 1, 'Transient': 0}}
data.replace(customer, inplace=True)

import sklearn.model_selection as ms
train, test = ms.train_test_split(data, test_size=0.2, random_state=42)
train_X = train.drop(labels='is_canceled', axis=1)
train_Y = train['is_canceled']
test_X = test.drop(labels='is_canceled', axis=1)
test_Y = test['is_canceled']

from imblearn.over_sampling import SMOTE
os = SMOTE(sampling_strategy='minority', random_state=42, k_neighbors=5)
train_smote_X, train_smote_Y = os.fit_resample(train_X, train_Y)
train_smote_X = pd.DataFrame(data=train_smote_X, columns=train_X.columns)
train_smote_Y = pd.DataFrame(data=train_smote_Y)

from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(random_state=42)
param_grid = {'n_estimators': (100, 1000, 2000), 'criterion': ['gini', 'entropy'], 'max_features': ['auto', 'sqrt', 'log2']}
from sklearn.model_selection import GridSearchCV
CV_rf = GridSearchCV(estimator=clf, param_grid=param_grid, n_jobs=1, cv=2, verbose=1, return_train_score=True)
CV_rf.fit(train_smote_X, train_smote_Y)

CV_rf.best_params_

pred = CV_rf.predict(test_X)

from sklearn.metrics import precision_score
print('Precision for RF on test data: ', precision_score(test_Y, pred))