Kaggle competition: Kobe Bryant Shot Selection
<SOS> metric: logloss Kaggle data source: kobe-bryant-shot-selection<save_to_csv>
import numpy as np
import pandas as pd
import xgboost as xgb
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<set_options>
data = pd.read_csv('.. /input/data.csv')
 data.set_index('shot_id', inplace=True )
%matplotlib inline 
<load_pretrained>
unknown_mask = data['shot_made_flag'].isnull()
data_cl = data.copy()
target = data_cl['shot_made_flag'].copy()
zip_file = glob.glob('.. /input/dogs-vs-cats-redux-kernels-edition/*.zip')
 print(zip_file)
 def extract_zip(file):
with ZipFile(file,'r')as zip_ref:
zip_ref.extractall('.')
 for files in zip_file:
extract_zip(files )<define_variables>
data_cl.drop('team_id', inplace=True, axis=1)
 data_cl.drop('lat', inplace=True, axis=1)
 data_cl.drop('lon', inplace=True, axis=1)
 data_cl.drop('game_id', inplace=True, axis=1)
 data_cl.drop('game_event_id', inplace=True, axis=1)
 data_cl.drop('team_name', inplace=True, axis=1)
 data_cl.drop('shot_made_flag', inplace=True, axis=1 )
def get_path(directory):
path = []
for files in os.listdir(directory):
path.append(files)
 return path
train_path = get_path('.. /working/train')
 test_path = get_path('.. /working/test' )<categorify>
data_cl['seconds_from_period_end'] = 60 * data_cl['minutes_remaining'] + data_cl['seconds_remaining']
data_cl['last_5_sec_in_period'] = data_cl['seconds_from_period_end'] < 5
data_cl['seconds_from_period_start'] = 60*(11-data_cl['minutes_remaining'])+(60-data_cl['seconds_remaining'])
 data_cl['seconds_from_game_start'] =(data_cl['period'] <= 4 ).astype(int)*(data_cl['period']-1)*12*60 +(data_cl['period'] > 4 ).astype(int)*(( data_cl['period']-4)*5*60 + 3*12*60)+ data_cl['seconds_from_period_start']
data_cl.drop('minutes_remaining', axis=1, inplace=True)
 data_cl.drop('seconds_remaining', axis=1, inplace=True )
def label_img(img):
category = img.split('.')[-3]
if category == 'cat': return [1,0]
elif category == 'dog': return [0,1]
def process_data(img_data, data_dir, isTrain=True):
data_df = []
for img in tqdm(img_data):
path = os.path.join(data_dir,img)
 if(isTrain):
label = label_img(img)
 else:
label = img.split('.')[0]
img = cv2.imread(path,cv2.IMREAD_COLOR)
 img = cv2.resize(img,(224,224))
data_df.append([np.array(img),np.array(label)])
 shuffle(data_df)
 return data_df<define_variables>
data_cl['home_play'] = data_cl['matchup'].str.contains('vs' ).astype('int')
 data_cl.drop('matchup', axis=1, inplace=True )
def get_label(directory):
label = []
for file in os.listdir(directory):
if(file.split('.')[0] == 'dog'):
label.append(str(1))
elif(file.split('.')[0] == 'cat'):
label.append(str(0))
return label
label = get_label('.. /working/train')
 df = pd.DataFrame({'filename': train_path, 'label': label})
 df.head()<split>
data_cl['game_date'] = pd.to_datetime(data_cl['game_date'])
 data_cl['game_year'] = data_cl['game_date'].dt.year
data_cl['game_month'] = data_cl['game_date'].dt.month
data_cl['dayOfWeek'] = data_cl['game_date'].dt.dayofweek
data_cl['dayOfYear'] = data_cl['game_date'].dt.dayofyear
data_cl.drop('game_date', axis=1, inplace=True )
train_df, valid_df = train_test_split(df, test_size = 0.2, stratify = df['label'], random_state = 123)
 print(train_df.shape)
 print(valid_df.shape )<define_variables>
rare_action_types = data_cl['action_type'].value_counts().sort_values().index.values[:20]
data_cl.loc[data_cl['action_type'].isin(rare_action_types), 'action_type'] = 'Other'
train_datagen = ImageDataGenerator(rotation_range = 10, zoom_range = 0.1, horizontal_flip = True, fill_mode = 'nearest',
width_shift_range = 0.1, height_shift_range = 0.1, preprocessing_function = preprocess_input)
 train_gen = train_datagen.flow_from_dataframe(train_df, directory = '.. /working/train', x_col = 'filename', y_col = 'label', target_size =(224,224),
batch_size = 64, class_mode='binary')
 valid_datagen = ImageDataGenerator(preprocessing_function = preprocess_input)
 valid_gen = valid_datagen.flow_from_dataframe(valid_df, directory = '.. /working/train', x_col = 'filename', y_col = 'label', target_size =(224,224),
batch_size = 64, class_mode='binary' )<choose_model_class>
categorial_cols = [
'action_type', 'combined_shot_type', 'period', 'season', 'shot_type',
'shot_zone_area', 'shot_zone_basic', 'shot_zone_range', 'game_year',
'game_month', 'opponent']
for cc in categorial_cols:
dummies = pd.get_dummies(data_cl[cc])
 dummies = dummies.add_prefix("{}_".format(cc))
data_cl.drop(cc, axis=1, inplace=True)
 data_cl = data_cl.join(dummies )
model = Sequential()
model.add(ResNet50(include_top = False, pooling = 'max', weights = 'imagenet'))
model.add(Dense(1, activation = 'sigmoid'))
model.layers[0].trainable = False<choose_model_class>
data_submit = data_cl[unknown_mask]
X = data_cl[~unknown_mask]
Y = target[~unknown_mask]
model.compile(optimizer = 'adam', metrics = ['accuracy'], loss = 'binary_crossentropy' )<choose_model_class>
d_train = xgb.DMatrix(X, label=Y)
 dtest = xgb.DMatrix(data_submit )
checkpointer = ModelCheckpoint(filepath = 'dogcat.weights.best.hdf5', save_best_only = True, save_weights_only = True )<train_model>
params = {}
params['objective'] = 'binary:logistic'
params['eval_metric'] = 'logloss'
params['max_depth'] = 7
params['silent'] = 1
params['colsample_bytree'] = 0.7
params['eta'] = 0.004
params['max_delta_step'] = 1
params['min_child_weight'] = 3
model.fit_generator(train_gen, epochs = 10, validation_data = valid_gen, callbacks = [checkpointer] )<load_pretrained>
model.load_weights('dogcat.weights.best.hdf5' )<create_dataframe>
clf = xgb.train(params, d_train, num_boost_round=961 )
<predict_on_test><EOS> 
preds = clf.predict(dtest)
 submission = pd.DataFrame()
submission["shot_id"] = data_submit.index
submission["shot_made_flag"]= preds
submission.to_csv("sub_xgb.csv",index=False )