Machine Learning Project: Hotel Booking Prediction [Part 2]

Written by - Aionlinecourse2069 times views

This is the second part of our Hotel Booking Prediction project. Throughout this tutorial, we will discuss outliers, the application of supervised algorithms, cross-validation, and so on.

How to Handle Outliers

If there are some data points that are too far away from the normal one those are exactly called outliers. More specifically, if you have data of 100 persons whose age range is between 1 to 100 but if 100 persons have an age of 700 years, that 100 persons are considered outliers. Here, we are going to build a model that will be badly impacted by outliers.

sas.distplot(dataframe['lead_time'])       #Making distribution plot of lead_time by accessing dataframe
import numpy as np      
def handle_outlier(col):            #taking log of lead_time time for greater extent of skewness 
    dataframe[col]=np.log1p(dataframe[col])
handle_outlier('lead_time')     #calling the function
sas.distplot(dataframe['lead_time'])              #showing distribution plot log applied lead_time

We handled outlier for our price feature named ‘adr’, same as before

     
sas.distplot(dataframe['adr'])                      #distribution plot of adr
handle_outlier('adr')                                         #handling outlier for adr
sas.distplot(dataframe['adr'].dropna())                       #distribution plot of adr and handling missing values by dropna

Applying Techniques of Feature Importance

Here, we are applying techniques of feature importance to our data for selecting the most important features because there are tons of features. By doing this we can build fancy/very useful machine learning models.

First, checked null values. We can see on the result that there is only one missing value in ‘adr’.

dataframe.isnull().sum()            #checking null values and doing their sum
dataframe.dropna(inplace=True)                               #dropping null values and updating dataframe
y=dataframe['is_canceled']                                   #predicting independent features -> is_canceled
x=dataframe.drop('is_canceled',axis=1)                       #dropping is_canceled feature
from sklearn.linear_model import lasso
from sklearn.feature_selection import SelectFromModel        #for selecting important features

Alpha is a penalty parameter that means the bigger the value of alpha the less number of features will get selected.

selected_feat                                            #printing the entire features

Applying Logistic Regression on Data and Cross-Validating it

Logistic regression is one of the supervised algorithms and a statistical model. For this, we are going to apply logistic regression to our data and after that will cross-validate it.

from sklearn.model_selection import train_test_split                                        #for splitting data into train and test set
X_train,X_test,y_train,y_test=train_test_split(x,y,test_size=0.25,random_state=0)           #taking 25% of data for testing
from sklearn.linear_model import LogisticRegression                                         #importing 
logreg=LogisticRegression()                                                                 #calling the logisticregression class
logreg.fit(X_train,y_train)                                                                 #fitting training data
y_pred=logreg.predict(X_test)                                                               #doing prediction on test data
y_pred                                                                                      #printing the prediction array
from sklearn.metrics import confusion_matrix                    #importing confusion matrix
confusion_matrix(y_test,y_pred)                                 #confusion matrix of this logistic regression model
from sklearn.metrics import accuracy_score                  #importing accuracy_score to check accuracy
accuracy_score(y_test,y_pred)                               #checking accuracy_score of y test and prediction
from sklearn.model_selection import cross_val_score           #importing cross validation
score=cross_val_score(logreg,x,y,cv=10)                          #applying cross validation for achieving more accurate score
score.mean()                                                    #achieved new score by calling mean

Applying Multiple Algorithms on Data

Here we are applying different types of supervised algorithms naive Bayes, decision tree, logistic regression, and so on to achieve very good accuracy.

So, this is our model accuracy. Thank you for reading this article.

Recommended Projects

Deep Learning Interview Guide

Topic modeling using K-means clustering to group customer reviews

Have you ever thought about the ways one can analyze a review to extract all the misleading or useful information?...

Natural Language Processing
Deep Learning Interview Guide

Medical Image Segmentation With UNET

Have you ever thought about how doctors are so precise in diagnosing any conditions based on medical images? Quite simply,...

Computer Vision
Deep Learning Interview Guide

Build A Book Recommender System With TF-IDF And Clustering(Python)

Have you ever thought about the reasons behind the segregation and recommendation of books with similarities? This project is aimed...

Machine LearningDeep LearningNatural Language Processing
Deep Learning Interview Guide

Automatic Eye Cataract Detection Using YOLOv8

Cataracts are a leading cause of vision impairment worldwide, affecting millions of people every year. Early detection and timely intervention...

Computer Vision
Deep Learning Interview Guide

Crop Disease Detection Using YOLOv8

In this project, we are utilizing AI for a noble objective, which is crop disease detection. Well, you're here if...

Computer Vision
Deep Learning Interview Guide

Vegetable classification with Parallel CNN model

The Vegetable Classification project shows how CNNs can sort vegetables efficiently. As industries like agriculture and food retail grow, automating...

Machine LearningDeep Learning
Deep Learning Interview Guide

Banana Leaf Disease Detection using Vision Transformer model

Banana cultivation is a significant agricultural activity in many tropical and subtropical regions, providing a vital source of income and...

Deep LearningComputer Vision