<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anusheela Banik on Medium]]></title>
        <description><![CDATA[Stories by Anusheela Banik on Medium]]></description>
        <link>https://medium.com/@banikanusheela?source=rss-663c3effb0d6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*AW4B6wwJM9RizDmE</url>
            <title>Stories by Anusheela Banik on Medium</title>
            <link>https://medium.com/@banikanusheela?source=rss-663c3effb0d6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 10:41:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@banikanusheela/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Flight Departure Delay Prediction]]></title>
            <link>https://medium.com/@banikanusheela/flight-departure-delay-prediction-417240a72ea4?source=rss-663c3effb0d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/417240a72ea4</guid>
            <category><![CDATA[predictive-analytics]]></category>
            <dc:creator><![CDATA[Anusheela Banik]]></dc:creator>
            <pubDate>Sat, 27 Oct 2018 21:26:05 GMT</pubDate>
            <atom:updated>2018-10-27T21:53:20.374Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s predict if a flight will have a delay in it’s departure!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jdoHNP-zTDQbBc4N.jpg" /></figure><p><strong>INTRODUCTION</strong></p><p>The U.S. Department of Transportation’s (DOT) Bureau of Transportation Statistics tracks the status of the flights. They provide a summary of the arrival delays, departure delays, on-time arrival etc in their monthly report. This data set is a summary of different air carriers showing their departure delays, arrival delays, scheduled departure, etc.</p><p>We can use it to predict if a flight carrier will have a departure delay and hence try to avoid that from happening. This will prevent the customers as well as the airlines to avoid any losses, whether in their time or business. For this project, I will build different predictive models and use the following steps.</p><p>Ø Data Exploration</p><p>Ø Feature Engineering</p><p>Ø Building training/validation/test samples</p><p>Ø Model Selection</p><p>Ø Model Evaluation</p><p><strong>PROJECT DEFINITION</strong></p><p>Predict if a flight will have a departure in its delay.</p><p><strong>DATA EXPLORATION</strong></p><p>The data is taken from Kaggle(<a href="https://www.kaggle.com/miquar/explore-flights-csv-airports-csv-airlines-csv/data">link</a>). I have used the flights and airports data set for this project. The flights data set contains 1049000 rows of flights data of 14 different carriers in the year 2015. For the project, I have used the first 100000 rows to build my model and predict the delay. I have also used the airport data set to use the origin and destination airport.</p><p>We will use Python to build the predictive model. Let’s begin by first loading the data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*j7SnCPaIjFTK_dwKe7OkfQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*cscvhZAOjyekqOAx1nBQXA.png" /></figure><p>First, we load the flights data set. After analyzing the flights data set, that there are two different types of columns, numeric and categorical. Let’s look at the columns below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*nx6EkZOqIvZmhgwYmR0RIA.png" /></figure><p>The most important column in my table is DEPARTURE_DELAY. This will be used to predict if there will be a delay in the departure of any flight or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*0Zrcb68Al_BxiVTGyxid0w.png" /></figure><p>The negative values indicate an early departure of the flights and the positive values indicate that there was a delay in the departure of the flights. Other than this column, another column CANCELLED plays a major role. This tells us if the flight was cancelled or not and we can filter out the rows depending on that because in case that happened there won’t be a delay in the departure of the flight. The cancelled flights are assigned 1 and the rest are 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*F73wSqYHvuqTv3Hc-rkgPw.png" /></figure><p>After this, let’s define our output variable for our classification problem. In this case, we are trying to predict if a flight will have a delay in its departure or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/551/1*PGW3zb6Wk6FUXcNDt2hv1Q.png" /></figure><p>Next, let’s calculate the prevalence of the flight carriers that will have a delay in their departure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*QI-NleijzXxClGHRC-96Tg.png" /></figure><p>From this we can conclude that approximately 43% of the flights will have a departure delay. This clearly shows an imbalance and we will work on this.</p><p>Now we will load the airports data set and use this to see if the origin and destination airports affect the delays. We will also explore the airports data set and change the unknown airport numbers to OTHER. This helps in reducing the dimensionality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*mDc_5Ka_a-lBmSrwaV0oKQ.png" /></figure><p>From our analysis, we can see a variety of columns. Let’s throw some light on a few of them.</p><p>TAXI_IN, TAXI_OUT, WHEELS_ON, WHEELS_OFF: These are numerical values but will not contribute in any way to the delay prediction, so these will not be included in the model.</p><p>ORIGIN_AIRPORT, DESTINATION_AIRPORT: These are the most important categorical variables because they will play a major role in the delays. They have many categorical variables under them and will help in building a good predictive model.</p><p>DAY_OF_WEEK: Day of the week is a numerical value and plays an important role and will be used in building the models.</p><p>MONTH, FLIGHT NUMBER: These are some numerical variables that will not be useful in the prediction and hence we will not use them in building our model.</p><p>DIVERTED: This is another numerical variable which will have no effect on the departure delays and hence we will not include it in our model</p><p><strong>FEATURE ENGINEERING</strong></p><p>In this section, we will create different features to build our model for predicting the departure delays on flights. We will use those features from our data set that significantly contribute in the prediction. We will break all our variables into their subcategories such as numerical and categorical.</p><p><strong>NUMERICAL FEATURES</strong></p><p>Let’s look at the numerical features we will use for our predictive model. They are the simplest features that do not need any modifications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*ZPAdUi0EJoYzlfDUXoCGWw.png" /></figure><p>Now let’s check if they have any missing values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*lYoalMhrNM252MsgmAe3zg.png" /></figure><p>From this we can clearly see that none of the numerical features have missing values.</p><p><strong>CATEGORICAL FEATURES</strong></p><p>Next, we will look at the categorical variables. These are non-numeric data such as origin airport, destination airport etc. We will convert them into numeric data by using the concept on one-hot encoding. We will talk about it in a while.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*ZfcUdtJoPgNn3es1MgVhRw.png" /></figure><p>Now let’s check if any of these variables have missing values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/169/1*czcbZ-bodRrogUqZjI82pw.png" /></figure><p>Now that we see that there are no missing values, let’s look at the unique values for each of the categorical variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/267/1*xuteKadaITF9mgTmTqqc0Q.png" /></figure><p>We implement the concept of one-hot encoding. We use one-hot encoding to convert our categorical variables to numerical data. In this each unique categorical variable gets converted into a numeric column. Then in that column, 1 is assigned if the sample is unique else 0. For the column AIRLINE, we would create new columns (‘AIRLINE_AS’,’AIRLINE_B6&#39;, etc). If the airline is AS, the flight gets a 1 under ‘AIRLINE_AS’ and 0 under the rest of the AIRLINE columns. To create these one-hot encoding columns, we can use the get_dummies function. The prefix option is used to trace back to the original column.</p><p>The get_dummies function does not work on numerical columns. Now let’s apply this concept on our categorical data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*7YYV0IPy9kVgLqYBmTse-Q.png" /></figure><p>Now, we add them to our data frame by using the concat function. We set axis=1 to indicate add the columns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*Hrw2mDMTEMXcPQfu7LznTQ.png" /></figure><p>Now, let’s save them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*KXq0MQ2-9sbTcxPGsmAizQ.png" /></figure><p><strong>Engineering Features Summary</strong></p><p>We have created 656 features for our machine learning models. They are divided as follows:</p><p>Ø 6 Numerical Features</p><p>Ø 650 Categorical Features</p><p>Now we will create a new data frame with only these features and the OUTPUT_LABEL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*SwTjeoaV5e78Q31hvQOG0w.png" /></figure><p><strong>Building Training/Validation/Test Samples</strong></p><p>We have analysed our data and created features from the them. Now we will split our data. We split the data to get an idea on how our model will perform on unseen data, which in our case will be test data. We split the data into three parts:</p><p>Ø Training: This is used to train our model. The major portion of the data set is assigned as training samples. In our case, it is 70% of the data.</p><p>Ø Validation: These are separate from the training set and are used to evaluate how our model will perform and hence make changes to improve it. In our case it will be 15% of the data.</p><p>Ø Test: These samples are kept separate and after selecting our best model are used for final evaluation. In our case, it is also 15% of the data.</p><p>First, we will shuffle our data to avoid any specific order our samples were arranged it. To do this, we will use the sample function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*FtK0oVSDcJc-pcHYOZEhLQ.png" /></figure><p>Next, we will use 30% of the data after sampling again. This will come from the same distribution and will be used to get our validation and test sets in the next step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*s5PR4je2u2bkPPQ38brL0A.png" /></figure><p>Now we split the data into test and validation sets by splitting them into half. We use the 50% fraction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*15M1qQmmqmeUsfOYlgzmxg.png" /></figure><p>We use the .drop function to drop the rows that were not a part of the sample to get our test sample. We use the same logic to get our training data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*GCtVh7RhWbgs0kH5u1yjDw.png" /></figure><p>After this we will calculate the prevalence of all the sets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*iWM9nRYP77BGn1d6D6jTAQ.png" /></figure><p>We can see that the prevalence for each of the groups is almost the same. We need to balance the data to give more weight to the positive samples. Here, we will create a balanced training data set with 50% of both positive and negative samples. The ratio can be modified to improve the performance of our model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*XkQwB9S5hE5UhRIbcz-CSg.png" /></figure><p>Now we build X and Y input matrices for our machine learning package.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*6JGscuP9Rx-yXWB0W3GXyQ.png" /></figure><p>Since machine learning models can be of different sizes, we need to scale them. For this purpose, we will use the scikit-learn’s Standard Scaler to remove the means and scale them to unit variance. In this case, the scaler is created using the training data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*IvJzfFlJbI8-laDvqCKafw.png" /></figure><p>This scaler data will be used for the test data at a later stage of the project. Hence, we will save it using a pickle package. Then we will load it back.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*WK2IdqDKCZlM18ssypJ7Fg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*1F7BEjkRXzrYtxP5BUg9kA.png" /></figure><p>Now we can transform our data matrices</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*4V-_Xawqy-_69g4A-EProw.png" /></figure><p>We will not use our test data set till the end.</p><p><strong>MODEL SELECTION</strong></p><p>After cleaning and preparing the data, we will build our models and check their performance on our validation sets and hence select the best model.</p><p>The models will be evaluated on different performance metrics as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*4LLRvDF2yxkQdpOeP2zNBw.png" /></figure><p>We will evaluate the models at a threshold of 0.5</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/188/1*SXN1zFxEmcNZB0AIxesMGg.png" /></figure><p><strong>MODEL SELECTION: BASELINE MODELS</strong></p><p>We will compare the performance of the following 6 machine learning models using default hyperparameters:</p><p>· Logistic regression</p><p>· Stochastic gradient descent</p><p>· Naive Bayes</p><p>· Decision tree</p><p>· Random forest</p><p>· Gradient boosting classifier</p><p><strong>LOGISTIC REGRESSION</strong></p><p><a href="http://www.statisticssolutions.com/academic-solutions/membership-resources/member-profile/data-analysis-plan-templates/data-analysis-plan-logistic-regression/">Logistic regression</a> is executed when the dependent variable is binary. It is a predictive analysis technique. It describes data and explains the relationship between one dependent binary variable and one or more independent variables. The model is usually easy to interpret, and we can know which feature is important for us. We need to scale our data before applying this model to it. We can fit logistic regression using the following code from scikit-learn.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*uGdNFo2bzb0B1_vLuUb9KQ.png" /></figure><p><strong>STOCHASTIC GRADIENT DESCENT</strong></p><p>Stochastic gradient descent and logistic regression are similar in nature. Though they both use coefficient of the liner function, Stochastic Gradient Descent works in small batches hence making the execution much faster than the former. We can use the following code to fit this model from scikit-learn.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*En3SsgZHARcmnHqxIR9_rA.png" /></figure><p><strong>NAIVE BAYES</strong></p><p>Naive Bayes is a machine learning model that uses the Bayes rule. It is called so because it assumes that all the variables are independent of each other. This is a good assumption in case of natural language processing. We can fit Naive Bayes with the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*o1-qk06gS2EZb0eWgvzu9A.png" /></figure><p><strong>DECISION TREE</strong></p><p>Machine learning models can also be tree-based. We will first look at Decision Tree. In this model, we divide samples by splitting them based on a threshold. In each question, you ask if the samples have a specific variable greater than some threshold and then split the samples. The final prediction is the fraction of positive samples in the final split of the tree. We need to figure out what threshold to select for each split. They usually do not have any presumptions and we need to provide sufficient depth for the split. We can fit decision trees using the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*HhKyD5n7IrDMbmbwJGnY5w.png" /></figure><p><strong>RANDOM FOREST</strong></p><p>Decision trees tend to result in overfitting because they memorize the training data. Random forest helps overcome this disadvantage. Multiple trees are generated, and results are then aggregated. Random forests tend to perform better than decision trees because they can generalize easily. To fit random forests, we can use the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*Y4CivWZx9-Dc4kb6Y-pxOw.png" /></figure><p><strong>GRADIENT BOOSTING CLASSIFIER</strong></p><p>Gradient boosting classifier is another tree-based method which is used to improve the problem of overfitting in decision trees. A bunch of shallow trees are created, and they optimize the error occurred previously. It is paired with the gradient descent classifier. To fit gradient boosting classifier, we can use the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*tOO13_9Mh8j7IKE2XBzrXA.png" /></figure><p><strong>ANALYSIS OF BASELINE MODELS</strong></p><p>Now we will compare all the baseline models. For this purpose, we will create a data frame with the results of each one of them. We can use any performance metric to evaluate the models and hence pick the best one. In this project, AUC will be used to evaluate the best model. We will plot the ROC curve and observe the relation between the true positive rate and the false positive rate. In this case, no threshold is required for comparison.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*c9GfnMoHJ3zv1-B1NMU9nA.png" /></figure><p>We can see all models (except Naive Bayes) have almost a similar performance on the validation data set. There are signs of slight overfitting which can be concluded by the drop between training and validation. Else, overall the models look balanced.</p><p><strong>MODEL SELECTION: LEARNING CURVE</strong></p><p>Now we plot learning curves to evaluate our models. We will plot the AUC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*ECnEslPEw_P8K6kEf-cvcg.png" /></figure><p>In the case of random forest, we can see the training and validation scores have a slight difference in their AUC scores. This is called high variance and is a sign of overfitting. To improve the model, we can add more samples, reduce the number of features, decrease model complexity, add better features etc.</p><p>In case of high bias, we can add new features, increase model complexity or even change the model architecture.</p><p><strong>MODEL SELECTION: FEATURE IMPORTANCE</strong></p><p>To improve our model, it is essential to understand what features are important to the model. This can be done for Logistic Regression as well as Random Forest. This can help us to inspire new feature ideas in both high bias as well as high variance cases, find out the top features and avoid any data leakage which can occur in case the column affecting the output label is included.</p><p>We can get the feature importance from logistic regression using the following</p><p>We can look at the top 50 positive and top 50 negative features.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*U9WaN6PRBq2YVftxsfVM5w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*o5togT0ePdA7J6ZzmZkVvg.png" /></figure><p>After analyzing these plots, we can add more data related to the features that affect our model the most. Also, by plotting this we can find out if there is any error in our model. We can avoid Data leakage which basically happens when accidentally a wrong variable is included in our training data set which leads to cheating and biased model. Initially, I had included the Departure Delay column in my model which was resulting in 100% accuracy because the model already knew the results. Hence, this feature helps us to avoid such cases and build an efficient model.</p><p><strong>MODEL EVALUATION</strong></p><p>After looking at the performance of all my models, I have decided to select Logistic Regression as my best model. Now, we need to check the performance of our model on our test data set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*wViJy6HGlMLuocqyavyh1A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*RBHEyEVwKZTuAeeuzSMJ1A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Ou3ud32aUA50HOezliqj0Q.png" /></figure><p>We can plot the ROC curve for the three sets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*HrxJreq5mf6QfRdT_9hDCQ.png" /></figure><p><strong>CONCLUSION</strong></p><p>Through this project, we created a machine learning model that can predict the flight departure delays. The best model was Logistic Regression model. The model was able to catch 62% of the departure delays. Also, it was observed that the delay of the flights was heavily dependent on the departure airports. This clearly implies that if an airport is busy and is a major airport, the chances of flight delays will be more compared to the other airports. By this analysis, it can be made sure that the schedules are better managed, and the functioning of that airport can be improved to avoid such delays. I believe that flights are the fastest way to reach a place and its efficiency matters a lot. Hence, to improve the business for different flights as well as the airports it is essential to minimize these delays. This model will help us evaluate the places we need to work on to make the overall system more efficient.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=417240a72ea4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>