Kaggle competition: Kobe Bryant Shot Selection
<SOS> metric: logloss Kaggle data source: kobe-bryant-shot-selection<concatenate>
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import xgboost as xgb
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LinearRegression
from sklearn.metrics import log_loss, accuracy_score
from sklearn.cross_validation import cross_val_score, train_test_split
train = np.hstack(( train.drop(['Name', 'Description', 'PetID', 'AdoptionSpeed'], axis=1 ).values, X))
test = np.hstack(( test.drop(['Name', 'Description', 'PetID'], axis=1 ).values, X_test))<split>
data = pd.read_csv('.. /input/data.csv' )
train_predictions = np.zeros(( train.shape[0], 1))
test_predictions = np.zeros(( test.shape[0], 1))
zero_test_predictions = np.zeros(( test.shape[0], 1))
FOLDS = 3
print("stratified k-folds")
 skf = StratifiedKFold(n_splits=FOLDS, random_state=42, shuffle=True)
 skf.get_n_splits(train, y)
 cv_scores = []
fold = 1
coefficients = np.zeros(( FOLDS, 4))
for train_idx, valid_idx in skf.split(train, y):
xtrain, xvalid = train[train_idx], train[valid_idx]
xtrain_text, xvalid_text = X[train_idx], X[valid_idx]
ytrain, yvalid = y.iloc[train_idx], y.iloc[valid_idx]
w = y.value_counts()
weights = {i : np.sum(w)/ w[i] for i in w.index}
print(weights)
 lgb_params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'learning_rate': 0.005,
'subsample':.8,
'colsample_bytree': 0.8,
'min_split_gain': 0.006,
'min_child_samples': 150,
'min_child_weight': 0.1,
'max_depth': 17,
'n_estimators': 10000,
'num_leaves': 80,
'silent': -1,
'verbose': -1,
'max_depth': 11,
'random_state': 2018
}
model = lgb.LGBMRegressor(**lgb_params)
 model.fit(
 xtrain, ytrain,
eval_set=[(xvalid, yvalid)],
eval_metric='rmse',
verbose=100,
early_stopping_rounds=100 
)
 valid_preds = model.predict(xvalid, num_iteration=model.best_iteration_)
 optR = OptimizedRounder()
optR.fit(valid_preds, yvalid.values)
 coefficients[fold-1,:] = optR.coefficients()
valid_p = optR.predict(valid_preds, coefficients[fold-1,:])
 print("Valid Counts = ", Counter(yvalid.values))
print("Predicted Counts = ", Counter(valid_p))
test_preds = model.predict(test, num_iteration=model.best_iteration_)
 scr = quadratic_weighted_kappa(yvalid.values, valid_p)
 cv_scores.append(scr)
 print("Fold = {}.QWK = {}.Coef = {}".format(fold, scr, coefficients[fold-1,:]))
print("
")
 train_predictions[valid_idx] = valid_preds.reshape(-1, 1)
 test_predictions += test_preds.reshape(-1, 1)
 fold += 1
test_predictions = test_predictions * 1./FOLDS
print("Mean Score: {}.Std Dev: {}.Mean Coeff: {}".format(np.mean(cv_scores), np.std(cv_scores), np.mean(coefficients, axis=0)) )<statistical_test>
total_actions = dict(data.action_type.value_counts())
 threshold = 100
data['type'] = data.apply(lambda row: row['action_type'] if total_actions[row['action_type']] >= threshold \
else row['combined_shot_type'], axis=1)
 data['time_remaining'] = data.apply(lambda row: row['minutes_remaining'] * 60 + row['seconds_remaining'], axis=1)
 threshold = 3
anomaly = 14
data['last_moment'] = data.apply(lambda row: row['time_remaining'] < threshold or row['time_remaining'] == anomaly, axis=1)
 data['away'] = data.matchup.str.contains('@')
 data['season'] = data.apply(lambda row: int(row['season'].split('-')[0]), axis=1)
 data['shot_distance'] = data.apply(lambda row: 28 if row['shot_distance'] > 28 else row['shot_distance'], axis=1)
 def get_acc(data, field):
ct = pd.crosstab(data.shot_made_flag, data[field] ).apply(lambda x: x / x.sum() , axis=0)
 x, y = ct.columns, ct.values[1, :]
plt.figure(figsize=(7, 5))
plt.plot(x, y)
 plt.xlabel(field)
 plt.ylabel('% shots made')
 plt.show()
def sort_encode(data, field):
ct = pd.crosstab(data.shot_made_flag, data[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'
data[new_field] = data[field].map(new_map)
 get_acc(data, new_field)
 data.drop([
'action_type',
'combined_shot_type',
'game_event_id',
'game_id',
'lat',
'lon',
'minutes_remaining',
'seconds_remaining',
'time_remaining',
'team_id',
'team_name',
'matchup',
'game_date',
'shot_type',
'playoffs',
'season',
'loc_x',
'loc_y',
], axis=1, inplace=True)
 dummies = [
'period',
'type',
'shot_zone_area',
'shot_zone_basic',
'shot_zone_range',
'opponent',
]
dummie_counter = {}
for dummy in dummies:
dummie_counter[dummy] = len(data[dummy].unique())
 data = pd.get_dummies(data, columns=dummies)
 data.head()
optR = OptimizedRounder()
train_predictions = np.array([item for sublist in train_predictions for item in sublist])
 optR.fit(train_predictions, y)
 coefficients = optR.coefficients()
print(quadratic_weighted_kappa(y, optR.predict(train_predictions, coefficients)))
 predictions = optR.predict(test_predictions, coefficients ).astype(int)
 predictions = [item for sublist in predictions for item in sublist]<load_from_csv>
for col in data.columns.tolist() :
if col not in ['shot_made_flag', 'shot_id']:
print(col + ': ' + str(len(data[col].unique())) )
sample = pd.read_csv('.. /input/test/sample_submission.csv' )<prepare_output>
train = data[~data['shot_made_flag'].isnull() ]
test = data[data['shot_made_flag'].isnull() ]
print('train size: ' + str(len(train)))
 print('test size: ' + str(len(test)))
 target = 'shot_made_flag'
features = data.columns.tolist()
features.remove(target)
 features.remove('shot_id')
 X_test = test[features]
X_train = train[features]
y_train = train[[target]]['shot_made_flag'].values
print(X_train.shape)
 print(y_train.shape )
sample.AdoptionSpeed = predictions<save_to_csv>
def modelfit(alg, dtrain, predictors, useTrainCV=True, cv_folds=5, early_stopping_rounds=50):
if useTrainCV:
xgb_param = alg.get_xgb_params()
xgtrain = xgb.DMatrix(dtrain[predictors].values, label=dtrain[target].values)
 cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params() ['n_estimators'], nfold=cv_folds,
early_stopping_rounds=early_stopping_rounds, metrics=['logloss'])
 alg.set_params(n_estimators=cvresult.shape[0])
 X_train, X_valid, y_train, y_valid = train_test_split(dtrain[predictors], dtrain[target], test_size=0.2)
 alg.fit(X_train, y_train, eval_metric='logloss')
 y_pred = alg.predict_proba(X_valid)[:,1]
result = log_loss(y_valid, y_pred)
 print(result)
 return result
<count_values><EOS> 
params = {
'base_score': 0.5,
'colsample_bylevel': 1,
'colsample_bytree': 0.8,
'learning_rate': 0.05,
'max_depth': 7,
'min_child_weight': 1,
'n_estimators': 200,
'nthread': -1,
'objective': 'binary:logistic',
'seed': 27,
'silent': True,
'subsample': 0.8
}
clf = xgb.XGBClassifier()
clf.set_params(**params)
 clf.fit(X_train, y_train)
 test.shot_made_flag = [i[1] for i in clf.predict_proba(X_test)]
test[['shot_id', 'shot_made_flag']].to_csv('sub.csv', index=False)
 predictions_train = clf.predict_proba(X_train)
 features_train = pd.DataFrame({'shot_made_flag': predictions_train[:, 1]})
 features_train[['shot_made_flag']].to_csv('features_train.csv', index=False )