Kaggle competition: Google Cloud & NCAA® ML Competition 2018-Men's
<SOS> metric: LogLoss Kaggle data source: mens-machine-learning-competition-2018<categorify>
import numpy as np
import pandas as pd
import scipy as sp
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import log_loss
def RLenc(img, order='F', format=True):

 bytes = img.reshape(img.shape[0] * img.shape[1], order=order)
 runs = []
r = 0
pos = 1
for c in bytes:
if(c == 0):
if r != 0:
runs.append(( pos, r))
pos += r
r = 0
pos += 1
else:
r += 1
if r != 0:
runs.append(( pos, r))
pos += r
r = 0
if format:
z = ''
for rr in runs:
z += '{} {} '.format(rr[0], rr[1])
 return z[:-1]
else:
return runs<predict_on_test>
season_result = pd.read_csv('.. /input/RegularSeasonCompactResults.csv')
 tourney_result = pd.read_csv('.. /input/NCAATourneyCompactResults.csv')
 sample_submission = pd.read_csv('.. /input/SampleSubmissionStage2.csv' )
preds_test = model.predict(x_test )<compute_test_metric>
team_list = sorted(set(season_result.WTeamID)| set(season_result.LTeamID))
n_teams = len(team_list)
 team_to_int = {t: i for i, t in enumerate(team_list)}
season_result['team_a'] = season_result.WTeamID.apply(team_to_int.get)
 season_result['team_b'] = season_result.LTeamID.apply(team_to_int.get)
 season_result['log_ratio'] = np.log(season_result.WScore / season_result.LScore)
 tourney_result['team_a'] = tourney_result.WTeamID.apply(team_to_int.get)
 tourney_result['team_b'] = tourney_result.LTeamID.apply(team_to_int.get)
 sample_submission['team_a'] = sample_submission.ID.apply(lambda a: team_to_int[int(a.split('_')[1])])
 sample_submission['team_b'] = sample_submission.ID.apply(lambda a: team_to_int[int(a.split('_')[2])] )
pred_dict = {idx: RLenc(np.round(downsample(preds_test[i])> threshold_best)) for i, idx in enumerate(tqdm_notebook(test_df.index.values)) }<save_to_csv>
def encode_team(d):
n_samples = d.shape[0]
a = np.arange(n_samples)
 x = sp.sparse.lil_matrix(( n_samples, n_teams))
x[a, d.team_a] = 1
x[a, d.team_b] = -1
return x
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv('submission.csv' )<train_model>
def cv_w_c(year_list, w_list, c_list):
loss_array = np.zeros(( len(year_list), len(w_list), len(c_list)))
 for i, year in enumerate(year_list):
sr = season_result[season_result.Season == year]
x_sr = encode_team(sr)
 y_sr = np.ones(x_sr.shape[0], dtype=np.int64)
 x_train = sp.sparse.vstack([x_sr, -x_sr])
 y_train = np.concatenate([y_sr, -y_sr])
 log_ratio = np.concatenate([sr.log_ratio, sr.log_ratio])
 tr = tourney_result[tourney_result.Season == year]
x_tr = encode_team(tr)
 y_tr = np.ones(x_tr.shape[0], dtype=np.int64)
 x_test = sp.sparse.vstack([x_tr, -x_tr])
 y_test = np.concatenate([y_tr, -y_tr])
 for j, w in enumerate(w_list):
if w is None:
w_train = None
else:
w_train = w * log_ratio.min() + log_ratio
for k, c in enumerate(c_list):
cls = LogisticRegression(C=c)
 cls.fit(x_train, y_train, w_train)
 col = list(cls.classes_ ).index(1)
 y_pred = cls.predict_proba(x_test)[:, col]
loss = log_loss(y_test, y_pred)
 loss_array[i, j, k] = loss
return loss_array.mean(0 )
t_finish = time.time()
print(f"Kernel run time = {(t_finish-t_start)/3600} hours" )<set_options>
year_list = range(2010, 2018 )
plt.style.use('seaborn-white')
 sns.set_style('white')
 %matplotlib inline
t_start = time.time()<define_variables>
find_c(year_list, None, np.logspace(-3, 3, 21))
version = 5
basic_name = f'Unet_resnet_v{version}'
save_model_name = basic_name + '.model'
submission_file = basic_name + '.csv'
print(save_model_name)
 print(submission_file )<train_model>
find_c(year_list, None, np.linspace(0.5, 2, 16))
img_size_ori = 101
img_size_target = 101
def upsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_target, img_size_target), mode='constant', preserve_range=True)
 def downsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_ori, img_size_ori), mode='constant', preserve_range=True )<load_from_csv>
def find_w(year_list, w_list, c_list):
loss_array = cv_w_c(year_list, w_list, c_list)
 a = divmod(loss_array.argmin() , loss_array.shape[-1])
 print('w_list={}'.format(w_list))
print('w={} c={} loss={}'.format(w_list[a[0]], c_list[a[1]], loss_array[a]))
plt.plot(w_list, loss_array[:, a[1]])
 plt.show()
train_df = pd.read_csv(".. /input/train.csv", index_col="id", usecols=[0])
 depths_df = pd.read_csv(".. /input/depths.csv", index_col="id")
 train_df = train_df.join(depths_df)
 test_df = depths_df[~depths_df.index.isin(train_df.index)]
len(train_df )<feature_engineering>
find_w(year_list,
np.concatenate([np.zeros(1), np.logspace(-1, 1, 10)]),
np.logspace(-3, 3, 21))
train_df["images"] = [np.array(load_img(".. /input/train/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]<feature_engineering>
find_w(year_list,
np.linspace(1, 4, 7),
np.logspace(-3, 3, 21))
train_df["masks"] = [np.array(load_img(".. /input/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]<categorify>
find_c(year_list, 2.5, np.linspace(2, 8, 31))
train_df["coverage"] = train_df.masks.map(np.sum)/ pow(img_size_ori, 2)
 def cov_to_class(val):
for i in range(0, 11):
if val * 10 <= i :
return i
train_df["coverage_class"] = train_df.coverage.map(cov_to_class )<split>
def predict(w, c, filename):
year = 2018
sr = season_result[season_result.Season == year]
x_sr = encode_team(sr)
 y_sr = np.ones(x_sr.shape[0], dtype=np.int64)
 x_train = sp.sparse.vstack([x_sr, -x_sr])
 y_train = np.concatenate([y_sr, -y_sr])
 log_ratio = np.concatenate([sr.log_ratio, sr.log_ratio])
 x_test = encode_team(sample_submission)
 if w is None:
w_train = None
else:
w_train = w * log_ratio.min() + log_ratio
cls = LogisticRegression(C=c)
 cls.fit(x_train, y_train, w_train)
 col = list(cls.classes_ ).index(1)
 y_pred = cls.predict_proba(x_test)[:, col]
submission = pd.DataFrame()
submission['ID'] = sample_submission.ID
submission['Pred'] = y_pred
submission.to_csv(filename, index=False)
 return cls
ids_train, ids_valid, x_train, x_valid, y_train, y_valid, cov_train, cov_test, depth_train, depth_test = \
train_test_split(train_df.index.values,
np.array(train_df.images.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1),
np.array(train_df.masks.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1),
train_df.coverage.values,
train_df.z.values,
test_size=0.2, stratify=train_df.coverage_class, random_state=555 )<define_search_model>
lr = predict(w=None, c=1.1, filename='predict_with_logistic.csv')
 wlr = predict(w=2.5, c=3.6, filename='predict_with_weighted_logistic.csv' )