<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 What questions would you have about this data?
Step1:  How would you encode categorical data such a carrier, day of week and origin airport as numerical features?
Step2:  Features
Step3:  Unique Carrier One-Hot Feature
Step4:  Day Of Week One-Hot Feature
Step5:  Time of Day Bucket Feature
<ASSISTANT_TASK:>
Python Code:
df.shape
len(set(df.Origin))
df.FlightDate.min()
df.FlightDate.max()
df.DepTime.count()
df.DepTime.dropna().describe()
needed_columns = ['Year',
 'Quarter',
 'Month',
 'DayofMonth',
 'DayOfWeek',
 'FlightDate',
 'UniqueCarrier',
 'Origin',
 'OriginCityName',
 'Dest',
 'DestCityName',
 'CRSDepTime',
 'DepTime',
 'DepDelay',
 'DepDelayMinutes',
 'DepDel15',
 'DepartureDelayGroups',
 'DepTimeBlk',
 'CRSArrTime',
 'ArrTime',
 'ArrDelay',
 'ArrDelayMinutes',
 'ArrDel15',
 'ArrTimeBlk',
 'Cancelled',
 'Diverted',
 'CRSElapsedTime',
 'ActualElapsedTime',
 'Distance',
 'DistanceGroup',
]
# Percentage of flights. 1=Monday, 2=Tuesday, etc.
df[['DayOfWeek', 'DepDel15']].groupby('DayOfWeek').mean()
df[['UniqueCarrier', 'DepDelayMinutes']].groupby('UniqueCarrier').count()
# Mean minutes of delay by carrier
df[['UniqueCarrier', 'DepDelayMinutes']].groupby('UniqueCarrier').mean()
CARRIERS = {
    'AA': 'American',
    'AS': 'Alaska',
    'B6': 'Jet Blue',
    'DL': 'Delta',
    'EV': 'Express Jet',
    'F9': 'Frontier',
    'HA': 'Hawaiian',
    'NK': 'Spirit',
    'OO': 'SkyWest',
    'UA': 'United',
    'VX': 'Virgin',
    'WN': 'Southwest'
}
df[['Origin', 'DepDel15']].groupby('Origin').mean()
Explanation: What questions would you have about this data?
End of explanation
# Percent of flights arriving within 15 minute of time by origin
mean_dep_delay15 = df[['Origin', 'DepDel15']].groupby('Origin').mean()
len(set(df.Origin))
for x in df.Origin:
    break
import numpy as np
Explanation: How would you encode categorical data such a carrier, day of week and origin airport as numerical features?
End of explanation
match = [x==y for (x,y) in zip((df.DepDelayMinutes >= 15), df.DepDel15)]
quantiles = [0] + list(np.percentile(mean_dep_delay15, [20,40,60,80])) + [1.1]
quantiles
origin_groups = []
for (low, high) in list(zip(quantiles, quantiles[1:])):
    origin_groups.append(
        set(mean_dep_delay15[(mean_dep_delay15 >= low) & (mean_dep_delay15 < high)].dropna().index)
    )
[len(x) for x in origin_groups]
for i, group in enumerate(origin_groups):
    df['OriginGroup%s' % i] = [int(o in group) for o in df.Origin]
Explanation: Features:
Origin group
UniqueCarrier one hot encoding
Day of week one hot encoding
Time of day bucket
Objective function:
DepDelay15 (Whether or not the flight will be delayed by 15 minutes or more
Origin Groups
End of explanation
unique_carriers = list(set(df.UniqueCarrier))
for carrier in unique_carriers:
    df['Carrier%s' % carrier]  = [int(x == carrier) for x in df.UniqueCarrier]
Explanation: Unique Carrier One-Hot Feature
End of explanation
days_of_week = sorted(list(set(df.DayOfWeek)))
for dow in days_of_week:
    df['DayOfWeek%s' % dow]  = [int(x == dow) for x in df.DayOfWeek]
Explanation: Day Of Week One-Hot Feature
End of explanation
clean_df = df[['DepTime', 'DepDelayMinutes']].dropna()
plt.scatter(x=clean_df.DepTime.iloc[:5000], y = clean_df.DepDelayMinutes.iloc[:5000])
thresholds = [-1, 400, 800, 1200, 1600, 2000, 2401]
buckets = []
for i, (min_time, max_time) in enumerate(list(zip(thresholds, thresholds[1:]))):
    df["DepTimeBucket%s" % i] = ((df.DepTime >= min_time) & (df.DepTime < max_time)).astype(int)
features = (['OriginGroup%s' % i for i in range(5)] +
            ['Carrier%s' % carrier for carrier in unique_carriers] +
            ['DayOfWeek%s' % dow for dow in days_of_week] + 
            ['DepTimeBucket%s' % i for i in range(6)]
           )
from sklearn.linear_model import LogisticRegression, LinearRegression
model = LogisticRegression()
clean_df = df[features + ['DepDelayMinutes']].dropna()
clean_df['Delayed'] = [int(x >= 15) for x in clean_df.DepDelayMinutes]
train_size = int(len(clean_df) * 0.7)
model.fit(clean_df[features].iloc[:train_size], clean_df.Delayed.iloc[:train_size])
predictions = model.predict(clean_df[features].iloc[train_size:])
actuals = clean_df.Delayed.iloc[train_size:]
origin_groups
from sklearn.metrics import roc_curve, auc
predict_probs = [tpl[1] for tpl in model.predict_proba(clean_df[features].iloc[train_size:])]
# Compute micro-average ROC curve and ROC area
fpr, tpr, _ = roc_curve(actuals, predict_probs)
import matplotlib.pyplot as plt
#Plot of a ROC curve for a specific class
plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange',
         lw=lw)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()
auc(fpr, tpr)
model.intercept_
coefs = dict(list(zip(features, model.coef_[0])))
coefs
Explanation: Time of Day Bucket Feature:
End of explanation
<END_TASK>