Kaggle competition: Kobe Bryant Shot Selection
<SOS> metric: logloss Kaggle data source: kobe-bryant-shot-selection<compute_test_metric>
sns.set_style('darkgrid')
 sns.set_palette('bone')
 pd.options.display.float_format = '{:,.3f}'.format
roc_auc_score(y_val, val_preds )<compute_test_metric>
df = pd.read_csv(".. /input/data.csv")
 df.shape
log_loss(y_val, val_preds )<predict_on_test>
df.game_id.nunique() , df.game_date.nunique()
test_preds = clf.predict_proba(test_InceptionResNetV2)[:,1]<save_to_csv>
df.game_event_id.nunique()
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<import_modules>
df.action_type.value_counts() [:10]
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
from sklearn.linear_model import LogisticRegression
import os 
<load_from_csv>
_ = df[(df["minutes_remaining"] == 0)&(df["seconds_remaining"] <= 10)]
_.mean() ["shot_made_flag"], _.count() ["shot_made_flag"]
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
df["game_year"] = df["game_date"].str[0:4].astype(int)
 df["game_month"] = df["game_date"].str[5:7].astype(int)
 df['action_first_words'] = df["action_type"].str.split(' ' ).str[0]
df['action_last_words'] = df["action_type"].str.split(' ' ).str[-2]
df['season_start_year'] = df.season.str.split('-' ).str[0].astype(int)
 df["remaining"] = df["minutes_remaining"] * 60 + df["seconds_remaining"]
df["hurry_shot"] =(( df["minutes_remaining"] == 0)&(df["seconds_remaining"] < 10)).astype(int)
 df["home_game"] = df["matchup"].apply(lambda x: 1 if(x.find('@')< 0)else 0)
 df['distance_bin'] = pd.cut(df.shot_distance, bins=10, labels=range(10)).astype('int')
 df["angle"] = df.apply(lambda row: 90 if row["loc_y"]==0 else m.degrees(m.atan(row["loc_x"]/abs(row["loc_y"]))),axis=1)
 df["angle_bin"] = pd.cut(df.angle, 7, labels=range(7)).astype(int )
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224<load_pretrained>
df.drop(["team_id", "team_name", "game_date", "game_event_id", "matchup"], axis=1, inplace=True )
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )<define_variables>
nullcount = df.isnull().sum()
nullcount[nullcount > 0]
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
_ = pd.concat([df.game_id, df.period, df.shot_made_flag, df.game_id.shift(1), df.period.shift(1), df.shot_made_flag.shift(1)], axis=1)
 _.columns = ["game_id", "period", "shot_made_flag", "pre_game_id", "pre_period", "pre_shot_made_flag"]
_.dropna()
_ = _[(_["game_id"] == _["pre_game_id"])&(_["period"] == _["pre_period"])]
_.groupby(["pre_shot_made_flag"] ).mean() ["shot_made_flag"]
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
df_enc = df.copy()
train_NAS_large = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_2/train_NAS_large.npy')
 test_NAS_large = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_2/test_NAS_large.npy' )<split>
for i, t in df_enc.dtypes.iteritems() :
if t == object:
le = LabelEncoder()
le.fit(df_enc[i].astype(str))
df_enc[i] = le.transform(df_enc[i].astype(str))
X_train, X_val, y_train, y_val = train_test_split(train_NAS_large, target, test_size=0.1, random_state=42 )<train_model>
train = df[~df.shot_made_flag.isnull() ]
%%time
lr = LogisticRegression(C=0.0075, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
def shot_mean(group_col):
return train.groupby([group_col] ).mean() ["shot_made_flag"]
def sorted_shot_mean(group_col):
return train.groupby([group_col] ).mean() ["shot_made_flag"].sort_values(ascending=False )
val_preds = lr.predict_proba(X_val)[:,1]<compute_test_metric>
X_train = df_enc[~df_enc.shot_made_flag.isnull() ]
X_game_id = X_train.pop('game_id')
 Y_train = X_train['shot_made_flag']
X_train = X_train.drop(['shot_id','shot_made_flag'], axis=1)
 X_test = df_enc[df_enc.shot_made_flag.isnull() ].drop(['game_id','shot_id','shot_made_flag'], axis=1 )
roc_auc_score(y_val, val_preds )<compute_test_metric>
params={'learning_rate': 0.03,
'objective':'binary',
'metric':'binary_logloss',
'num_leaves': 31,
'verbose': 1,
'random_state':42,
'bagging_fraction': 1,
'feature_fraction': 0.8
}
folds = GroupKFold(n_splits=10)
 oof_preds = np.zeros(X_train.shape[0])
 sub_preds = np.zeros(X_test.shape[0])
 for fold_,(trn_, val_)in enumerate(folds.split(X_train, Y_train, groups=X_game_id)) :
trn_x, trn_y = X_train.iloc[trn_], Y_train.iloc[trn_]
val_x, val_y = X_train.iloc[val_], Y_train.iloc[val_]
reg = lgb.LGBMRegressor(**params, n_estimators=3000)
 reg.fit(trn_x, trn_y, eval_set=[(val_x, val_y)], early_stopping_rounds=100, verbose=500)
 oof_preds[val_] = reg.predict(val_x, num_iteration=reg.best_iteration_)
 sub_preds += reg.predict(X_test, num_iteration=reg.best_iteration_)/ folds.n_splits
pred = sub_preds
<predict_on_test><EOS> 
submission = pd.DataFrame({
"shot_id": df[df.shot_made_flag.isnull() ]["shot_id"],
"shot_made_flag": pred
})
 submission.to_csv("submission.csv", index=False )