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.