text_plain_output_1.png image_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score

from sklearn.metrics import f1_score

from sklearn.metrics import precision_score

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import OneHotEncoder

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams.update({'font.size': 14})

df = pd.read_csv('/kaggle/input/airways-customer-data/filtered_customer_booking.csv', index_col=0)

df = df.reset_index(drop=True)

df_final = df

from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(handle_unknown='ignore')
encoder_df = pd.DataFrame(encoder.fit_transform(df[['sales_channel']]).toarray())
encoder_df = encoder_df.rename(columns={0: 'Internet', 1: 'Mobile'})
df_final = df_final.join(encoder_df)
encoder_df = pd.DataFrame(encoder.fit_transform(df[['trip_type']]).toarray())
encoder_df = encoder_df.rename(columns={0: 'RoundTRip', 1: 'OneWayTrip', 2: 'CircleTrip'})
df_final = df_final.join(encoder_df)

df_final.drop(['sales_channel', 'trip_type', 'booking_origin', 'route'], axis=1, inplace=True)

df_final = df_final.drop('booking_complete', axis=1)

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaled_df = scaler.fit_transform(df_final)

scaled_df = pd.DataFrame(scaled_df, columns=df_final.columns)

corr = scaled_df.corr()

from sklearn.model_selection import train_test_split
X = scaled_df.iloc[:, :-1]
y = scaled_df['label']
X_train, X_test, y_train, y_test = train_test_split(X.to_numpy(), y.to_numpy(), test_size=0.2, random_state=42)

"""
    Create functions to fit and predict the values of whether customer would complete the booking.
    Also functions with metrics to evaluate the model prediction.
"""
def model_fit_predict(model, X, y, X_predict):
    model.fit(X, y)
    return model.predict(X_predict)
def acc_score(y_true, y_pred):
    return accuracy_score(y_true, y_pred)
def pre_score(y_true, y_pred):
    return precision_score(y_true, y_pred)
def f_score(y_true, y_pred):
    return f1_score(y_true, y_pred)

clf_rf = RandomForestClassifier(max_depth=50, min_samples_split=5, random_state=0)

y_pred_train = model_fit_predict(clf_rf, X_train, y_train, X_train)
set(y_pred_train)
f1 = round(f1_score(y_train, y_pred_train), 2)
acc = round(accuracy_score(y_train, y_pred_train), 2)
pre = round(precision_score(y_train, y_pred_train), 2)

clf_rf = RandomForestClassifier(max_depth=50, min_samples_split=5, random_state=0)
y_pred_test = model_fit_predict(clf_rf, X_train, y_train, X_test)
f1 = round(f1_score(y_test, y_pred_test), 2)
acc = round(accuracy_score(y_test, y_pred_test), 2)
pre = round(precision_score(y_test, y_pred_test), 2)
print(f'Accuracy, precision and f1-score for training data are {acc}, {pre} and {f1} respectively')