Kaggle competition: Kobe Bryant Shot Selection
<SOS> metric: logloss Kaggle data source: kobe-bryant-shot-selection<categorify>
data = pd.read_csv(".. /input/data.csv")
 print(check_output(["ls", ".. /input"] ).decode("utf8"))
%%time
def fill_missing_values(df):
df.category_name.fillna(value="missing", inplace=True)
 df.brand_name.fillna(value="missing", inplace=True)
 df.item_description.fillna(value="missing", inplace=True)
 df.item_description.replace('No description yet',"missing", inplace=True)
 return df
print("Filling missing data...")
 full_df = fill_missing_values(full_df)
 print(full_df.category_name[1] )<categorify>
def test_it_GBC(data_test):
clf = GradientBoostingClassifier()
return cross_val_score(clf, data_test.drop('shot_made_flag', 1), data_test.shot_made_flag,
scoring='log_loss', cv=10 )
%%time
print("Processing categorical data...")
 le = LabelEncoder()
le.fit(full_df.category_name)
 full_df['category'] = le.transform(full_df.category_name)
 le.fit(full_df.brand_name)
 full_df.brand_name = le.transform(full_df.brand_name)
 le.fit(full_df.subcat_0)
 full_df.subcat_0 = le.transform(full_df.subcat_0)
 le.fit(full_df.subcat_1)
 full_df.subcat_1 = le.transform(full_df.subcat_1)
 le.fit(full_df.subcat_2)
 full_df.subcat_2 = le.transform(full_df.subcat_2)
 del le<feature_engineering>
def sort_encode(df, field):
ct = pd.crosstab(df.shot_made_flag, df[field] ).apply(lambda x:x/x.sum() , axis=0)
 temp = list(zip(ct.values[1, :], ct.columns))
temp.sort(reverse=True)
 new_map = {}
for index,(acc, old_number)in enumerate(temp):
new_map[old_number] = index
new_field = field + '_sort_enumerated'
df[new_field] = df[field].map(new_map)
 return new_field
<feature_engineering>
sort_encode(data, 'action_type' )
%%time
print("Transforming text data to sequences...")
 raw_text = np.hstack([full_df.item_description.str.lower() , full_df.name.str.lower() , full_df.category_name.str.lower() ])
 print(" Fitting tokenizer...")
 tok_raw = Tokenizer()
tok_raw.fit_on_texts(raw_text)
 print(" Transforming text to sequences...")
 full_df['seq_item_description'] = tok_raw.texts_to_sequences(full_df.item_description.str.lower())
 full_df['seq_name'] = tok_raw.texts_to_sequences(full_df.name.str.lower())
 del tok_raw<define_variables>
sort_encode(data, 'shot_zone_area' )
MAX_NAME_SEQ = 10
MAX_ITEM_DESC_SEQ = 75
MAX_CATEGORY_SEQ = 8
MAX_TEXT = np.max([
np.max(full_df.seq_name.max()),
np.max(full_df.seq_item_description.max()),
])+ 100
MAX_CATEGORY = np.max(full_df.category.max())+ 1
MAX_BRAND = np.max(full_df.brand_name.max())+ 1
MAX_CONDITION = np.max(full_df.item_condition_id.max())+ 1
MAX_DESC_LEN = np.max(full_df.desc_len.max())+ 1
MAX_NAME_LEN = np.max(full_df.name_len.max())+ 1
MAX_SUBCAT_0 = np.max(full_df.subcat_0.max())+ 1
MAX_SUBCAT_1 = np.max(full_df.subcat_1.max())+ 1
MAX_SUBCAT_2 = np.max(full_df.subcat_2.max())+ 1<categorify>
data['time'] = data['minutes_remaining']*60 + data['seconds_remaining']
data['time_new'] = 1000
data.loc[data.time>5, ['time_new']] = 0
data.loc[data.time<=5, ['time_new']] = 1
%%time
def get_rnn_data(dataset):
X = {
'name': pad_sequences(dataset.seq_name, maxlen=MAX_NAME_SEQ),
'item_desc': pad_sequences(dataset.seq_item_description, maxlen=MAX_ITEM_DESC_SEQ),
'brand_name': np.array(dataset.brand_name),
'category': np.array(dataset.category),
'item_condition': np.array(dataset.item_condition_id),
'num_vars': np.array(dataset[["shipping"]]),
'desc_len': np.array(dataset[["desc_len"]]),
'name_len': np.array(dataset[["name_len"]]),
'subcat_0': np.array(dataset.subcat_0),
'subcat_1': np.array(dataset.subcat_1),
'subcat_2': np.array(dataset.subcat_2),
}
return X
train = full_df[:n_trains]
dev = full_df[n_trains:n_trains+n_devs]
test = full_df[n_trains+n_devs:]
X_train = get_rnn_data(train)
 Y_train = train.target.values.reshape(-1, 1)
 X_dev = get_rnn_data(dev)
 Y_dev = dev.target.values.reshape(-1, 1)
 X_test = get_rnn_data(test )<compute_test_metric>
data['away'] = data.matchup.str.contains('@' )
def root_mean_squared_logarithmic_error(y_true, y_pred):
first_log = K.log(K.clip(y_pred, K.epsilon() , None)+ 1.)
second_log = K.log(K.clip(y_true, K.epsilon() , None)+ 1.)
return K.sqrt(K.mean(K.square(first_log - second_log), axis=-1)+0.0000001)
 def root_mean_squared_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1)+0.0000001 )<define_variables>
data_test = data.loc[~data.shot_made_flag.isnull() ,\
['action_type_sort_enumerated','shot_distance','time_new',\
'away','shot_made_flag']]
np.random.seed(123)
 def new_rnn_model(lr=0.001, decay=0.0):
name = Input(shape=[X_train["name"].shape[1]], name="name")
 item_desc = Input(shape=[X_train["item_desc"].shape[1]], name="item_desc")
 brand_name = Input(shape=[1], name="brand_name")
 item_condition = Input(shape=[1], name="item_condition")
 num_vars = Input(shape=[X_train["num_vars"].shape[1]], name="num_vars")
 desc_len = Input(shape=[1], name="desc_len")
 name_len = Input(shape=[1], name="name_len")
 subcat_0 = Input(shape=[1], name="subcat_0")
 subcat_1 = Input(shape=[1], name="subcat_1")
 subcat_2 = Input(shape=[1], name="subcat_2")
 emb_name = Embedding(MAX_TEXT, 20 )(name)
 emb_item_desc = Embedding(MAX_TEXT, 60 )(item_desc)
 emb_brand_name = Embedding(MAX_BRAND, 10 )(brand_name)
 emb_item_condition = Embedding(MAX_CONDITION, 5 )(item_condition)
 emb_desc_len = Embedding(MAX_DESC_LEN, 5 )(desc_len)
 emb_name_len = Embedding(MAX_NAME_LEN, 5 )(name_len)
 emb_subcat_0 = Embedding(MAX_SUBCAT_0, 10 )(subcat_0)
 emb_subcat_1 = Embedding(MAX_SUBCAT_1, 10 )(subcat_1)
 emb_subcat_2 = Embedding(MAX_SUBCAT_2, 10 )(subcat_2)
 rnn_layer1 = GRU(16 )(emb_item_desc)
 rnn_layer2 = GRU(8 )(emb_name)
 main_l = concatenate([
Flatten()(emb_brand_name)
 , Flatten()(emb_item_condition)
 , Flatten()(emb_desc_len)
 , Flatten()(emb_name_len)
 , Flatten()(emb_subcat_0)
 , Flatten()(emb_subcat_1)
 , Flatten()(emb_subcat_2)
 , rnn_layer1
, rnn_layer2
, num_vars
])
 main_l = Dropout(0.1 )(Dense(512,kernel_initializer='normal',activation='relu' )(main_l))
main_l = Dropout(0.1 )(Dense(256,kernel_initializer='normal',activation='relu' )(main_l))
main_l = Dropout(0.1 )(Dense(128,kernel_initializer='normal',activation='relu' )(main_l))
main_l = Dropout(0.1 )(Dense(64,kernel_initializer='normal',activation='relu' )(main_l))
output = Dense(1, activation="linear" )(main_l)
 model = Model([name, item_desc, brand_name , item_condition,
num_vars, desc_len, name_len, subcat_0, subcat_1, subcat_2], output)
 optimizer = Adam(lr=lr, decay=decay)
 model.compile(loss = 'mse', optimizer = optimizer)
 return model
model = new_rnn_model()
model.summary()
del model<train_model>
train = data.loc[~data.shot_made_flag.isnull() , ['action_type_sort_enumerated', \
'shot_distance', 'time_new', 'away', 'shot_made_flag']]
test = data.loc[data.shot_made_flag.isnull() , ['action_type_sort_enumerated', \
'shot_distance', 'time_new', 'away', 'shot_id']]
<predict_on_test><EOS> 
clf = GradientBoostingClassifier()
clf.fit(train.drop('shot_made_flag',axis=1), train.shot_made_flag)
 predictions = clf.predict_proba(test.drop('shot_id', axis=1))
submission = pd.DataFrame({'shot_id': test.shot_id, 'shot_made_flag': predictions[:, 1]})
 submission[['shot_id', 'shot_made_flag']].to_csv('submission_GBC.csv', index=False )