Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<concatenate>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
tourney_win_result['result'] = 1
tourney_lose_result['result'] = 0
train_df = pd.concat(( tourney_win_result, tourney_lose_result)).reset_index(drop=True)
 train_df<load_from_csv>
path = '.. /input/train/train/'
df = pd.read_csv('.. /input/train.csv')
 n = len(df)
 y = np.zeros(shape=(n,1),dtype=int)
 X = np.zeros(shape=(n,32,32,3))
for i,v in df.iterrows() :
y[i] = v[1]
X[i] =(mpimg.imread(f'{path}/{v[0]}')) /255
test_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/WSampleSubmissionStage1_2020.csv')
 sub = test_df.copy()<feature_engineering>
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]))<load_from_csv>
X_all = np.concatenate(( X,X_vflip,X_hflip,X_rot))
y_all = np.concatenate(( y,y,y,y))
tourney_seed = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/WDataFiles_Stage1/WNCAATourneySeeds.csv')
 if finish_data == 2014:
tourney_seed = tourney_seed[tourney_seed['Season'] > 2014]<load_from_csv>
from keras.models import Model
from keras.layers import Input, Dense, Convolution2D, Flatten, MaxPooling2D, Dropout
season_result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/WDataFiles_Stage1/WRegularSeasonCompactResults.csv')
 if finish_data == 2014:
season_result = season_result[season_result['Season'] > 2014]
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_score = season_result.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()<merge>
input_layer = Input(shape=(32,32,3))
x = Convolution2D(16, kernel_size=3,activation='relu' )(input_layer)
 x = MaxPooling2D()(x)
 x = Convolution2D(32, kernel_size=3,activation='relu' )(x)
 x = Convolution2D(64, kernel_size=3,activation='relu' )(x)
 x = MaxPooling2D()(x)
 x = Dense(256,activation='relu' )(Flatten()(x))
x = Dropout(rate=0.5 )(x)
 output = Dense(1, activation='sigmoid' )(x)
 model = Model(inputs=input_layer, outputs=output)
 model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
 model.summary()
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 )<feature_engineering>
model.fit(X_all,y_all,epochs=10 )
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<prepare_x_and_y>
tpath = '.. /input/test/test/'
submit = pd.read_csv('.. /input/sample_submission.csv')
 t_X = np.zeros(shape=(len(submit),32,32,3))
for i,v in submit.iterrows() :
t_X[i] =(mpimg.imread(f'{tpath}/{v[0]}')) /255
X = train_df.drop('result', axis=1)
 y = train_df.result<init_hyperparams>
t_y = model.predict(t_X )
params_lgb = {'num_leaves': lgb_num_leaves_max,
'min_data_in_leaf': lgb_in_leaf,
'objective': 'binary',
'max_depth': -1,
'learning_rate': lgb_lr,
"boosting_type": "gbdt",
"bagging_seed": lgb_bagging,
"metric": 'logloss',
"verbosity": -1,
'random_state': 42,
}<prepare_x_and_y>
submit['has_cactus'] = t_y
submit.head()
<init_hyperparams><EOS> 
submit.to_csv('170419_perm.csv',index=False )