-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
183 lines (134 loc) · 6.5 KB
/
main.py
File metadata and controls
183 lines (134 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing, metrics
import seaborn as sb
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression, Lasso
""" The following code estimates flight delay. It includes preprocessing data, its visualization, detecting and removing
outliers. Then, it uses 3 machine learning models to predict the flight delay. They are: linear regression, polynomial
regression and lasso regularization. After applying these models, the code checks its metrics. """
# accessing dataset of flight_delay.csv
dataset = pd.read_csv('flight_delay.csv')
print(f'{dataset.head().to_string()}\n')
# encoding data to obtain categorical features
label_enc = preprocessing.LabelEncoder()
dataset['Depature Airport'] = label_enc.fit_transform(dataset['Depature Airport'])
dataset['Destination Airport'] = label_enc.fit_transform(dataset['Destination Airport'])
print(f'{dataset.head().to_string()}\n')
# checking for missing values
dataset.isnull()
dataset.isnull().sum()
# converting string of the following columns into datetime
dataset['Scheduled depature time'] = pd.to_datetime(dataset['Scheduled depature time'])
dataset['Scheduled arrival time'] = pd.to_datetime(dataset['Scheduled arrival time'])
# adding a new feature, i.e. duration
duration = dataset['Scheduled arrival time'] - dataset['Scheduled depature time']
flight_dur_sec = duration.dt.total_seconds()
flight_dur = flight_dur_sec/60
dataset['Flight duration'] = flight_dur
print(f'{dataset.head().to_string()}\n')
# splitting data into train and test data
train_data = dataset.drop(dataset[pd.DatetimeIndex(dataset['Scheduled depature time']).year == 2018].index)
test_data = dataset.drop(dataset[pd.DatetimeIndex(dataset['Scheduled depature time']).year < 2018].index)
# plotting correlation matrix to see the delay dependence of different features
corr_mat = train_data.corr()
sb.heatmap(corr_mat, annot = True)
plt.show()
# visualising data
plt.plot(dataset['Flight duration'], dataset['Delay'], 'bo')
plt.title('Flight duration vs Delay')
plt.xlabel('Flight duration (min)')
plt.ylabel('Delay (min)')
plt.show()
# define the outlier
sb.boxplot(data = dataset['Delay'], x = dataset['Flight duration'])
plt.title('Boxplot for outliers detection')
# outliers removing
train_data = train_data.drop(train_data[(train_data['Flight duration'] > 900)].index)
# obtaining X_train, y_train, x_test, y_test
X_train = train_data.drop(['Scheduled depature time', 'Scheduled arrival time','Delay'], axis=1)
y_train = train_data['Delay']
x_test = test_data.drop(['Scheduled depature time', 'Scheduled arrival time','Delay'], axis=1)
y_test = test_data['Delay']
# Linear Regression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# checking train data
y_train_pred = regressor.predict(X_train)
# checking test data
y_test_pred = regressor.predict(x_test)
print('Metrics for Linear Regression')
# checking train data
print('Testing train data')
print('Mean Absolute Error:', metrics.mean_absolute_error(y_train, y_train_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_train, y_train_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_train, y_train_pred)))
print('R score', metrics.r2_score(y_train, y_train_pred))
# checking test data
print('Testing test data')
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_test_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_test_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
print('R score', metrics.r2_score(y_test, y_test_pred))
# Polynomial Regression
poly_reg = PolynomialFeatures(degree=3)
x_train_poly = poly_reg.fit_transform(X_train)
poly_reg_model = LinearRegression()
poly_reg_model.fit(x_train_poly,y_train)
# checking train data
y_train_pred = poly_reg_model.predict(x_train_poly)
# checking test data
y_test_pred = poly_reg_model.predict(poly_reg.fit_transform(x_test))
print('Metrics for polynomial regression')
# checking train data
print('Testing train data')
print('Mean Absolute Error:', metrics.mean_absolute_error(y_train, y_train_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_train, y_train_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_train, y_train_pred)))
print('R score', metrics.r2_score(y_train, y_train_pred))
# checking test data
print('Testing test data')
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_test_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_test_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
print('R score', metrics.r2_score(y_test, y_test_pred))
# Lasso
X_train, x_val, y_train, y_val = train_test_split(X_train, y_train, test_size=1/8, random_state=123)
alphas = [2.2, 2, 1.5, 1.3, 1.2, 1.1, 1, 0.3, 0.1]
losses = []
# defining the best alpha for Lasso
for alpha in alphas:
lasso = Lasso(alpha=alpha)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(x_val)
mse = mean_squared_error(y_val, y_pred)
losses.append(mse)
plt.plot(alphas, losses)
plt.title("Lasso alpha value selection")
plt.xlabel("alpha")
plt.ylabel("Mean squared error")
plt.show()
best_alpha = alphas[np.argmin(losses)]
print("Best value of alpha:", best_alpha)
lasso = Lasso(best_alpha)
lasso.fit(X_train, y_train)
# checking train data
y_train_pred = lasso.predict(X_train)
# checking test data
y_test_pred = lasso.predict(x_test)
print('Metrics for Lasso')
# checking train data
print('Testing train data')
print('Mean Absolute Error:', metrics.mean_absolute_error(y_train, y_train_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_train, y_train_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_train, y_train_pred)))
print('R score', metrics.r2_score(y_train, y_train_pred))
# checking test data
print('Testing test data')
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_test_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_test_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
print('R score', metrics.r2_score(y_test, y_test_pred))