<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import datetime

df=pd.read_csv('FlightData.csv')   #Importing the dataset
#Watching the Dataset in DataFrame
df.head()
df.columns
df.shape
df['MONTH'].unique(),df['YEAR'].unique()
df.info()
#Finding the sum of null values in each column
df.isnull().sum()

#Dropping out the useless columns from the DataFrame

df.drop(['Unnamed: 25'],axis=1,inplace=True)
df.head()
df.shape
df.info()
#Keep the columns which are uselful for the model

df=df[['MONTH','DAY_OF_MONTH','DAY_OF_WEEK','ORIGIN','DEST','CRS_ARR_TIME','ARR_DEL15']]
df.head()



df.info()
#checking out again the null values in our new dataframe
df[df.isnull().values==True]
#Fill out the null values with 0,1 in the ARR_DEL15 column
#Note:I didn't try to remove the complete rows containing the null values as i didnt wanted to loose the data that maybe useful 

df.fillna(1,inplace=True)
#visualizing ARR_DEL15 column
fig, axs = plt.subplots(2)

df2=df[df['ARR_DEL15']==0]
Y=[len(df2[df2['MONTH']==i]) for i in df2['MONTH'].unique()]
axs[0].bar(df['MONTH'].unique(),Y)
axs[0].set(xlabel='MONTH', ylabel='No. of Flights late')


df2=df[df['ARR_DEL15']==1]
Y=[len(df2[df2['MONTH']==i]) for i in df2['MONTH'].unique()]
axs[1].bar(df['MONTH'].unique(),Y)
axs[1].set(xlabel='MONTH', ylabel='No. of Flights on time')



df.info()
df.head()
#feature scaling
df['CRS_ARR_TIME']=(df['CRS_ARR_TIME']/100).astype(int)   #Feature Scaling is required for training our model to get more accurate results
df.head()
df['ORIGIN'].unique(),df['DEST'].unique()
df=pd.get_dummies(df,columns=['ORIGIN','DEST'])
df.head()
df.shape
#bulding machine learning model
x=df.iloc[:,[0,1,2,3,5,6,7,8,9,10,11,12,13,14]].values
y=df.iloc[:,[4]].values
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)
from sklearn.ensemble import RandomForestClassifier
classifier=RandomForestClassifier(n_estimators=10,criterion='entropy',random_state=0)
classifier.fit(x_train,y_train)


x_train.shape
y_train.shape,y_test.shape
y_pred=classifier.predict(x_test)
res=classifier.score(x_test,y_test)
res    #score is not best method to check the accuracy.sometimes it is not useful
#confusion matrix
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test,y_pred)
#using ROC AUC
from sklearn.metrics import roc_auc_score
probab=classifier.predict_proba(x_test)
probab[:,1]
roc_auc_score(y_test, probab[:, 1])
#using recall 
from sklearn.metrics import recall_score
recall_score(y_test,y_pred)    # It may be low because falsse negatives maybe high in number
from sklearn.metrics import roc_curve

fpr, tpr , _ = roc_curve(y_test, probab[:, 1])
plt.plot(fpr, tpr)
plt.plot([0, 1], [0, 1], color='grey', linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
df.columns
def predict_delay(dep_date_time,origin,destination):
   from datetime import datetime
   try:
      dep_date_time_parsed = datetime.strptime(dep_date_time, '%m/%d/%Y %H:%M:%S')
   except ValueError as e:
       print('Error parsing date/time - {}'.format(e))
   
  
   date=dep_date_time_parsed.day
   month=dep_date_time_parsed.month
   day_of_week=dep_date_time_parsed.isoweekday()
   hour=dep_date_time_parsed.hour
  

   origin=origin.upper()
   destination=destination.upper()

  
   input_1=[{'MONTH':month,'DAY_OF_MONTH':
            date,'DAY_OF_WEEK':day_of_week,
           'CRS_DEP_TIME':hour,
           'ORIGIN_ATL': 1 if origin=='ATL' else 0,
           'ORIGIN_DTW': 1 if origin == 'DTW' else 0,
           'ORIGIN_JFK': 1 if origin == 'JFK' else 0,
           'ORIGIN_MSP': 1 if origin == 'MSP' else 0,
           'ORIGIN_SEA': 1 if origin == 'SEA' else 0,
           'DEST_ATL': 1 if destination == 'ATL' else 0,
           'DEST_DTW': 1 if destination == 'DTW' else 0,
           'DEST_JFK': 1 if destination == 'JFK' else 0,
           'DEST_MSP': 1 if destination == 'MSP' else 0,
           'DEST_SEA': 1 if destination == 'SEA' else 0 }]

   return classifier.predict_proba(pd.DataFrame(input_1))[0][0]   #It returns the probability of on time flight arrival

  
predict_delay('10/01/2018 21:45:00', 'JFK', 'ATL')
 from datetime import datetime
 dep_date_time='10/01/2018 21:45:00'
 dep_date_time_parsed = datetime.strptime(dep_date_time, '%m/%d/%Y %H:%M:%S')
dep_date_time_parsed.date
#visualization
x_label=['aug 1','aug 2','aug 3','aug 4']
values=(predict_delay('10/01/2018 21:45:00', 'JFK', 'ATL'),predict_delay('10/02/2018 21:45:00', 'JFK', 'ATL'),
predict_delay('10/03/2018 21:45:00', 'JFK', 'ATL'),predict_delay('10/04/2018 21:45:00', 'JFK', 'ATL'))

plt.bar(x_label,values)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup