<jupyter_start><jupyter_text>Goal to test binary classifier for delays, binning for target variable and give ranges for answers<jupyter_code>import pandas as pd
pd.set_option('display.max_columns', None)
import numpy as np
import random
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=1.4)
import copy
import pickle
df = pd.read_csv("data/flights_cleaned_no_outlier_iqr_with_delays.csv")
df.head()
working = [ # ran with what the test data can do
    'fl_date', # get month and bin
#     'op_unique_carrier', # most extensive name list
#     'origin', # need 'origin' to merge weather but already merged! ;)
#     'dest_airport_id', # not sure about this one
    'crs_dep_time', # bin times
#     'dep_time', # only using in TRAIN, to learn how other columns affect this
#     'crs_arr_time',
#     'arr_time', # only using in TRAIN, to learn how other columns affect this
    'weather_type', # add weight values
     #'passengers', # not sure about this one
    'arr_delay' # so we can make a target column...
]
df_ = df.filter(items = working)
df_
# Time weight: 0-500 = 1, 501-1000 = 8, 1001-1500 = 10, 1501-2000 = 8, 2001 > = 5
df_.crs_dep_time = df_.crs_dep_time // 100
crs_dep_time_remap = {
    0: 0.10,
    1: 0.10, 
    2: 0.10,
    3: 0.10, 
    4: 0.10,
    5: 0.10, 
    6: 1,
    7: 1, 
    8: 1,
    9: 1, 
    10: 1,
    11: 1, 
    12: 1,
    13: 1, 
    14: 1,
    15: 1, 
    16: 1,
    17: 1,
    18: 1,
    19: 1, 
    20: 1,
    21: 0.50, 
    22: 0.50, 
    23: 0.50
}
df_["dep_time_hour_weight"] = df_.crs_dep_time.map(crs_dep_time_remap)
df_["month"] = [ i [5:7] for i in df_.fl_date ]
df_ = df_.drop(labels="fl_date", axis=1)
df_ = df_.drop(labels="crs_dep_time", axis=1)
month_remap = { 
    '10': 0.5,
    '11': 0.75, 
    '12': 1,
    '01': 0.75
}
df_["month_weight"] = df_.month.map(month_remap)
df_ = pd.get_dummies(df_, columns=['weather_type'], drop_first=True)
df_ = df_.drop(labels="month", axis=1)
df_.fillna(0,inplace=True)
# df_['binned']=pd.cut(x=df_['arr_delay'], bins=[-45,-30,-15,0,15,30,45],labels = [0,1,2,3,4,5])
tcriteria = [df_['arr_delay'].between(-10000, 0), df_['arr_delay'].between(0, 10000)]
values = [0,30]
df_['arr_delay'] = np.select(tcriteria, values, 0)
df_
X = df_.drop('arr_delay',axis=1) # Features
y = df_.arr_delay # Target variable
# X=(X-X.min())/(X.max()-X.min())
# y.replace([np.inf, -np.inf], np.nan, inplace=True)
# y.dropna()
# X.replace([np.inf, -np.inf], np.nan, inplace=True)
# X.dropna()
y
y.head(50)
# importing train_test_split from sklearn
from sklearn.model_selection import train_test_split
# splitting the data
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
# importing module
from sklearn.linear_model import LinearRegression
# creating an object of LinearRegression class
LR = LinearRegression()
# fitting the training data
LR.fit(x_train,y_train)
y_prediction =  LR.predict(x_test)
y_prediction
# importing r2_score module
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
# predicting the accuracy score
score=r2_score(y_test,y_prediction)
print( 'r2 score is',score)
print('mean_sqrd_error is ==',mean_squared_error(y_test,y_prediction))
print('root_mean_squared error of is ==',np.sqrt(mean_squared_error(y_test,y_prediction)))<jupyter_output>r2 score is 0.006841228530647836
mean_sqrd_error is == 137.77564943879722
root_mean_squared error of is == 11.737787246274198
StarcoderdataJupyterStructuredCleanDedup