Kaggle competition: Focus start 2020
<SOS> metric: auc Kaggle data source: focus-start-2020<define_variables>
import warnings
import numpy as np
import pandas as pd
from pathlib import Path
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from catboost import CatBoostClassifier
features = [
'prior_question_elapsed_time',
'prior_question_had_explanation',
'user_correctness',
'part',
'content_id',
'content_count',
'tags_encoded',
'tag_1',
'tag_2',
'prior_question_elapsed_time_mean',
'residual_user_mean'
]<train_on_grid>
PATH_TO_DATA = Path('.. /input/focus-start-2020' )
%%time
model = sm.OLS(train_df[target], train_df[features])
 lin_model = model.fit()
roc_auc_score(valid_df[target], lin_model.predict(valid_df[features]))<predict_on_test>
train_df = pd.read_csv(PATH_TO_DATA / 'flight_delays_train.csv' )
lin_model.predict(valid_df[features].values[:10] )<define_variables>
test_df = pd.read_csv(PATH_TO_DATA / 'flight_delays_test.csv' )
EPOCHS=5
BATCH_SIZE=4096<define_variables>
def transform(df):
df['flight'] = df['origin'] + '-->' + df['dest']
df['Month'] = df['month']
df['DayofMonth'] = df['day_of_month']
df['DayOfWeek'] = df['day_of_week']
df['DepTime'] = df['dep_time']
df['DepTime_hour'] = df['DepTime'] // 100
df['DepTime_minute'] = df['DepTime'] % 100
df['morning'] =(( df['DepTime_hour'] >= 6)&(df['DepTime_hour'] < 12))
df['day'] =(( df['DepTime_hour'] >= 12)&(df['DepTime_hour'] < 18))
df['evening'] =(( df['DepTime_hour'] >= 18)&(df['DepTime_hour'] < 24))
df['night'] =(( df['DepTime_hour'] >= 0)&(df['DepTime_hour'] < 6))
df['low_delay'] =(( df['DepTime_hour'] >= 4)&(df['DepTime_hour'] < 9))
df['other_time'] =(( df['DepTime_hour'] < 9)&(df['DepTime_hour'] >=5))
df['delay_time'] =(( df['DepTime_hour'] >= 13)&(df['DepTime_hour'] < 24)|(df['DepTime_hour'] < 5))
df['middle_time'] =(( df['DepTime_hour'] >= 9)&(df['DepTime_hour'] < 13))
df['high_delay'] =(( df['DepTime_hour'] >= 4)&(df['DepTime_hour'] < 9))
df['flight_T1'] = df['dest'] + '-->' + df['DepTime'].astype("str")
 df['flight_T2'] = df['origin'] + '-->' + df['DepTime'].astype("str")
 df['flight_T2_hour'] = df['origin'] + '-->' + df['DepTime_hour'].astype("str")
 df['flight_T2_min'] = df['origin'] + '-->' + df['DepTime_minute'].astype("str")
 df['flight_T3_hour'] = df['dest'] + '-->' + df['DepTime_hour'].astype("str")
 df['flight_T3_min'] = df['dest'] + '-->' + df['DepTime_minute'].astype("str")
 df['fl_Un'] = df['flight'] + '-->' + df['unique_carrier'].astype("str")
 df['Or_Un'] = df['origin'] + '-->' + df['unique_carrier'].astype("str")
 df['Dest_Un'] = df['dest'] + '-->' + df['unique_carrier'].astype("str")
 df['flight_M1'] = df['dest'].astype(str)+ '-->' + df['Month'].astype(str)
 df['flight_D1'] = df['dest'].astype(str)+ '-->' + df['DayOfWeek'].astype(str)
 df['flight_M2'] = df['dest'].astype(str)+ '-->' + df['DayofMonth'].astype(str)
 return df
cat_features = ['tags_encoded', 'tag_1', 'tag_2']<define_variables>
train_df = transform(train_df)
 test_df = transform(test_df )
cont_features = [x for x in features if x not in cat_features]<import_modules>
categ_feat_idx = np.where(train_df.drop('dep_delayed_15min', axis=1 ).dtypes == 'object')[0]
categ_feat_idx
fastai.__version__<data_type_conversions>
X_train = train_df.drop('dep_delayed_15min', axis=1 ).values
y_train = train_df['dep_delayed_15min']
X_test = test_df.values
train_df[target] = train_df[target].astype('float32' )<prepare_x_and_y>
X_train_part, X_valid, y_train_part, y_valid = train_test_split(X_train, y_train,
test_size=0.3,
random_state=42 )
%%time
dls = TabularDataLoaders.from_df(train_df,
procs=[Categorify, FillMissing, Normalize],
cat_names=cat_features,
cont_names=cont_features,
y_names=target, valid_idx=valid_df.index, bs=BATCH_SIZE )<compute_test_metric>
ctb = CatBoostClassifier(random_seed=42, silent=True )
def my_auc(inp, targ):
"Simple wrapper around scikit's roc_auc_score function for regression problems"
inp,targ = flatten_check(inp,targ)
 return roc_auc_score(targ.cpu().numpy() , inp.cpu().numpy() )<compute_test_metric>
%%time
ctb.fit(X_train_part, y_train_part, cat_features=categ_feat_idx )
def bce(inp,targ):
"Binary cross entropy"
inp,targ = flatten_check(inp,targ)
 loss = F.binary_cross_entropy(inp, targ)
 return loss<choose_model_class>
ctb_valid_pred = ctb.predict_proba(X_valid)[:, 1]
print(roc_auc_score(y_valid, ctb_valid_pred))
learn = tabular_learner(dls, layers=[200,100], metrics=my_auc )<find_best_params>
%%time
ctb.fit(X_train, y_train,
cat_features=categ_feat_idx);
learn.model<load_pretrained>
ctb_test_pred = ctb.predict_proba(X_test)[:, 1]
learn.model.layers.add_module('sigmoid', nn.Sigmoid())
 learn.loss_func = bce<find_best_params>
sample_sub = pd.read_csv(PATH_TO_DATA / 'sample_submission.csv' )
<train_model><EOS> 
sample_sub = pd.read_csv(PATH_TO_DATA / 'sample_submission.csv')
 sample_sub['Predicted'] = ctb_test_pred
sample_sub.to_csv('ctb_pred.csv', index=False )