Predicting Air Travel Trends with Linear Regression

Setting the Scene

So far, we have explored the Flights dataset from Seaborn, analyzed trends, and visualized these trends using various charts such as line plots and heat maps. Today, we focus on leveraging this historical data to predict future trends using a technique known as Linear Regression.

Linear regression is a powerful tool used to predict an outcome (dependent variable) based on one or more predictor (independent) variables, forming a linear relationship. For instance, we might wish to forecast future passenger numbers based on past trends using our air travel dataset.

Why is this important? Anticipating future trends is key to strategic decision-making. Airlines, for example, use these predictions for numerous operational and strategic decisions, such as scheduling flights, capacity planning, resource allocation, and strategic expansions. Let's see how we can make such predictions!

Introduction to Linear Regression

Linear regression assumes a linear relationship between the dependent and predictor variable(s), which can be represented as: y=a+b∗xy = a + b * x. Here, yy is the dependent variable we want to predict, xx is our predictor variable, aa is the y-intercept, and bb is the slope of the line. In the context of our Flights dataset, yy can represent the number of passengers, and xx can represent time (years or months).

Python
import seaborn as sns
import pandas as pd

flights_data = sns.load_dataset("flights")
flights_data['year'] = pd.to_datetime(flights_data['year'], format='%Y')
flights_pivot = pd.pivot_table(data=flights_data, values='passengers', index='year', aggfunc='sum').reset_index()

import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import numpy as np

# Extracting the year from the date and converting it into the appropriate format
flights_pivot['year'] = flights_pivot['year'].dt.year

X = np.array(flights_pivot['year']).reshape(-1,1)
y = flights_pivot['passengers']

reg = LinearRegression().fit(X, y)

plt.scatter(X, y, color = "m", marker = "o", s = 30)

Y_pred = reg.predict(X)
plt.plot(X, Y_pred, color = "g")

plt.xlabel('Year')
plt.ylabel('Passengers')
plt.title('Linear Regression: Passengers Over Time')
plt.show()

In the above code example, we load the Flights dataset and pivot it to get the total passenger count for each year. Next, we create our Linear Regression model and fit it to the data (years and passenger counts). We use a scatter plot to visualize the data points, while the line plot indicates our fitted regression line.

The purple dots represent the actual number of passengers for each year from 1949 to 1960, plotted against the year. The green line represents the line of best fit generated by linear regression. This line aims to minimize the total distance between itself and each point (which signifies the error or residual). It represents our model's best guess for the passenger count given a particular year.

Linear Regression: Passengers Over Time

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal