Kaggle competition: Kobe Bryant Shot Selection
<SOS> metric: logloss Kaggle data source: kobe-bryant-shot-selection<predict_on_test>
%matplotlib inline
rnn_preds = rnn_model.predict(X_test, batch_size=BATCH_SIZE, verbose=1)
 rnn_preds = np.expm1(rnn_preds )<concatenate>
print(check_output(["ls", ".. /input"] ).decode("utf8"))
df = pd.read_csv('.. /input/data.csv' )
full_df = pd.concat([train_df, dev_df, test_df] )<data_type_conversions>
def test_it(data):
clf = RandomForestClassifier(n_jobs=-1)
 return -cross_val_score(clf, data.drop('shot_made_flag', 1), data.shot_made_flag,
scoring='log_loss', cv=10 
)
 def best_r() :
score = []
ns = range(4,30,2)
 for n in ns:
x = df['loc_x'].values
y = df['loc_y'].values
r = np.sqrt(x**2 +y**2)/ n
r = r.astype(int)
 df['r'] = r
data = df[['r','shot_made_flag']]
data = data.dropna()
score.append(test_it(data ).mean())
 plt.figure(figsize=(7, 5))
plt.plot(ns, score, 'go')
 plt.xlabel('denominator')
 plt.ylabel('result')
 plt.show()
best_r()
%%time
print("Handling missing values...")
 full_df['category_name'] = full_df['category_name'].fillna('missing' ).astype(str)
 full_df['subcat_0'] = full_df['subcat_0'].astype(str)
 full_df['subcat_1'] = full_df['subcat_1'].astype(str)
 full_df['subcat_2'] = full_df['subcat_2'].astype(str)
 full_df['brand_name'] = full_df['brand_name'].fillna('missing' ).astype(str)
 full_df['shipping'] = full_df['shipping'].astype(str)
 full_df['item_condition_id'] = full_df['item_condition_id'].astype(str)
 full_df['desc_len'] = full_df['desc_len'].astype(str)
 full_df['name_len'] = full_df['name_len'].astype(str)
 full_df['item_description'] = full_df['item_description'].fillna('No description yet' ).astype(str )<feature_engineering>
data = df[['shot_distance', 'shot_made_flag']]
df['shot_distance'] = df.apply(lambda row: 28 if row['shot_distance'] > 28 else row['shot_distance'], axis=1)
 data = data.dropna()
def test_it(data):
clf = RandomForestClassifier(n_jobs=-1)
 return -cross_val_score(clf, data.drop('shot_made_flag', 1), data.shot_made_flag,
scoring='log_loss', cv=10 
)
 test_it(data ).mean()
%%time
print("Vectorizing data...")
 default_preprocessor = CountVectorizer().build_preprocessor()
def build_preprocessor(field):
field_idx = list(full_df.columns ).index(field)
 return lambda x: default_preprocessor(x[field_idx])
 vectorizer = FeatureUnion([ 
('name', CountVectorizer(
 ngram_range=(1, 2),
max_features=50000,
preprocessor=build_preprocessor('name'))), 
('subcat_0', CountVectorizer(
 token_pattern='.+',
preprocessor=build_preprocessor('subcat_0'))), 
('subcat_1', CountVectorizer(
 token_pattern='.+',
preprocessor=build_preprocessor('subcat_1'))), 
('subcat_2', CountVectorizer(
 token_pattern='.+',
preprocessor=build_preprocessor('subcat_2'))), 
('brand_name', CountVectorizer(
 token_pattern='.+',
preprocessor=build_preprocessor('brand_name'))), 
('shipping', CountVectorizer(
 token_pattern='\d+',
preprocessor=build_preprocessor('shipping'))), 
('item_condition_id', CountVectorizer(
 token_pattern='\d+',
preprocessor=build_preprocessor('item_condition_id'))), 
('desc_len', CountVectorizer(
 token_pattern='\d+',
preprocessor=build_preprocessor('desc_len'))), 
('name_len', CountVectorizer(
 token_pattern='\d+',
preprocessor=build_preprocessor('name_len'))), 
('item_description', TfidfVectorizer(
 ngram_range=(1, 3),
max_features=100000,
preprocessor=build_preprocessor('item_description'))),
])
 X = vectorizer.fit_transform(full_df.values)
 X_train = X[:n_trains]
Y_train = train_df.target.values.reshape(-1, 1)
 X_dev = X[n_trains:n_trains+n_devs]
Y_dev = dev_df.target.values.reshape(-1, 1)
 X_test = X[n_trains+n_devs:] 
<train_on_grid>
x = df['loc_x'].values
y = df['loc_y'].values
r = np.sqrt(x**2 +y**2)/ 20.0
r = r.astype(int)
 angle = 5*np.abs(np.arctan(x/y))
angle = angle.astype(int)
 df['r'] = r
df['angle'] = angle
get_acc(df, 'r' )
%%time
print("Fitting Ridge model on training examples...")
 ridge_model = Ridge(
 solver='auto', fit_intercept=True, alpha=1.0,
max_iter=100, normalize=False, tol=0.05, random_state = 1, 
)
 ridge_modelCV = RidgeCV(
 fit_intercept=True, alphas=[5.0],
normalize=False, cv = 2, scoring='neg_mean_squared_error', 
)
 ridge_model.fit(X_train, Y_train)
 ridge_modelCV.fit(X_train, Y_train )<predict_on_test>
data = df[['loc_y', 'shot_made_flag']]
data = data.dropna()
test_it(data ).mean()
Y_dev_preds_ridge = ridge_model.predict(X_dev)
 Y_dev_preds_ridge = Y_dev_preds_ridge.reshape(-1, 1)
 print("RMSL error on dev set:", rmsle(Y_dev, Y_dev_preds_ridge))<compute_train_metric>
data = df[['shot_distance', 'shot_made_flag']]
data = data.dropna()
test_it(data ).mean()
Y_dev_preds_ridgeCV = ridge_modelCV.predict(X_dev)
 Y_dev_preds_ridgeCV = Y_dev_preds_ridgeCV.reshape(-1, 1)
 print("CV RMSL error on dev set:", rmsle(Y_dev, Y_dev_preds_ridgeCV))<predict_on_test>
print(df.season.unique())
 df['season_start_year'] = df.season.str.split('-' ).str[0]
df['season_start_year'] = df['season_start_year'].astype(int)
 get_acc(df, 'season_start_year' )
%%time
ridge_preds = ridge_model.predict(X_test)
 ridge_preds = np.expm1(ridge_preds)
 ridgeCV_preds = ridge_modelCV.predict(X_test)
 ridgeCV_preds = np.expm1(ridgeCV_preds )<compute_train_metric>
action_map = {action: i for i, action in enumerate(df.action_type.unique())}
df['action_type_enumerated'] = df.action_type.map(action_map)
 get_acc(df, 'action_type_enumerated' )
%%time
def aggregate_predicts3(Y1, Y2, Y3, ratio1, ratio2):
assert Y1.shape == Y2.shape
return Y1 * ratio1 + Y2 * ratio2 + Y3 *(1.0 - ratio1-ratio2)
<compute_train_metric>
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()
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)
 get_acc(df, new_field)
 sort_encode(df, 'action_type_enumerated' )
%%time
best1 = 0
best2 = 0
lowest = 0.99
for i in range(100):
for j in range(100):
r = i*0.01
r2 = j*0.01
if r+r2 < 1.0:
Y_dev_preds = aggregate_predicts3(Y_dev_preds_rnn, Y_dev_preds_ridgeCV, Y_dev_preds_ridge, r, r2)
 fpred = rmsle(Y_dev, Y_dev_preds)
 if fpred < lowest:
best1 = r
best2 = r2
lowest = fpred
Y_dev_preds = aggregate_predicts3(Y_dev_preds_rnn, Y_dev_preds_ridgeCV, Y_dev_preds_ridge, best1, best2 )<save_to_csv>
data = df[['action_type_enumerated', 'shot_distance', 'shot_made_flag']].dropna()
x = test_it(data)
 data = df[['action_type_enumerated_sort_enumerated', 'shot_distance', 'shot_made_flag']].dropna()
y = test_it(data)
 print(x.mean() , y.mean() )
preds = aggregate_predicts3(rnn_preds, ridgeCV_preds, ridge_preds, best1, best2)
 submission = pd.DataFrame({
"test_id": test_df.test_id,
"price": preds.reshape(-1),
})
 submission.to_csv("./rnn_ridge_submission_best.csv", index=False )<define_variables>
opponent_map = {opp: i for i, opp in enumerate(df.opponent.unique())}
df['opponent_enumerated'] = df.opponent.map(opponent_map)
 sort_encode(df, 'opponent_enumerated' )
stop_real = datetime.now()
execution_time_real = stop_real-start_real
print(execution_time_real )<import_modules>
df['away'] = df.matchup.str.contains('@')
 data = df[['action_type_enumerated', 'shot_distance',
'shot_made_flag', 'away']].dropna()
test_it(data ).mean()
import numpy as np
import pandas as pd
import os
import xgboost as xgb
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import LabelEncoder
from scipy import sparse
from sklearn.decomposition import TruncatedSVD
from sklearn.model_selection import train_test_split, KFold, StratifiedKFold
import scipy as sp
from sklearn import linear_model
from functools import partial
from sklearn import metrics
from collections import Counter
import json
import lightgbm as lgb<compute_test_metric>
data = df[['action_type_enumerated', 'shot_distance',
'shot_made_flag', 'away']].dropna()
estimators, scores = list(range(1, 100, 5)) , []
for i in estimators:
clf = RandomForestClassifier(n_jobs=-1, n_estimators=i, random_state=2016)
 x = cross_val_score(clf, data.drop('shot_made_flag', 1), data.shot_made_flag,
scoring='roc_auc', cv=10)
 scores.append(x)
 x = [i for i in estimators for j in range(10)]
sns.boxplot(x, np.array(scores ).flatten() )
def confusion_matrix(rater_a, rater_b, min_rating=None, max_rating=None):

 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(rater_a + rater_b)
 if max_rating is None:
max_rating = max(rater_a + rater_b)
 num_ratings = int(max_rating - min_rating + 1)
 conf_mat = [[0 for i in range(num_ratings)]
for j in range(num_ratings)]
for a, b in zip(rater_a, rater_b):
conf_mat[a - min_rating][b - min_rating] += 1
return conf_mat
def histogram(ratings, min_rating=None, max_rating=None):

 if min_rating is None:
min_rating = min(ratings)
 if max_rating is None:
max_rating = max(ratings)
 num_ratings = int(max_rating - min_rating + 1)
 hist_ratings = [0 for x in range(num_ratings)]
for r in ratings:
hist_ratings[r - min_rating] += 1
return hist_ratings
def quadratic_weighted_kappa(y, y_pred):

 rater_a = y
rater_b = y_pred
min_rating=None
max_rating=None
rater_a = np.array(rater_a, dtype=int)
 rater_b = np.array(rater_b, dtype=int)
 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(min(rater_a), min(rater_b))
if max_rating is None:
max_rating = max(max(rater_a), max(rater_b))
conf_mat = confusion_matrix(rater_a, rater_b,
min_rating, max_rating)
 num_ratings = len(conf_mat)
 num_scored_items = float(len(rater_a))
hist_rater_a = histogram(rater_a, min_rating, max_rating)
 hist_rater_b = histogram(rater_b, min_rating, max_rating)
 numerator = 0.0
denominator = 0.0
for i in range(num_ratings):
for j in range(num_ratings):
expected_count =(hist_rater_a[i] * hist_rater_b[j]
/ num_scored_items)
 d = pow(i - j, 2.0)/ pow(num_ratings - 1, 2.0)
 numerator += d * conf_mat[i][j] / num_scored_items
denominator += d * expected_count / num_scored_items
return(1.0 - numerator / denominator )<compute_test_metric>
clf = RandomForestClassifier(n_jobs=-1, n_estimators=70, max_depth=7, random_state=2016)
 train = df.loc[~df.shot_made_flag.isnull() , ['action_type_enumerated_sort_enumerated',
'shot_distance', 'shot_made_flag', 'away']]
test = df.loc[df.shot_made_flag.isnull() , ['action_type_enumerated_sort_enumerated',
'shot_distance', 'shot_id', 'away']]
mode = test.action_type_enumerated_sort_enumerated.mode() [0]
test.action_type_enumerated_sort_enumerated.fillna(mode, inplace=True)
 clf.fit(( train.drop('shot_made_flag', 1)) [:21000], train.shot_made_flag[:21000])
 predictions_train = clf.predict_proba(train.drop('shot_made_flag', 1))
predictions_test = clf.predict_proba(test.drop('shot_id', 1))
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = quadratic_weighted_kappa(y, X_p)
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']<load_from_csv>
features_train = pd.DataFrame({'shot_made_flag': predictions_train[:, 1]})
 features_test = pd.DataFrame({'shot_id': test.shot_id,
'shot_made_flag': predictions_test[:, 1]})
 features_train[['shot_made_flag']].to_csv('features_train.csv', index=False)
 features_test[['shot_id','shot_made_flag']].to_csv('features_test.csv', index=False )