Predicting Airfare Price Using Machine Learning Techniques
By: Naiborhu Josua
Tags: Pandas Dataframe, Kaggle, Data Science

Photo by National Cancer Institute on Unsplash

1. Cleaning the Data

T he first and important aspect of implementing the Data Science process started by cleaning the data in order to separate important data and error data that brings bias(error) in developing the flight price prediction that we want to build. This process has been extremely crucial in the data science process because it can affect the result.

Pandas, Seaborn, NumPy, and matplotlib are some of the most used libraries by Data Scientist in order to visualize and analyze the dataset. We will use these libraries such as follows :

Importing Dataset

We will get the features and records by importing the dataset. We can see there are some columns such as Airline, Date_of_Journey, Source, etc.

Based on these columns, we will try to eliminate the null values(error input) so that it can affect the prediction in our analysis. We will start by the Duration column to know the counts of each group. We will check the null values then drop the NaN values using dropna method and check whether the null values using isnull and sum method.

Drop NaN Values

2. Exploratory Data Analysis

Exploratory Data Analysis has been playing an important role in the success of our prediction. We will do feature engineering of some features so that it can represent the output for our model created using machine learning techniques. From the dataset shown above, we can see that column Date_of_Journey, Arrival_Time, Dep_Time, and Duration columns are a string data type, We will convert this datatype into timestamp to use this column for prediction. We will use pandas_to_datetime to convert it and get the hour and minutes of each column. You can refer to the notebook provided in Github.

One of the most important parts of EDA is handling categorical Data. Categorical data can be divided into Nominal data (without order) by using OneHot Encoder and Ordinal Data( with Order) by using LabelEncoder to label this data. We can start by Airline column and plot the data using seaborn drawn such as follows :

We can see that the average price ticket is the same among the airlines except for Jet Airways Business due to facilities provided by the Airline. We can also see some outliers among Jet Airways Business, Jet Airways, and Multiple Carriers that affect the price of the airlines.

We will perform the Airline column using One Hot Encoder because of nominal categorical data by using this code. We will implement the same code to the Source column and compare the Price and Source features.

Handling categorical data using One Hot Encoder

Comparison of Source and Price

We can see that some outliers on Bangalore Source. We will implement One Hot Encoder for destination and Label Encoder for the Total_Stops encoder for Ordinal categorical data.

Then we concat all the changes in the features using concat method in pandas.

3. Test Set

In our Machine Learning Process, We have already separated the training and test dataset. We will use this method in order to avoid Data Leakage. We import the data and preprocess it. We will predict the price in this analysis. Thus, we will do a feature selection to choose the best feature that has good relations with the target variable(Price).

We will then see the correlation between independent and dependent variables using heatmap and ExtraTreesRegressor to look at which one is the better features to select.

Selecting better features for the price prediction

Based on the graph, We can see that the Total_Stops feature has the highest influence in predicting the price followed by Duration_hours and Journey_Day.

4. Fitting the Model Using Random Forest

We will use Random Forest for this type of analysis by splitting the data using scikit-learn such as follows :

We can see that the prediction score has exceeds 95 % for the training data and 79% for the test data. We can improve this score by implementing hyperparameter tuning that consists of RandomizedSearchCV and GridSearchCV.

and create a random grid and use the fit method such as follows :

And then we can plot and predict the result again and we have improved the result based on our hyperparameter tuning. We have already improved our score from 79% to 81% by implementing the RandomizedSearchCV as our hyperparameter tuning. You can see the full code in the jupyter notebook uploaded in this Github link.

Reference :

Machine Learning Concepts (Supervised Learning, Unsupervised Learning, Hyperparameter tuning, Classification, Regression, etc)