Kaggle competition: Google Cloud & NCAA® ML Competition 2020-NCAAW
train_data[categorical[0]]<categorify>
path = '.. /input/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/'
tourney_result = pd.read_csv(path + 'WDataFiles_Stage1/WNCAATourneyCompactResults.csv')
 tourney_seed = pd.read_csv(path + 'WDataFiles_Stage1/WNCAATourneySeeds.csv')
 season_result = pd.read_csv(path + 'WDataFiles_Stage1/WRegularSeasonCompactResults.csv' )
pd.get_dummies(train_data[categorical[0]] )<categorify>
test_df= pd.read_csv(path +'WSampleSubmissionStage1_2020.csv' )
for i in categorical:
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,inplace=True,axis=1)
 train_data.head() 
<feature_engineering>
tourney_result = tourney_result.drop(['DayNum', 'WScore', 'LScore', 'WLoc', 'NumOT'], axis=1)
 tourney_result
for i in train_data.drop('IsBadBuy',axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
train_data.shape 
<split>
tourney_result = pd.merge(tourney_result, tourney_seed, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Seed':'WSeed'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result = pd.merge(tourney_result, tourney_seed, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Seed':'LSeed'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42)
 print(X_train.shape,y_train.shape)
 print(X_test.shape,y_test.shape )<train_model>
def get_seed(x):
return int(x[1:3])
 tourney_result['WSeed'] = tourney_result['WSeed'].map(lambda x: get_seed(x))
tourney_result['LSeed'] = tourney_result['LSeed'].map(lambda x: get_seed(x))
tourney_result
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )<compute_test_metric>
season_win_result = season_result[['Season', 'WTeamID', 'WScore']]
season_lose_result = season_result[['Season', 'LTeamID', 'LScore']]
season_win_result.rename(columns={'WTeamID':'TeamID', 'WScore':'Score'}, inplace=True)
 season_lose_result.rename(columns={'LTeamID':'TeamID', 'LScore':'Score'}, inplace=True)
 season_result = pd.concat(( season_win_result, season_lose_result)).reset_index(drop=True)
 season_result
KNN.score(X_test,y_test )<predict_on_test>
season_score = season_result.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()
season_score
predict=KNN.predict(test_data.drop('RefId',axis=1))
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head() 
<save_to_csv>
tourney_result = pd.merge(tourney_result, season_score, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Score':'WScoreT'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result = pd.merge(tourney_result, season_score, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Score':'LScoreT'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True)
 Submission.head()
Submission.to_csv('Submission.csv')
<load_from_zip>
tourney_win_result = tourney_result.drop(['Season', 'WTeamID', 'LTeamID'], axis=1)
 tourney_win_result.rename(columns={'WSeed':'Seed1', 'LSeed':'Seed2', 'WScoreT':'ScoreT1', 'LScoreT':'ScoreT2'}, inplace=True)
 tourney_win_result
! tar xf.. /input/bird-songs-pad-and-resize-spectrogram/spectrograms_resized.tar.bz2<set_options>
tourney_lose_result = tourney_win_result.copy()
tourney_lose_result['Seed1'] = tourney_win_result['Seed2']
tourney_lose_result['Seed2'] = tourney_win_result['Seed1']
tourney_lose_result['ScoreT1'] = tourney_win_result['ScoreT2']
tourney_lose_result['ScoreT2'] = tourney_win_result['ScoreT1']
tourney_lose_result
%reload_ext autoreload
%autoreload 2
%matplotlib inline 
<define_variables>
tourney_win_result['Seed_diff'] = tourney_win_result['Seed1'] - tourney_win_result['Seed2']
tourney_win_result['ScoreT_diff'] = tourney_win_result['ScoreT1'] - tourney_win_result['ScoreT2']
tourney_lose_result['Seed_diff'] = tourney_lose_result['Seed1'] - tourney_lose_result['Seed2']
tourney_lose_result['ScoreT_diff'] = tourney_lose_result['ScoreT1'] - tourney_lose_result['ScoreT2']
data_dir = Path('.. /input')
 label_dir = data_dir/'multilabel-bird-species-classification-nips2013/nips4b_bird_challenge_train_labels/NIPS4B_BIRD_CHALLENGE_TRAIN_LABELS'
spect_dir = Path('./spectrograms_resized' )<load_from_csv>
test_df['Season'] = test_df['ID'].map(lambda x: int(x[:4]))
test_df['WTeamID'] = test_df['ID'].map(lambda x: int(x[5:9]))
test_df['LTeamID'] = test_df['ID'].map(lambda x: int(x[10:14]))
test_df
df = pd.read_csv(label_dir/'nips4b_birdchallenge_train_labels.csv', skiprows=[0, 2])
 df.tail()<data_type_conversions>
test_df = pd.merge(test_df, tourney_seed, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Seed':'Seed1'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, tourney_seed, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Seed':'Seed2'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, season_score, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Score':'ScoreT1'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, season_score, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Score':'ScoreT2'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df
df.drop(df.columns[[1, 3]],axis=1,inplace=True)
 df.rename(columns={df.columns[0]:'file', df.columns[1]:'EMPTY'}, inplace=True)
 df = df[:-1]
df.fillna(0, inplace=True)
 df = df.astype('int32', errors='ignore')
 df['file'] = df['file'].apply(lambda fn: str(Path(fn ).with_suffix('')))
 df.tail()<normalization>
test_df['Seed1'] = test_df['Seed1'].map(lambda x: get_seed(x))
test_df['Seed2'] = test_df['Seed2'].map(lambda x: get_seed(x))
test_df['Seed_diff'] = test_df['Seed1'] - test_df['Seed2']
test_df['ScoreT_diff'] = test_df['ScoreT1'] - test_df['ScoreT2']
test_df = test_df.drop(['ID', 'Pred', 'Season', 'WTeamID', 'LTeamID'], axis=1)
 test_df
tfms = get_transforms(do_flip=False, max_rotate=None, max_warp=None)
 data =(src.transform(tfms, size=128)
.databunch(num_workers=0 ).normalize(imagenet_stats))<define_variables>
tourney_win_result['result'] = 1
tourney_lose_result['result'] = 0
tourney_result = pd.concat(( tourney_win_result, tourney_lose_result)).reset_index(drop=True)
 tourney_result
data.show_batch(rows=3, figsize=(12,9), ds_type=DatasetType.Valid )<choose_model_class>
X_train = tourney_result.drop('result', axis=1)
 y_train = tourney_result.result
arch = models.resnet50
acc_02 = partial(accuracy_thresh, thresh=0.2)
 learn = cnn_learner(data, arch, metrics=acc_02, path='.' )<train_model>
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold
import lightgbm as lgb
import xgboost as xgb
from xgboost import XGBClassifier
import gc
lr = 2.29E-02
learn.fit_one_cycle(5, slice(lr))<save_model>
params_lgb = {'num_leaves': 400,
'min_child_weight': 0.034,
'feature_fraction': 0.379,
'bagging_fraction': 0.418,
'min_data_in_leaf': 106,
'objective': 'binary',
'max_depth': 50,
'learning_rate': 0.0068,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'logloss',
"verbosity": -1,
'reg_alpha': 0.3899,
'reg_lambda': 0.648,
'random_state': 47,
}
params_xgb = {'colsample_bytree': 0.8,
'learning_rate': 0.0004,
'max_depth': 31,
'subsample': 1,
'objective':'binary:logistic',
'eval_metric':'logloss',
'min_child_weight':3,
'gamma':0.25,
'n_estimators':5000
}
learn.save('stage-1-rn50', return_path=True )<train_model>
NFOLDS = 200
folds = KFold(n_splits=NFOLDS)
 columns = X_train.columns
splits = folds.split(X_train, y_train )
learn.fit_one_cycle(5, slice(3.02E-03, lr/5))<save_model>
y_preds_lgb = np.zeros(test_df.shape[0])
 y_oof_lgb = np.zeros(X_train.shape[0] )
learn.save('stage-2-rn50' )<categorify>
for fold_n,(train_index, valid_index)in enumerate(splits):
print('Fold:',fold_n+1)
 X_train1, X_valid1 = X_train[columns].iloc[train_index], X_train[columns].iloc[valid_index]
y_train1, y_valid1 = y_train.iloc[train_index], y_train.iloc[valid_index]
dtrain = lgb.Dataset(X_train1, label=y_train1)
 dvalid = lgb.Dataset(X_valid1, label=y_valid1)
 clf = lgb.train(params_lgb, dtrain, 10000, valid_sets = [dtrain, dvalid], verbose_eval=200)
 y_pred_valid = clf.predict(X_valid1)
 y_oof_lgb[valid_index] = y_pred_valid
y_preds_lgb += clf.predict(test_df)/ NFOLDS
del X_train1, X_valid1, y_train1, y_valid1
gc.collect()
data_256 =(src.transform(tfms, size=256)
.databunch(num_workers=0 ).normalize(imagenet_stats))
learn.data = data_256
learn.data.train_ds[0][0].shape<train_model>
del X_train2, X_valid2, y_train2, y_valid2
gc.collect()
lr = 5E-03
learn.fit_one_cycle(5, slice(lr))<save_model>
submission_df = pd.read_csv(path + 'WSampleSubmissionStage1_2020.csv')
 submission_df['Pred'] = y_preds_lgb
submission_df
learn.save('stage-1-256-rn50' )<train_model>
submission_df.to_csv('submission.csv', index=False )