Kaggle competition: Kobe Bryant Shot Selection
<SOS> metric: logloss Kaggle data source: kobe-bryant-shot-selection<define_variables>
sns.set_style('darkgrid')
 warnings.filterwarnings('ignore')
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
df=pd.read_csv('.. /input/kobe-bryant-shot-selection/data.csv', header=0,sep=',' )
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
df.shot_made_flag.value_counts()
train_EB0_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB0_ns.npy')
 test_EB0_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB0_ns.npy' )<split>
df.shot_made_flag.isnull().sum()
X_train, X_val, y_train, y_val = train_test_split(train_EB0_ns, target, test_size=0.1, random_state=42 )<train_model>
if 'lat' in df.columns:
df.drop(labels='lat',axis=1,inplace=True)
 if 'lon' in df.columns:
df.drop(labels='lon',axis=1,inplace=True )
%%time
lr = LogisticRegression(C=0.021, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
df.team_name.value_counts()
val_preds = lr.predict_proba(X_val)[:,1]<compute_test_metric>
if 'team_name' in df.columns:
df.drop(labels='team_name', inplace=True, axis=1 )
roc_auc_score(y_val, val_preds )<compute_test_metric>
if 'shot_id' in df.columns:
df.drop(labels='shot_id', inplace=True, axis=1 )
log_loss(y_val, val_preds )<predict_on_test>
df.game_id.value_counts().head(10 )
test_preds = lr.predict_proba(test_EB0_ns)[:,1]<save_to_csv>
if 'game_id' in df.columns:
df.drop(labels='game_id', inplace=True, axis=1)
 if 'game_event_id' in df.columns:
df.drop(labels='game_event_id', inplace=True, axis=1 )
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<import_modules>
df.matchup.str.startswith('LAL' ).value_counts()
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['home_or_away']=df.matchup.apply(lambda x: 'home' if x.find('@')==-1 else 'away')
 df['home_or_away']=df['home_or_away'].astype('category' )
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
if 'matchup' in df.columns:
df.drop(labels='matchup', axis=1,inplace=True )
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['opponent']=df['opponent'].astype('category' )
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>
df.team_id.value_counts()
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
if 'team_id' in df.columns:
df.drop(labels='team_id', axis=1,inplace=True )
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
df.shot_type.value_counts()
train_EB6_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB6_ns.npy')
 test_EB6_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB6_ns.npy' )<split>
df['shot_type']=df.shot_type.astype('category' )
X_train, X_val, y_train, y_val = train_test_split(train_EB6_ns, target, test_size=0.1, random_state=42 )<train_model>
df['game_date']=pd.to_datetime(df['game_date'] )
%%time
lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
df['season']=df.season.astype('category' )
val_preds = lr.predict_proba(X_val)[:,1]<compute_test_metric>
df['weekofyear']=df.game_date.apply(lambda x:x.weekofyear)
 df['dayofweek']=df.game_date.apply(lambda x:x.dayofweek)
 df['year']=df.game_date.apply(lambda x:x.year)
 df['month']=df.game_date.apply(lambda x:x.month)
 df['weekofyear']=df['weekofyear'].astype('category')
 df['dayofweek']=df['dayofweek'].astype('category')
 df['year']=df['year'].astype('category')
 df['month']=df['month'].astype('category')
roc_auc_score(y_val, val_preds )<compute_test_metric>
if 'game_date' in df.columns:
df.drop(labels='game_date',axis=1,inplace=True)
log_loss(y_val, val_preds )<predict_on_test>
df.shot_zone_range.value_counts()
test_preds = lr.predict_proba(test_EB6_ns)[:,1]<save_to_csv>
df['shot_zone_range']=df['shot_zone_range'].astype('category' )
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<import_modules>
df.shot_zone_basic.value_counts()
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['shot_zone_basic']=df['shot_zone_basic'].astype('category')
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
df.shot_zone_area.value_counts()
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['shot_zone_area']=df['shot_zone_area'].astype('category' )
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>
df.action_type.value_counts()
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
df['action_type']=df.action_type.astype('category' )
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
df.combined_shot_type.value_counts()
train_EB7 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/train_EB7.npy')
 test_EB7 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/test_EB7.npy' )<split>
df['combined_shot_type']=df.combined_shot_type.astype('category' )
X_train, X_val, y_train, y_val = train_test_split(train_EB7, target, test_size=0.1, random_state=42 )<train_model>
df.minutes_remaining.value_counts()
%%time
lr = LogisticRegression(C=0.01, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
df['total_seconds_remaining']=df[['minutes_remaining','seconds_remaining']].apply(lambda x:x[0]*60+x[1], axis=1 ).values
bins_=[0]+list(np.linspace(6,715,71))
df['time_intervals']=pd.cut(df.total_seconds_remaining,bins=bins_,labels=list(range(1,72)) ).values
plt.figure(figsize=(16,6))
df.groupby('time_intervals')['shot_made_flag'].mean().plot(kind='line')
 plt.show()
val_preds = lr.predict_proba(X_val)[:,1]<compute_test_metric>
df['in_last_five_seconds']=[1 if val==1 else 0 for val in df.time_intervals.values]
df['in_last_five_seconds']=df['in_last_five_seconds'].astype('category')
 if 'minutes_remaining' in df.columns:
df.drop(labels='minutes_remaining',axis=1,inplace=True)
 if 'seconds_remaining' in df.columns:
df.drop(labels='seconds_remaining',axis=1,inplace=True)
 if 'time_intervals' in df.columns:
df.drop(labels='time_intervals',axis=1,inplace=True)
roc_auc_score(y_val, val_preds )<compute_test_metric>
df.period.value_counts()
log_loss(y_val, val_preds )<predict_on_test>
df.playoffs.value_counts()
test_preds = lr.predict_proba(test_EB7)[:,1]<save_to_csv>
df['period']=df['period'].astype('category')
 df['playoffs']=df['playoffs'].astype('category')
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<import_modules>
df.shot_distance.nunique()
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.shot_distance.value_counts().head(15 )
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
df['shot_made_flag']=df['shot_made_flag'].astype('category')
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_with_dummies=pd.get_dummies(df.drop(labels='shot_made_flag',axis=1),drop_first=True)
 xtrain=df_with_dummies[df.shot_made_flag.notnull() ]
ytrain=df.shot_made_flag[df.shot_made_flag.notnull() ].values
test=df_with_dummies[df.shot_made_flag.isnull() ]
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>
from sklearn.ensemble import RandomForestClassifier
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
def optimization_of_parameter_of_rf(X,y,dict_of_param,name_of_parameter, list_of_values, min_estimators, max_estimators):
list_of_parameter_dicts=[(value,{**dict_of_param,**{'n_estimators':100, 'warm_start':True, 'oob_score':True, 'n_jobs':-1,
'random_state':434,name_of_parameter:value}})for value in list_of_values]
ensemble_clfs = [("{x}={y}".format(x=name_of_parameter, y=value), RandomForestClassifier(**param_dict))
for value, param_dict in list_of_parameter_dicts]
error_rate = OrderedDict(( label, [])for label, _ in ensemble_clfs)
 for label, clf in ensemble_clfs:
for i in range(min_estimators, max_estimators + 1):
clf.set_params(n_estimators=i)
 clf.fit(X, y)
 oob_error = 1 - clf.oob_score_
error_rate[label].append(( i, oob_error))
plt.figure(figsize=(15,8))
for label, clf_err in error_rate.items() :
xs, ys = zip(*clf_err)
 plt.plot(xs, ys, label=label)
 plt.xlim(min_estimators, max_estimators)
 plt.xlabel("n_estimators")
 plt.ylabel("OOB error rate")
 plt.legend(loc="upper right")
 plt.show()
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
optimization_of_parameter_of_rf(xtrain.values, ytrain,{'min_samples_leaf':9},'max_features',[50,70,90],30,250 )
train_EB3 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/train_EB3.npy')
 test_EB3 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/test_EB3.npy' )<split>
optimization_of_parameter_of_rf(xtrain.values, ytrain,{'max_features':50},'min_samples_leaf',[5,9,11,13],30,250 )
X_train, X_val, y_train, y_val = train_test_split(train_EB3, target, test_size=0.1, random_state=42 )<train_model>
optimization_of_parameter_of_rf(xtrain.values, ytrain,{'max_features':50, 'min_samples_leaf':13},'max_depth',[15,20,25],30,250 )
%%time
lr = LogisticRegression(C=0.008, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
rfc=RandomForestClassifier(n_estimators=400,max_features=50,min_samples_leaf=13, max_depth=20)
 rfc.fit(xtrain.values, ytrain)
 preds=rfc.predict_proba(test)
<compute_test_metric><EOS> 
preds_df=pd.DataFrame({'shot_made_flag':preds[:,1]},index=df[df.shot_made_flag.isnull() ].index+1)
 preds_df.index.name='shot_id'
preds_df.to_csv('submission.csv' )