Spaces:
Runtime error
Runtime error
ShivamMore
commited on
Commit
·
e428742
1
Parent(s):
69e0998
update
Browse files- .DS_Store +0 -0
- .idea/.gitignore +8 -0
- .idea/final-project-flight-never-delay.iml +8 -0
- .idea/inspectionProfiles/Project_Default.xml +27 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- .streamlit/config.toml +2 -0
- __pycache__/constants.cpython-310.pyc +0 -0
- __pycache__/constants.cpython-311.pyc +0 -0
- __pycache__/constants.cpython-38.pyc +0 -0
- __pycache__/constants.cpython-39.pyc +0 -0
- __pycache__/introduction.cpython-310.pyc +0 -0
- __pycache__/introduction.cpython-38.pyc +0 -0
- __pycache__/introduction.cpython-39.pyc +0 -0
- __pycache__/prediction.cpython-310.pyc +0 -0
- __pycache__/prediction.cpython-38.pyc +0 -0
- __pycache__/prediction.cpython-39.pyc +0 -0
- __pycache__/visualization.cpython-310.pyc +0 -0
- __pycache__/visualization.cpython-38.pyc +0 -0
- __pycache__/visualization.cpython-39.pyc +0 -0
- constants.py +53 -0
- pages/.DS_Store +0 -0
- pages/prediction.py +94 -0
- pages/visualization.py +615 -0
- prediction.ipynb +0 -0
- saved_models/cancel_log_reg.sav +0 -0
- saved_models/data_transformer.sav +0 -0
- saved_models/delay_carrier_lin_reg.sav +0 -0
- saved_models/delay_late_aircraft_lin_reg.sav +0 -0
- saved_models/delay_lin_reg.sav +0 -0
- saved_models/delay_nas_lin_reg.sav +0 -0
- saved_models/delay_security_lin_reg.sav +0 -0
- saved_models/delay_weather_lin_reg.sav +0 -0
.DS_Store
ADDED
Binary file (8.2 kB). View file
|
|
.idea/.gitignore
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 默认忽略的文件
|
2 |
+
/shelf/
|
3 |
+
/workspace.xml
|
4 |
+
# 基于编辑器的 HTTP 客户端请求
|
5 |
+
/httpRequests/
|
6 |
+
# Datasource local storage ignored files
|
7 |
+
/dataSources/
|
8 |
+
/dataSources.local.xml
|
.idea/final-project-flight-never-delay.iml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<module type="PYTHON_MODULE" version="4">
|
3 |
+
<component name="NewModuleRootManager">
|
4 |
+
<content url="file://$MODULE_DIR$" />
|
5 |
+
<orderEntry type="jdk" jdkName="Python 3.8 (data)" jdkType="Python SDK" />
|
6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
7 |
+
</component>
|
8 |
+
</module>
|
.idea/inspectionProfiles/Project_Default.xml
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<profile version="1.0">
|
3 |
+
<option name="myName" value="Project Default" />
|
4 |
+
<inspection_tool class="CythonUsageBeforeDeclarationInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
5 |
+
<inspection_tool class="DuplicatedCode" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
6 |
+
<inspection_tool class="PyAssignmentToLoopOrWithParameterInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
7 |
+
<inspection_tool class="PyClassHasNoInitInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
8 |
+
<inspection_tool class="PyFinalInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
9 |
+
<inspection_tool class="PyGlobalUndefinedInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
10 |
+
<inspection_tool class="PyMethodMayBeStaticInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
11 |
+
<inspection_tool class="PyMissingConstructorInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
12 |
+
<inspection_tool class="PyPep8Inspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
13 |
+
<inspection_tool class="PyPep8NamingInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
14 |
+
<inspection_tool class="PyProtocolInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
15 |
+
<inspection_tool class="PyRedeclarationInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
16 |
+
<inspection_tool class="PyShadowingNamesInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
17 |
+
<inspection_tool class="PySimplifyBooleanCheckInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
18 |
+
<inspection_tool class="PyStatementEffectInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
19 |
+
<inspection_tool class="PyUnboundLocalVariableInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
20 |
+
<inspection_tool class="PyUnusedLocalInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
21 |
+
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
|
22 |
+
<option name="processCode" value="true" />
|
23 |
+
<option name="processLiterals" value="true" />
|
24 |
+
<option name="processComments" value="true" />
|
25 |
+
</inspection_tool>
|
26 |
+
</profile>
|
27 |
+
</component>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<settings>
|
3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
4 |
+
<version value="1.0" />
|
5 |
+
</settings>
|
6 |
+
</component>
|
.idea/misc.xml
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (data)" project-jdk-type="Python SDK" />
|
4 |
+
</project>
|
.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectModuleManager">
|
4 |
+
<modules>
|
5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/final-project-flight-never-delay.iml" filepath="$PROJECT_DIR$/.idea/final-project-flight-never-delay.iml" />
|
6 |
+
</modules>
|
7 |
+
</component>
|
8 |
+
</project>
|
.idea/vcs.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="VcsDirectoryMappings">
|
4 |
+
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
5 |
+
</component>
|
6 |
+
</project>
|
.streamlit/config.toml
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
[server]
|
2 |
+
maxMessageSize = 500
|
__pycache__/constants.cpython-310.pyc
ADDED
Binary file (2.23 kB). View file
|
|
__pycache__/constants.cpython-311.pyc
ADDED
Binary file (2.28 kB). View file
|
|
__pycache__/constants.cpython-38.pyc
ADDED
Binary file (3.61 kB). View file
|
|
__pycache__/constants.cpython-39.pyc
ADDED
Binary file (2.24 kB). View file
|
|
__pycache__/introduction.cpython-310.pyc
ADDED
Binary file (826 Bytes). View file
|
|
__pycache__/introduction.cpython-38.pyc
ADDED
Binary file (815 Bytes). View file
|
|
__pycache__/introduction.cpython-39.pyc
ADDED
Binary file (811 Bytes). View file
|
|
__pycache__/prediction.cpython-310.pyc
ADDED
Binary file (2.7 kB). View file
|
|
__pycache__/prediction.cpython-38.pyc
ADDED
Binary file (590 Bytes). View file
|
|
__pycache__/prediction.cpython-39.pyc
ADDED
Binary file (586 Bytes). View file
|
|
__pycache__/visualization.cpython-310.pyc
ADDED
Binary file (7.69 kB). View file
|
|
__pycache__/visualization.cpython-38.pyc
ADDED
Binary file (7.79 kB). View file
|
|
__pycache__/visualization.cpython-39.pyc
ADDED
Binary file (6.22 kB). View file
|
|
constants.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
delay_category = {
|
2 |
+
"carrier_delay": 26.715905850648916,
|
3 |
+
"weather_delay": 4.561987648962998,
|
4 |
+
"nas_delay": 11.274788825104947,
|
5 |
+
"security_delay": 0.21346667066188915,
|
6 |
+
"late_aircraft_delay": 23.864074798297647
|
7 |
+
}
|
8 |
+
|
9 |
+
airline_name = {
|
10 |
+
"WN": "Southwest Airlines",
|
11 |
+
"AA": "American Airlines",
|
12 |
+
"OO": "Skywest Airlines",
|
13 |
+
"DL": "Delta Airlines",
|
14 |
+
"UA": "United Airlines",
|
15 |
+
"B6": "JetBlue Airlines",
|
16 |
+
"YX": "Republic Airways",
|
17 |
+
"NK": "Spirit Airlines",
|
18 |
+
"MQ": "American Eagle Airlines",
|
19 |
+
"AS": "Alaska Airlines",
|
20 |
+
}
|
21 |
+
|
22 |
+
airports = ['ABE', 'ABI', 'ABQ', 'ABR', 'ABY', 'ACK', 'ACT', 'ACV',
|
23 |
+
'ACY', 'ADK', 'ADQ', 'AEX', 'AGS', 'AKN', 'ALB', 'ALO', 'AMA', 'ANC',
|
24 |
+
'APN', 'ASE', 'ATL', 'ATW', 'AUS', 'AVL', 'AVP', 'AZO', 'BDL', 'BET',
|
25 |
+
'BFL', 'BGM', 'BGR', 'BHM', 'BIL', 'BIS', 'BJI', 'BLI', 'BMI', 'BNA',
|
26 |
+
'BOI', 'BOS', 'BPT', 'BQK', 'BQN', 'BRD', 'BRO', 'BRW', 'BTM', 'BTR',
|
27 |
+
'BTV', 'BUF', 'BUR', 'BWI', 'BZN', 'CAE', 'CAK', 'CDC', 'CDV', 'CHA',
|
28 |
+
'CHO', 'CHS', 'CID', 'CIU', 'CLE', 'CLL', 'CLT', 'CMH', 'CMI', 'CMX',
|
29 |
+
'CNY', 'COD', 'COS', 'COU', 'CPR', 'CRP', 'CRW', 'CSG', 'CVG', 'CWA',
|
30 |
+
'DAB', 'DAL', 'DAY', 'DBQ', 'DCA', 'DEN', 'DFW', 'DHN', 'DIK', 'DLG',
|
31 |
+
'DLH', 'DRO', 'DSM', 'DTW', 'DVL', 'EAU', 'EGE', 'EKO', 'ELM', 'ELP',
|
32 |
+
'ERI', 'ESC', 'EUG', 'EVV', 'EWN', 'EWR', 'EYW', 'FAI', 'FAR', 'FAT',
|
33 |
+
'FAY', 'FCA', 'FLG', 'FLL', 'FNT', 'FSD', 'FSM', 'FWA', 'GCC', 'GCK',
|
34 |
+
'GEG', 'GFK', 'GGG', 'GJT', 'GNV', 'GPT', 'GRB', 'GRI', 'GRK', 'GRR',
|
35 |
+
'GSO', 'GSP', 'GST', 'GTF', 'GTR', 'GUC', 'GUM', 'HDN', 'HIB', 'HLN',
|
36 |
+
'HNL', 'HOB', 'HOU', 'HPN', 'HRL', 'HSV', 'HYA', 'HYS', 'IAD', 'IAH',
|
37 |
+
'ICT', 'IDA', 'ILG', 'ILM', 'IMT', 'IND', 'INL', 'ISP', 'ITH', 'ITO',
|
38 |
+
'JAC', 'JAN', 'JAX', 'JFK', 'JLN', 'JMS', 'JNU', 'KOA', 'KTN', 'LAN',
|
39 |
+
'LAR', 'LAS', 'LAW', 'LAX', 'LBB', 'LBE', 'LCH', 'LEX', 'LFT', 'LGA',
|
40 |
+
'LGB', 'LIH', 'LIT', 'LNK', 'LRD', 'LSE', 'LWS', 'MAF', 'MBS', 'MCI',
|
41 |
+
'MCO', 'MDT', 'MDW', 'MEI', 'MEM', 'MFE', 'MFR', 'MGM', 'MHK', 'MHT',
|
42 |
+
'MIA', 'MKE', 'MKG', 'MLB', 'MLI', 'MLU', 'MOB', 'MOT', 'MQT', 'MRY',
|
43 |
+
'MSN', 'MSO', 'MSP', 'MSY', 'MTJ', 'MVY', 'MYR', 'OAJ', 'OAK', 'OGG',
|
44 |
+
'OKC', 'OMA', 'OME', 'ONT', 'ORD', 'ORF', 'ORH', 'OTH', 'OTZ', 'PAH',
|
45 |
+
'PBI', 'PDX', 'PHF', 'PHL', 'PHX', 'PIA', 'PIB', 'PIH', 'PIT', 'PLN',
|
46 |
+
'PNS', 'PPG', 'PSC', 'PSE', 'PSG', 'PSP', 'PUB', 'PVD', 'PWM', 'RAP',
|
47 |
+
'RDD', 'RDM', 'RDU', 'RHI', 'RIC', 'RKS', 'RNO', 'ROA', 'ROC', 'ROW',
|
48 |
+
'RST', 'RSW', 'SAF', 'SAN', 'SAT', 'SAV', 'SBA', 'SBN', 'SBP', 'SCC',
|
49 |
+
'SCE', 'SDF', 'SEA', 'SFO', 'SGF', 'SGU', 'SHV', 'SIT', 'SJC', 'SJT',
|
50 |
+
'SJU', 'SLC', 'SMF', 'SMX', 'SNA', 'SPI', 'SPS', 'SRQ', 'STC', 'STL',
|
51 |
+
'STT', 'STX', 'SUN', 'SUX', 'SWF', 'SYR', 'TLH', 'TOL', 'TPA', 'TRI',
|
52 |
+
'TTN', 'TUL', 'TUS', 'TVC', 'TWF', 'TXK', 'TYR', 'TYS', 'VEL', 'VLD',
|
53 |
+
'VPS', 'WRG', 'WYS', 'XNA', 'YAK', 'YUM']
|
pages/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
pages/prediction.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pickle
|
3 |
+
import pandas as pd
|
4 |
+
import sys
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Add the parent directory to the path
|
8 |
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
9 |
+
|
10 |
+
from constants import *
|
11 |
+
import datetime
|
12 |
+
|
13 |
+
transformer_filename = 'saved_models/data_transformer.sav'
|
14 |
+
delay_carrier_filename = 'saved_models/delay_carrier_lin_reg.sav'
|
15 |
+
delay_weather_filename = 'saved_models/delay_weather_lin_reg.sav'
|
16 |
+
delay_nas_filename = 'saved_models/delay_nas_lin_reg.sav'
|
17 |
+
delay_security_filename = 'saved_models/delay_security_lin_reg.sav'
|
18 |
+
delay_late_aircraft_filename = 'saved_models/delay_late_aircraft_lin_reg.sav'
|
19 |
+
cancel_filename = 'saved_models/cancel_log_reg.sav'
|
20 |
+
|
21 |
+
transformer = pickle.load(open(transformer_filename, 'rb'))
|
22 |
+
delay_carrier_lin_reg_model = pickle.load(open(delay_carrier_filename, 'rb'))
|
23 |
+
delay_weather_lin_reg_model = pickle.load(open(delay_weather_filename, 'rb'))
|
24 |
+
delay_nas_lin_reg_model = pickle.load(open(delay_nas_filename, 'rb'))
|
25 |
+
delay_security_lin_reg_model = pickle.load(open(delay_security_filename, 'rb'))
|
26 |
+
delay_late_aircraft_lin_reg_model = pickle.load(open(delay_late_aircraft_filename, 'rb'))
|
27 |
+
cancel_lin_reg_model = pickle.load(open(cancel_filename, 'rb'))
|
28 |
+
|
29 |
+
|
30 |
+
def pred():
|
31 |
+
st.header("Flight Delay and Cancellation Prediction")
|
32 |
+
st.markdown("Fill in the information about your flight below and we will predict how long the flight may be \
|
33 |
+
delayed for (or arrive early) and the probability of the flight being cancelled.")
|
34 |
+
with st.form("flight_details"):
|
35 |
+
valid_input = True
|
36 |
+
c1, c2 = st.columns(2)
|
37 |
+
with c1:
|
38 |
+
airline = st.selectbox("Airline",
|
39 |
+
options=[key + ' : ' + value for key, value in airline_name.items()]).split(" : ")[0]
|
40 |
+
date = st.date_input("Departure Date", value=datetime.date.today(), min_value=datetime.date(2021, 1, 1), max_value=datetime.date(2024, 12, 31))
|
41 |
+
dpt_airport = st.selectbox("From Airport",
|
42 |
+
options=airports, index = 236)
|
43 |
+
dpt_time = int(st.time_input('Expected Departure Time', datetime.time(13, 15)).strftime("%H%M"))
|
44 |
+
with c2:
|
45 |
+
flight_num = st.text_input('Flight Number', '1111')
|
46 |
+
arr_date = st.date_input("Arrival Date", value=datetime.date.today(), min_value=datetime.date(2021, 1, 1), max_value=datetime.date(2024, 12, 31))
|
47 |
+
arv_airport = st.selectbox("To Airport",
|
48 |
+
options=airports, index = 39)
|
49 |
+
arv_time = int(st.time_input('Expected Arrival Time', datetime.time(15, 00)).strftime("%H%M"))
|
50 |
+
if not flight_num.isnumeric() or int(flight_num) < 0 or int(flight_num) > 9999:
|
51 |
+
st.write('Please enter a valid flight number between 1 and 9999.')
|
52 |
+
valid_input = False
|
53 |
+
elif dpt_airport == arv_airport:
|
54 |
+
st.write('Please select different departure and arrival airport.')
|
55 |
+
valid_input = False
|
56 |
+
else:
|
57 |
+
valid_input = True
|
58 |
+
|
59 |
+
month = date.month
|
60 |
+
day = date.day
|
61 |
+
weekday = date.weekday() + 1
|
62 |
+
|
63 |
+
submitted = st.form_submit_button("Predict!")
|
64 |
+
|
65 |
+
if valid_input and submitted:
|
66 |
+
dummy_data = [[month, day, weekday, airline, int(flight_num), dpt_airport, arv_airport, dpt_time, arv_time]]
|
67 |
+
dummy_df = pd.DataFrame(dummy_data, columns=['MONTH', 'DAY_OF_MONTH',
|
68 |
+
'DAY_OF_WEEK', 'OP_UNIQUE_CARRIER', 'OP_CARRIER_FL_NUM',
|
69 |
+
'ORIGIN', 'DEST', 'CRS_DEP_TIME',
|
70 |
+
'CRS_ARR_TIME'])
|
71 |
+
input = transformer.transform(dummy_df)
|
72 |
+
delay_carrier_pred = delay_carrier_lin_reg_model.predict(input)[0]
|
73 |
+
delay_weather_pred = delay_weather_lin_reg_model.predict(input)[0]
|
74 |
+
delay_nas_pred = delay_nas_lin_reg_model.predict(input)[0]
|
75 |
+
delay_security_pred = delay_security_lin_reg_model.predict(input)[0]
|
76 |
+
delay_late_aircraft_pred = delay_late_aircraft_lin_reg_model.predict(input)[0]
|
77 |
+
|
78 |
+
|
79 |
+
cancel_pred = cancel_lin_reg_model.predict_proba(input)[0][1] * 100
|
80 |
+
total_delay = delay_weather_pred + delay_carrier_pred + delay_nas_pred + delay_security_pred + delay_late_aircraft_pred
|
81 |
+
|
82 |
+
|
83 |
+
m1, m2, = st.columns(2)
|
84 |
+
with m1:
|
85 |
+
st.metric(label=f"Your flight may be late for", value=f"{round(total_delay)} minutes")
|
86 |
+
st.metric(label=f"Your flight may be late due to weather for", value=f"{round(delay_weather_pred)} minutes")
|
87 |
+
st.metric(label=f"Your flight may be late due to nas for", value=f"{round(delay_nas_pred)} minutes")
|
88 |
+
st.metric(label=f"Your flight may be late due to late aircraft for", value=f"{round(delay_late_aircraft_pred)} minutes")
|
89 |
+
with m2:
|
90 |
+
st.metric(label=f"The probability of your flight being cancelled is", value=f"{round(cancel_pred)}%")
|
91 |
+
st.metric(label=f"Your flight may be late due to carrier for", value=f"{round(delay_carrier_pred)} minutes")
|
92 |
+
st.metric(label=f"Your flight may be late due to security for", value=f"{round(delay_security_pred)} minutes")
|
93 |
+
|
94 |
+
pred()
|
pages/visualization.py
ADDED
@@ -0,0 +1,615 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from constants import airline_name
|
4 |
+
import seaborn as sns
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from vega_datasets import data
|
7 |
+
import altair as alt
|
8 |
+
|
9 |
+
@st.cache
|
10 |
+
def load_airline_data():
|
11 |
+
path = "data/airline.csv"
|
12 |
+
return pd.read_csv(path)
|
13 |
+
|
14 |
+
@st.cache
|
15 |
+
def load_time_data():
|
16 |
+
path = "data/time.csv"
|
17 |
+
return pd.read_csv(path)
|
18 |
+
|
19 |
+
@st.cache
|
20 |
+
def load_distance_data():
|
21 |
+
path = "data/distance.csv"
|
22 |
+
return pd.read_csv(path)
|
23 |
+
|
24 |
+
@st.cache
|
25 |
+
def load_destination_data():
|
26 |
+
path = "data/destination.csv"
|
27 |
+
return pd.read_csv(path)
|
28 |
+
|
29 |
+
@st.cache
|
30 |
+
def load_airline_time_data():
|
31 |
+
path = "data/airline_time.csv"
|
32 |
+
return pd.read_csv(path)
|
33 |
+
|
34 |
+
@st.cache
|
35 |
+
def load_route_data():
|
36 |
+
path = "data/routes.csv"
|
37 |
+
return pd.read_csv(path)
|
38 |
+
|
39 |
+
@st.cache
|
40 |
+
def load_origin_data():
|
41 |
+
path = "data/origin.csv"
|
42 |
+
return pd.read_csv(path)
|
43 |
+
|
44 |
+
def vis_airline_company():
|
45 |
+
st.markdown(
|
46 |
+
"""
|
47 |
+
## Chapter I. Check out factors that matter
|
48 |
+
"""
|
49 |
+
)
|
50 |
+
st.markdown(
|
51 |
+
"""
|
52 |
+
### 1. Airline company matters to flight delay!
|
53 |
+
"""
|
54 |
+
)
|
55 |
+
st.markdown(
|
56 |
+
"""
|
57 |
+
Flight delay rate is an important factor when measuring the reliability of an airline company. \n
|
58 |
+
**"Which airline has the worst/best delay rate in 2021?"** \n
|
59 |
+
In this part, we have selected the top 10 airlines with the most flights in 2021 and would like to explore how they behave in dealing with flight delay issues. \n
|
60 |
+
Firstly, we compared the delay time in a box plot. The median and the interquartile range in each box well demonstrate the performance of each airline in a flight delay. Alaska Airlines, Southwest Airlines, and Delta Airlines are the best 3 performers. Some airlines like Skywest Airlines and JetBlue Airlines do need to pay more attention to their flight delay issues.
|
61 |
+
"""
|
62 |
+
)
|
63 |
+
|
64 |
+
|
65 |
+
df = load_airline_data()
|
66 |
+
airlines = st.multiselect("Please choose airline companies",
|
67 |
+
options=[key + ':' + value for key, value in airline_name.items()],
|
68 |
+
default=["WN:Southwest Airlines", "AA:American Airlines",
|
69 |
+
"DL:Delta Airlines", "UA:United Airlines", "B6:JetBlue Airlines"],
|
70 |
+
key='airlines')
|
71 |
+
airlines_abbr = [x.split(":")[0] for x in airlines]
|
72 |
+
df_airline = df[df["OP_UNIQUE_CARRIER"].isin(airlines_abbr)]
|
73 |
+
|
74 |
+
def delay_type(x):
|
75 |
+
if x <= 20:
|
76 |
+
return 0
|
77 |
+
elif x <= 60:
|
78 |
+
return 1
|
79 |
+
return 2
|
80 |
+
df_airline["TYPE"] = df_airline["ARR_DELAY"].apply(delay_type)
|
81 |
+
|
82 |
+
fig2 = plt.figure(figsize=(10, len(airlines_abbr) * 1.5))
|
83 |
+
ax = sns.boxplot(data=df_airline, x="ARR_DELAY", y="OP_UNIQUE_CARRIER", showfliers=False, width=0.4, palette='Spectral')
|
84 |
+
ax.yaxis.label.set_visible(False)
|
85 |
+
# change the plot box color
|
86 |
+
for i, artist in enumerate(ax.patches):
|
87 |
+
# Set the linecolor on the artist to the facecolor, and set the facecolor to None
|
88 |
+
col = artist.get_facecolor()
|
89 |
+
artist.set_edgecolor(col)
|
90 |
+
|
91 |
+
new_labels = [airline_name[x.get_text()] for x in ax.get_yticklabels()]
|
92 |
+
ax.set_yticklabels(new_labels)
|
93 |
+
plt.setp(ax.get_yticklabels(), fontsize=10, weight='bold', rotation=0)
|
94 |
+
plt.setp(ax.get_xticklabels(), fontsize=10, weight='bold', rotation=0)
|
95 |
+
plt.xlabel('Delay time (minutes)', fontsize=16, weight='bold', labelpad=10)
|
96 |
+
st.pyplot(fig2)
|
97 |
+
|
98 |
+
st.markdown(
|
99 |
+
"""
|
100 |
+
##### Insights
|
101 |
+
"""
|
102 |
+
)
|
103 |
+
st.markdown(
|
104 |
+
"""
|
105 |
+
- **Alaska Airlines, Southwest Airlines, and Delta Airlines are the best 3 performers.** \n
|
106 |
+
- **Some airlines like Skywest Airlines and JetBlue Airlines do need to pay more attention to their flight delay issues.** \n
|
107 |
+
\n
|
108 |
+
"""
|
109 |
+
)
|
110 |
+
|
111 |
+
st.markdown(
|
112 |
+
"""
|
113 |
+
Based on the box plot, we identified that the delay time could be categorized into <20 minutes, 20-60 minutes, and >60 minutes. If it is smaller than 20, it is approximately on time. If it is between 20 and 60, it is a small delay that most people can tolerate. If it is larger than 60, the flight encounters a serious delay. \n
|
114 |
+
With the new categories of delay, it is easy to observe that **the proportion of delays varies from airline to airline.**
|
115 |
+
"""
|
116 |
+
)
|
117 |
+
|
118 |
+
fig1 = plt.figure(figsize=(10, len(airlines_abbr)*1.5))
|
119 |
+
ax = sns.countplot(data=df_airline, y="OP_UNIQUE_CARRIER", hue="TYPE", palette='Spectral')
|
120 |
+
ax.yaxis.label.set_visible(False)
|
121 |
+
new_labels = [airline_name[x.get_text()] for x in ax.get_yticklabels()]
|
122 |
+
ax.set_yticklabels(new_labels)
|
123 |
+
plt.setp(ax.get_yticklabels(), fontsize=10, weight='bold', rotation=0)
|
124 |
+
plt.setp(ax.get_xticklabels(), fontsize=10, weight='bold', rotation=0)
|
125 |
+
new_legend = plt.legend()
|
126 |
+
new_legend.get_texts()[0].set_text("on time (<20 min)")
|
127 |
+
new_legend.get_texts()[1].set_text("small delay (20~60 min)")
|
128 |
+
new_legend.get_texts()[2].set_text("large delay (>60 min)")
|
129 |
+
plt.xlabel('Number of flights', fontsize=16, weight='bold', labelpad=10)
|
130 |
+
st.pyplot(fig1)
|
131 |
+
|
132 |
+
st.markdown(
|
133 |
+
"""
|
134 |
+
##### Insights
|
135 |
+
"""
|
136 |
+
)
|
137 |
+
st.markdown(
|
138 |
+
"""
|
139 |
+
- **Airline companies behave very much differently in dealing with flight delay problems. It is safe to deduce that choosing an airline company would influence flight delays.** \n
|
140 |
+
- **Some airlines like JetBlue Airlines behave badly. They have a relatively low on-time rate and a relatively high large delay rate.** \n
|
141 |
+
- **Although SouthWest Airlines has the largest number of flight delays, these delays are mainly small delays and the company maintains a pretty good on-time rate** \n
|
142 |
+
\n
|
143 |
+
"""
|
144 |
+
)
|
145 |
+
|
146 |
+
def vis_flight_time():
|
147 |
+
st.markdown(
|
148 |
+
"""
|
149 |
+
### 2. Prepared to be delayed when flying in summer and December
|
150 |
+
"""
|
151 |
+
)
|
152 |
+
st.markdown(
|
153 |
+
"""
|
154 |
+
Next, we would like to sketch a box plot to identify the relationship between delay time and flight departure time. Here, we allow users to select departure time by Quarter, Month, and Day of Week.
|
155 |
+
"""
|
156 |
+
)
|
157 |
+
|
158 |
+
time_scale = st.radio("Please select a time scale",
|
159 |
+
("Quarter", "Month", "Day of Week"))
|
160 |
+
df_time = load_time_data()
|
161 |
+
if time_scale == "Quarter":
|
162 |
+
fig = plt.figure(figsize=(10, 4*1.5))
|
163 |
+
ax = sns.boxplot(data=df_time, x="ARR_DELAY", y="QUARTER", showfliers=False, orient="h", width=0.4, palette='Spectral')
|
164 |
+
# change the plot box color
|
165 |
+
for i, artist in enumerate(ax.patches):
|
166 |
+
# Set the linecolor on the artist to the facecolor, and set the facecolor to None
|
167 |
+
col = artist.get_facecolor()
|
168 |
+
artist.set_edgecolor(col)
|
169 |
+
plt.setp(ax.get_yticklabels(), fontsize=10, weight='bold', rotation=0)
|
170 |
+
plt.setp(ax.get_xticklabels(), fontsize=10, weight='bold', rotation=0)
|
171 |
+
plt.ylabel('Quarter', fontsize=16, weight='bold', labelpad=10)
|
172 |
+
plt.xlabel('Delay time (minutes)', fontsize=16, weight='bold', labelpad=10)
|
173 |
+
st.pyplot(fig)
|
174 |
+
elif time_scale == "Month":
|
175 |
+
st.write("It can be observed that summer is the peak season for flight delays.")
|
176 |
+
fig = plt.figure(figsize=(10, 12 * 1))
|
177 |
+
ax = sns.boxplot(data=df_time, x="ARR_DELAY", y="MONTH", showfliers=False, orient="h", width=0.4, palette='Spectral')
|
178 |
+
for i, artist in enumerate(ax.patches):
|
179 |
+
# Set the linecolor on the artist to the facecolor, and set the facecolor to None
|
180 |
+
col = artist.get_facecolor()
|
181 |
+
artist.set_edgecolor(col)
|
182 |
+
plt.setp(ax.get_yticklabels(), fontsize=10, weight='bold', rotation=0)
|
183 |
+
plt.setp(ax.get_xticklabels(), fontsize=10, weight='bold', rotation=0)
|
184 |
+
plt.ylabel('Month', fontsize=16, weight='bold', labelpad=10)
|
185 |
+
plt.xlabel('Delay time (minutes)', fontsize=16, weight='bold', labelpad=10)
|
186 |
+
st.pyplot(fig)
|
187 |
+
else:
|
188 |
+
st.write("It can be observed that it is more likely to encounter a flight delay on Sunday.")
|
189 |
+
fig = plt.figure(figsize=(10, 7 * 1))
|
190 |
+
ax = sns.boxplot(data=df_time, x="ARR_DELAY", y="DAY_OF_WEEK", showfliers=False, orient="h", width=0.4, palette='Spectral')
|
191 |
+
for i, artist in enumerate(ax.patches):
|
192 |
+
# Set the linecolor on the artist to the facecolor, and set the facecolor to None
|
193 |
+
col = artist.get_facecolor()
|
194 |
+
artist.set_edgecolor(col)
|
195 |
+
plt.setp(ax.get_yticklabels(), fontsize=10, weight='bold', rotation=0)
|
196 |
+
plt.setp(ax.get_xticklabels(), fontsize=10, weight='bold', rotation=0)
|
197 |
+
plt.ylabel('Day of Week', fontsize=16, weight='bold', labelpad=10)
|
198 |
+
plt.xlabel('Delay time (minutes)', fontsize=16, weight='bold', labelpad=10)
|
199 |
+
st.pyplot(fig)
|
200 |
+
|
201 |
+
st.markdown(
|
202 |
+
"""
|
203 |
+
##### Insights
|
204 |
+
"""
|
205 |
+
)
|
206 |
+
st.markdown(
|
207 |
+
"""
|
208 |
+
- **Generally, a long flight delay is more likely to happen in Quarter 2 & 3 than in Quarter 1 & 4.** \n
|
209 |
+
- **However, there is also a relatively long flight delay in December, which might be caused by the holiday season.** \n
|
210 |
+
- **People are more likely to experience a long flight delay on Sundays possibly due to a high volume of people needing to go back to work from the weekends.** \n
|
211 |
+
\n
|
212 |
+
"""
|
213 |
+
)
|
214 |
+
|
215 |
+
def vis_flight_distance():
|
216 |
+
st.markdown(
|
217 |
+
"""
|
218 |
+
### 3. Likely to be 30-min delay regardless of the distance
|
219 |
+
"""
|
220 |
+
)
|
221 |
+
st.markdown(
|
222 |
+
"""
|
223 |
+
Next, we would like to sketch a binned scattered plot to identify the relationship between delay time and flight distance. The data points in the plot are grouped into bins with a circle in each bin to represent the number of flights in that bin and its percentage of the total number of flights.
|
224 |
+
"""
|
225 |
+
)
|
226 |
+
|
227 |
+
data_type = st.radio("Please select a data type",
|
228 |
+
("Amount", "Percentage"))
|
229 |
+
df = load_distance_data()
|
230 |
+
df_distance = df[["DISTANCE", "ARR_DELAY"]]
|
231 |
+
def delay_type(x):
|
232 |
+
if x <= 20:
|
233 |
+
return 0
|
234 |
+
elif x <= 60:
|
235 |
+
return 1
|
236 |
+
return 2
|
237 |
+
df_distance["TYPE"] = df_distance["ARR_DELAY"].apply(delay_type)
|
238 |
+
df_distance = df_distance[df_distance["ARR_DELAY"] < 120]
|
239 |
+
df_distance = df_distance[df_distance["DISTANCE"] < 2000]
|
240 |
+
df_distance = df_distance.sample(50000, random_state=0)
|
241 |
+
|
242 |
+
# fig = plt.figure(figsize=(10, 10))
|
243 |
+
# ax = sns.histplot(data=df_distance, x="ARR_DELAY", y="DISTANCE", bins=20)
|
244 |
+
# plt.setp(ax.get_yticklabels(), fontsize=10, weight='bold', rotation=0)
|
245 |
+
# plt.setp(ax.get_xticklabels(), fontsize=10, weight='bold', rotation=0)
|
246 |
+
# plt.ylabel('Flight Distance (miles)', fontsize=16, weight='bold', labelpad=10)
|
247 |
+
# plt.xlabel('Delay time (minutes)', fontsize=16, weight='bold', labelpad=10)
|
248 |
+
# st.pyplot(fig)
|
249 |
+
|
250 |
+
if data_type == "Percentage":
|
251 |
+
fig1 = alt.Chart(df_distance).transform_bin(
|
252 |
+
"ARR_DELAY_bin", field="ARR_DELAY"
|
253 |
+
).transform_joinaggregate(
|
254 |
+
total="count()",
|
255 |
+
groupby=["ARR_DELAY_bin"]
|
256 |
+
).transform_joinaggregate(
|
257 |
+
in_group="count()",
|
258 |
+
groupby=["ARR_DELAY_bin", "DISTANCE"]
|
259 |
+
).transform_calculate(
|
260 |
+
percentage=alt.datum.in_group / alt.datum.total
|
261 |
+
).mark_circle(color= '#66c2a5').encode(
|
262 |
+
alt.X("ARR_DELAY:Q", bin=True, axis=alt.Axis(title="Delay Time (minutes)", titleFontSize=20)),
|
263 |
+
alt.Y("DISTANCE", bin=True, axis=alt.Axis(title="Flight Distance (miles)", titleFontSize=20)),
|
264 |
+
alt.Size("percentage:Q", legend=alt.Legend(format='%', title='Percentage', titleFontSize=15)),
|
265 |
+
tooltip=[alt.Tooltip('percentage:Q')]
|
266 |
+
)
|
267 |
+
fig1.width = 800
|
268 |
+
fig1.height = 400
|
269 |
+
st.altair_chart(fig1)
|
270 |
+
else:
|
271 |
+
fig2 = alt.Chart(df_distance).mark_circle(color= '#66c2a5').encode(
|
272 |
+
alt.X("ARR_DELAY:Q", bin=True, axis=alt.Axis(title="Delay Time (minutes)", titleFontSize=20)),
|
273 |
+
alt.Y("DISTANCE:Q", bin=True, axis=alt.Axis(title="Flight Distance (miles)", titleFontSize=20)),
|
274 |
+
size="count()",
|
275 |
+
tooltip=["count()"]
|
276 |
+
)
|
277 |
+
fig2.width = 800
|
278 |
+
fig2.height = 400
|
279 |
+
st.altair_chart(fig2)
|
280 |
+
|
281 |
+
st.markdown(
|
282 |
+
"""
|
283 |
+
##### Insights
|
284 |
+
"""
|
285 |
+
)
|
286 |
+
st.markdown(
|
287 |
+
"""
|
288 |
+
- **Flights usually delay around 30 minutes regardless of the travel distance.** \n
|
289 |
+
- **Surprisingly, long distance does not have a longer delay time, which might be due to the flights can fly faster to make up the lost time and mitigate the arrival delay.** \n
|
290 |
+
\n
|
291 |
+
"""
|
292 |
+
)
|
293 |
+
|
294 |
+
|
295 |
+
def origin_map():
|
296 |
+
states = alt.topo_feature(data.us_10m.url, feature='states')
|
297 |
+
base = alt.Chart(states).mark_geoshape(fill='lightgray', stroke='black', strokeWidth=0.5)
|
298 |
+
df = load_origin_data()
|
299 |
+
ansi = pd.read_csv('https://www2.census.gov/geo/docs/reference/state.txt', sep='|')
|
300 |
+
ansi.columns = ['id', 'abbr', 'state', 'statens']
|
301 |
+
ansi = ansi[['id', 'state', 'abbr']]
|
302 |
+
geo_data = df.groupby('ORIGIN_STATE')['ARR_DELAY'].mean().reset_index()
|
303 |
+
geo_data.columns = ['abbr', 'Delay Time']
|
304 |
+
geo_data = pd.merge(geo_data, ansi, how='left', on='abbr')
|
305 |
+
alt_fig = alt.Chart(states).mark_geoshape().encode(
|
306 |
+
color = alt.Color('Delay Time:Q', scale=alt.Scale(scheme='lightmulti')),
|
307 |
+
tooltip=['state:N', alt.Tooltip('Delay Time:Q')]
|
308 |
+
).transform_lookup(
|
309 |
+
lookup='id',
|
310 |
+
from_=alt.LookupData(geo_data, 'id', ['Delay Time','state'])
|
311 |
+
).project(
|
312 |
+
type='albersUsa'
|
313 |
+
).properties(
|
314 |
+
width=800
|
315 |
+
)
|
316 |
+
return base + alt_fig
|
317 |
+
|
318 |
+
|
319 |
+
def vis_flight_origin():
|
320 |
+
st.markdown(
|
321 |
+
"""
|
322 |
+
### 4. Peace in mind when flying ‘In-N-Out’ on the West Coast
|
323 |
+
"""
|
324 |
+
)
|
325 |
+
st.markdown(
|
326 |
+
"""
|
327 |
+
We utilized choropleth maps to visualize the average flight delay time in different departure states and destination states. The closer the color is to red, the longer the delay in that state.
|
328 |
+
"""
|
329 |
+
)
|
330 |
+
st.markdown(
|
331 |
+
"""
|
332 |
+
**Flight Departure:**
|
333 |
+
"""
|
334 |
+
)
|
335 |
+
st.altair_chart(origin_map())
|
336 |
+
|
337 |
+
def destination_map():
|
338 |
+
states = alt.topo_feature(data.us_10m.url, feature='states')
|
339 |
+
base = alt.Chart(states).mark_geoshape(fill='lightgray', stroke='black', strokeWidth=0.5)
|
340 |
+
df = load_destination_data()
|
341 |
+
ansi = pd.read_csv('https://www2.census.gov/geo/docs/reference/state.txt', sep='|')
|
342 |
+
ansi.columns = ['id', 'abbr', 'state', 'statens']
|
343 |
+
ansi = ansi[['id', 'state', 'abbr']]
|
344 |
+
geo_data = df.groupby('DEST_STATE')['ARR_DELAY'].mean().reset_index()
|
345 |
+
geo_data.columns = ['abbr', 'Delay Time']
|
346 |
+
geo_data = pd.merge(geo_data, ansi, how='left', on='abbr')
|
347 |
+
alt_fig = alt.Chart(states).mark_geoshape().encode(
|
348 |
+
color = alt.Color('Delay Time:Q', scale=alt.Scale(scheme='lightmulti')),
|
349 |
+
tooltip=['state:N', alt.Tooltip('Delay Time:Q')]
|
350 |
+
).transform_lookup(
|
351 |
+
lookup='id',
|
352 |
+
from_=alt.LookupData(geo_data, 'id', ['Delay Time','state'])
|
353 |
+
).project(
|
354 |
+
type='albersUsa'
|
355 |
+
).properties(
|
356 |
+
width=800
|
357 |
+
)
|
358 |
+
return base + alt_fig
|
359 |
+
|
360 |
+
def vis_flight_destination():
|
361 |
+
st.markdown(
|
362 |
+
"""
|
363 |
+
**Flight Destination:**
|
364 |
+
"""
|
365 |
+
)
|
366 |
+
st.altair_chart(destination_map())
|
367 |
+
|
368 |
+
st.markdown(
|
369 |
+
"""
|
370 |
+
##### Insights
|
371 |
+
"""
|
372 |
+
)
|
373 |
+
st.markdown(
|
374 |
+
"""
|
375 |
+
- **Departure and arrival flights in North Dakota are likely to be delayed for a longer period of time.** \n
|
376 |
+
- **Flights arriving on the West Coast are more likely to be on time.** \n
|
377 |
+
- **Flights departing from Washington and Georgia are more likely to be on time.** \n
|
378 |
+
\n
|
379 |
+
"""
|
380 |
+
)
|
381 |
+
st.markdown(
|
382 |
+
"""
|
383 |
+
### Summary
|
384 |
+
"""
|
385 |
+
)
|
386 |
+
st.markdown(
|
387 |
+
"""
|
388 |
+
For this part of the visualization, we mostly employed a single factor and identified that airline company, flight time, and departure/arriving hub are the key factors that correlate with a flight delay.
|
389 |
+
"""
|
390 |
+
)
|
391 |
+
|
392 |
+
st.markdown(
|
393 |
+
"""
|
394 |
+
## Chapter II. Make a better decision by integrating multiple factors
|
395 |
+
"""
|
396 |
+
)
|
397 |
+
st.markdown(
|
398 |
+
"""
|
399 |
+
In this Chapter, we are going to help our users make better decisions by taking multiple factors into consideration.
|
400 |
+
"""
|
401 |
+
)
|
402 |
+
|
403 |
+
def vis_flight_delay_distribution_over_time():
|
404 |
+
st.markdown(
|
405 |
+
"""
|
406 |
+
### 1. Watch out for your timing and your carrier
|
407 |
+
"""
|
408 |
+
)
|
409 |
+
st.markdown(
|
410 |
+
"""
|
411 |
+
In order to see how timings and airline companies would influence the flight delay together, we would like to use an interactive chart with cross-highlights. We randomly sampled 5000 from the data for efficient computation purposes. Here in the chart, the color indicates the number of flights with the delay time (y-axis) on the given time category (x-axis) for a selected time scale. The size of the circle indicates the percentage (likelihood) of a delay time occurring in each time category. Meanwhile, users can filter an airline by selecting from the bar plot below and see the performance of different airlines.
|
412 |
+
|
413 |
+
"""
|
414 |
+
)
|
415 |
+
scale = st.radio("Please select a time scale",
|
416 |
+
("Quarter", "Month", "Day of Week"), key='scale')
|
417 |
+
st.altair_chart(plot_delay_over_time(scale))
|
418 |
+
|
419 |
+
st.markdown(
|
420 |
+
"""
|
421 |
+
##### Insights
|
422 |
+
"""
|
423 |
+
)
|
424 |
+
st.markdown(
|
425 |
+
"""
|
426 |
+
- **Comparing United Airlines with Delta Airlines (with a similar number of flights), we can see United Airlines is more likely to have longer delays than Delta.** \n
|
427 |
+
- **Longer delays (350-400min) happen more frequently in December compared to the other months, which might be caused by the holiday season.** \n
|
428 |
+
- **The shorter and medium delays (50-300min) happen more frequently in June-August, which might indicate that people travel more often in the summertime.** \n
|
429 |
+
- **There are fewer flights from January-April, which might indicate that fewer people choose to travel during this time.** \n
|
430 |
+
\n
|
431 |
+
"""
|
432 |
+
)
|
433 |
+
|
434 |
+
|
435 |
+
def plot_delay_over_time(scale):
|
436 |
+
|
437 |
+
time_scale_dic = {"Quarter": "QUARTER", "Month": "MONTH", "Day of Week": "DAY_OF_WEEK"}
|
438 |
+
airlines = ['WN','AA','OO','DL','UA','B6','YX','NK']
|
439 |
+
df_time = load_airline_time_data()
|
440 |
+
df_time = df_time[df_time["ARR_DELAY"] < 400]
|
441 |
+
df_time = df_time[df_time["OP_UNIQUE_CARRIER"].isin(airlines)]
|
442 |
+
df_time = df_time.sample(5000, random_state=0)
|
443 |
+
|
444 |
+
pts = alt.selection(type="single", encodings=['x'])
|
445 |
+
# "QUARTER" "%s:N"%(time_scale_dic[scale])
|
446 |
+
|
447 |
+
rect = alt.Chart(df_time).mark_rect().encode(
|
448 |
+
alt.Y("ARR_DELAY:Q", bin=True),
|
449 |
+
alt.X("%s:N"%(time_scale_dic[scale])),
|
450 |
+
color = alt.Color("count()", scale=alt.Scale(scheme='lightmulti'))
|
451 |
+
).transform_filter(
|
452 |
+
pts
|
453 |
+
)
|
454 |
+
rect.width = 700
|
455 |
+
rect.height = 400
|
456 |
+
|
457 |
+
circle = alt.Chart(df_time).transform_bin(
|
458 |
+
"ARR_DELAY_bin", field="ARR_DELAY"
|
459 |
+
).transform_filter(
|
460 |
+
pts
|
461 |
+
).transform_joinaggregate(
|
462 |
+
total="count()",
|
463 |
+
groupby=["ARR_DELAY_bin"]
|
464 |
+
).transform_joinaggregate(
|
465 |
+
in_group="count()",
|
466 |
+
groupby=["ARR_DELAY_bin", "%s"%(time_scale_dic[scale])]
|
467 |
+
).transform_calculate(
|
468 |
+
PERCENT_BY_ARR_DELAY=alt.datum.in_group / alt.datum.total
|
469 |
+
).mark_circle(color= '#66c2a5').encode(
|
470 |
+
alt.Y("ARR_DELAY:Q", bin=True, axis=alt.Axis(title="Delay Time (minutes)", titleFontSize=14)),
|
471 |
+
alt.X("%s:N"%(time_scale_dic[scale]), axis=alt.Axis(title=scale, titleFontSize=14, labelAngle=0)),
|
472 |
+
alt.Size("PERCENT_BY_ARR_DELAY:Q", scale=alt.Scale(range=[0, 2000]), legend=alt.Legend(format='%', title='Percentage')),
|
473 |
+
tooltip=["%s:N"%(time_scale_dic[scale]), "count()", alt.Tooltip('PERCENT_BY_ARR_DELAY:Q', format='.2f')]
|
474 |
+
)
|
475 |
+
circle.width = 700
|
476 |
+
circle.height = 400
|
477 |
+
|
478 |
+
bar = alt.Chart(df_time).mark_bar().encode(
|
479 |
+
x=alt.X('OP_UNIQUE_CARRIER:N', sort='-y', axis=alt.Axis(title="Operating Carrier", labelAngle=0, titleFontSize=14)),
|
480 |
+
y=alt.Y('count()', axis=alt.Axis(titleFontSize=14)),
|
481 |
+
color=alt.condition(pts, alt.ColorValue("#5aa6bb"), alt.ColorValue("lightgrey")),
|
482 |
+
tooltip=['OP_UNIQUE_CARRIER:N', 'count()']
|
483 |
+
).properties(
|
484 |
+
width=750,
|
485 |
+
height=200
|
486 |
+
).add_selection(pts)
|
487 |
+
|
488 |
+
fig = alt.vconcat(
|
489 |
+
rect + circle,
|
490 |
+
bar
|
491 |
+
).resolve_legend(
|
492 |
+
color="independent",
|
493 |
+
size="independent"
|
494 |
+
)
|
495 |
+
|
496 |
+
return fig
|
497 |
+
|
498 |
+
|
499 |
+
|
500 |
+
def vis_flight_delay_distribution_over_location():
|
501 |
+
st.markdown(
|
502 |
+
"""
|
503 |
+
### 2. Worry less when traveling between big hubs
|
504 |
+
"""
|
505 |
+
)
|
506 |
+
st.markdown(
|
507 |
+
"""
|
508 |
+
Now that we understand how the timings and airline companies could influence the flight delay, we would like to explore the relationship between average delay time and the route of the flights. To better visualize the frequency of the route, we also included the number of flights on each route consideration. \n
|
509 |
+
The circle indicates the number of flights that departed from this airport. The larger circle means the larger airport hub. The edge indicates the flight between the two airports. The thickness of the edge indicates the number of flights between the two airports and the color of the edge indicates the average delay time between the two airports. Randomly sample 5000 from the data for efficient computation purposes.
|
510 |
+
"""
|
511 |
+
)
|
512 |
+
st.altair_chart(plot_delay_over_location())
|
513 |
+
st.markdown(
|
514 |
+
"""
|
515 |
+
##### Insights
|
516 |
+
"""
|
517 |
+
)
|
518 |
+
st.markdown(
|
519 |
+
"""
|
520 |
+
- **The average delay time of flights between two big hubs is shorter than that of flights from a big hub to a smaller airport, which might be due to the airports prioritizing flights between two big hubs. For example, hovering over DEN, we could see that the longest average delays are among small airports (circles with a smaller size).** \n
|
521 |
+
- **Smaller airports usually only connect to a few big hubs and are likely to have longer average delay times. For example, airport CRW only have flights to ORD and ATL, both having large average delay time.** \n
|
522 |
+
\n
|
523 |
+
"""
|
524 |
+
)
|
525 |
+
st.markdown(
|
526 |
+
"""
|
527 |
+
### Summary
|
528 |
+
"""
|
529 |
+
)
|
530 |
+
st.markdown(
|
531 |
+
"""
|
532 |
+
Flight carrier, flying time, and flying origin and destination are the most important factors that impact flight delay. On the other hand, the traveling distance does not impact the delay.
|
533 |
+
"""
|
534 |
+
)
|
535 |
+
st.markdown(
|
536 |
+
"""
|
537 |
+
#### Now, are you more aware of what flights to choose to avoid delays? \n
|
538 |
+
#### Don't worry if you are still not sure, we also have another tool for you!
|
539 |
+
"""
|
540 |
+
)
|
541 |
+
|
542 |
+
|
543 |
+
def plot_delay_over_location():
|
544 |
+
df_routes = load_route_data()
|
545 |
+
df_routes = df_routes[df_routes["ARR_DELAY"] < 400]
|
546 |
+
df_routes = df_routes.sample(5000, random_state=0)
|
547 |
+
|
548 |
+
states = alt.topo_feature(data.us_10m.url, feature="states")
|
549 |
+
|
550 |
+
background = alt.Chart(states).mark_geoshape(
|
551 |
+
fill="#ecf4f6",
|
552 |
+
stroke="white"
|
553 |
+
).properties(
|
554 |
+
width=800,
|
555 |
+
height=500
|
556 |
+
).project("albersUsa")
|
557 |
+
|
558 |
+
flights_airport = df_routes
|
559 |
+
select_city = alt.selection_single(
|
560 |
+
on="mouseover", nearest=True, fields=["ORIGIN"], empty="none"
|
561 |
+
)
|
562 |
+
|
563 |
+
connections = alt.Chart(flights_airport
|
564 |
+
).transform_filter(
|
565 |
+
(alt.datum.ORIGIN_STATE != "PR") & (alt.datum.ORIGIN_STATE != "VI") & (alt.datum.DEST_STATE != "PR") & (alt.datum.DEST_STATE != "VI")
|
566 |
+
).transform_joinaggregate(
|
567 |
+
Count="count()",
|
568 |
+
Avg_Delay='mean(ARR_DELAY)',
|
569 |
+
groupby=["ORIGIN", "DEST"]
|
570 |
+
).mark_rule(opacity=0.5).encode(
|
571 |
+
latitude="ORIGIN_LAT:Q",
|
572 |
+
longitude="ORIGIN_LONG:Q",
|
573 |
+
latitude2="DEST_LAT:Q",
|
574 |
+
longitude2="DEST_LONG:Q",
|
575 |
+
size=alt.Size("Count:Q", scale=alt.Scale(range=[0, 500]), legend=alt.Legend(title='Number of Flights')),
|
576 |
+
color=alt.Color("Avg_Delay:Q", scale=alt.Scale(scheme='lightmulti', domain=[0, 200]), legend=alt.Legend(title='Average Delay (min)'))
|
577 |
+
).transform_filter(
|
578 |
+
select_city
|
579 |
+
)
|
580 |
+
|
581 |
+
points = alt.Chart(flights_airport
|
582 |
+
).transform_filter(
|
583 |
+
(alt.datum.ORIGIN_STATE != "PR") & (alt.datum.ORIGIN_STATE != "VI") & (alt.datum.DEST_STATE != "PR") & (alt.datum.DEST_STATE != "VI")
|
584 |
+
).transform_joinaggregate(
|
585 |
+
Total_Flights="count()",
|
586 |
+
groupby=["ORIGIN"]
|
587 |
+
).mark_circle(color= '#66c2a5').encode(
|
588 |
+
latitude="ORIGIN_LAT:Q",
|
589 |
+
longitude="ORIGIN_LONG:Q",
|
590 |
+
size=alt.Size("Total_Flights:Q", scale=alt.Scale(range=[0, 1000]), legend=None),
|
591 |
+
order=alt.Order("Total_Flights:Q", sort="descending"),
|
592 |
+
tooltip=["ORIGIN:N", "Total_Flights:Q"]
|
593 |
+
).add_selection(
|
594 |
+
select_city
|
595 |
+
)
|
596 |
+
|
597 |
+
fig = (background + connections + points).configure_view(stroke=None)
|
598 |
+
fig.width = 800
|
599 |
+
fig.height = 500
|
600 |
+
|
601 |
+
return fig
|
602 |
+
|
603 |
+
|
604 |
+
|
605 |
+
def vis():
|
606 |
+
st.write("# Which factors lead to flight delay?")
|
607 |
+
vis_airline_company()
|
608 |
+
vis_flight_time()
|
609 |
+
vis_flight_distance()
|
610 |
+
vis_flight_origin()
|
611 |
+
vis_flight_destination()
|
612 |
+
vis_flight_delay_distribution_over_time()
|
613 |
+
vis_flight_delay_distribution_over_location()
|
614 |
+
|
615 |
+
vis()
|
prediction.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
saved_models/cancel_log_reg.sav
ADDED
Binary file (5.93 kB). View file
|
|
saved_models/data_transformer.sav
ADDED
Binary file (5.92 kB). View file
|
|
saved_models/delay_carrier_lin_reg.sav
ADDED
Binary file (5.6 kB). View file
|
|
saved_models/delay_late_aircraft_lin_reg.sav
ADDED
Binary file (5.6 kB). View file
|
|
saved_models/delay_lin_reg.sav
ADDED
Binary file (5.6 kB). View file
|
|
saved_models/delay_nas_lin_reg.sav
ADDED
Binary file (5.6 kB). View file
|
|
saved_models/delay_security_lin_reg.sav
ADDED
Binary file (5.6 kB). View file
|
|
saved_models/delay_weather_lin_reg.sav
ADDED
Binary file (5.6 kB). View file
|
|