Kaggle competition: APTOS 2019 Blindness Detection
def xgb_evaluate(max_depth, gamma, colsample_bytree):
params = {'eval_metric': 'rmse',
'max_depth': int(max_depth),
'subsample': 0.8,
'eta': 0.1,
'gamma': gamma,
'colsample_bytree': colsample_bytree}
cv_result = xgb.cv(params, dtrain, num_boost_round=100, nfold=3)
 return -1.0 * cv_result['test-rmse-mean'].iloc[-1]<choose_model_class>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
xgb_bo = BayesianOptimization(xgb_evaluate, {'max_depth':(3, 7),
'gamma':(0, 1),
'colsample_bytree':(0.3, 0.9)})
 xgb_bo.maximize(init_points=3, n_iter=5, acq='ei' )<init_hyperparams>
from fastai import *
from fastai.vision import *
import pandas as pd
import matplotlib.pyplot as plt
params = xgb_bo.res['max']['max_params']
params['max_depth'] = int(params['max_depth'] )<predict_on_test>
print('Make sure cudnn is enabled:', torch.backends.cudnn.enabled )
model2 = xgb.train(params, dtrain, num_boost_round=250)
 y_pred = model2.predict(dtest)
 y_train_pred = model2.predict(dtrain)
 print(np.sqrt(mean_squared_error(y_test, y_pred)))
 print(np.sqrt(mean_squared_error(y_train, y_train_pred)) )<predict_on_test>
PATH = Path('.. /input/aptos2019-blindness-detection' )
test = pd.read_csv('.. /input/test.csv' ).set_index('key')
 test['pickup_datetime'] = test['pickup_datetime'].str.slice(0, 16)
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'], utc=True, format='%Y-%m-%d %H:%M')
 test = transform(test)
 dtest = xgb.DMatrix(test)
 y_pred_test = model2.predict(dtest )<save_to_csv>
df = pd.read_csv(PATH/'train.csv')
 df.head()
holdout = pd.DataFrame({'key': test.index, 'fare_amount': y_pred_test})
 holdout.to_csv('submission.csv', index=False )<import_modules>
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
SEED = 999
seed_everything(SEED )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<load_from_csv>
base_image_dir = os.path.join('.. ', 'input/aptos2019-blindness-detection/')
 train_dir = os.path.join(base_image_dir,'train_images/')
 df = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))
df['path'] = df['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))
 df = df.drop(columns=['id_code'])
 df = df.sample(frac=1 ).reset_index(drop=True)
 df.head(10 )
df = pd.read_csv('.. /input/train.csv',nrows=5123456, usecols=[1,2,3,4,5,6,7] )<data_type_conversions>
len_df = len(df)
 len_df
df['pickup_datetime'] = df['pickup_datetime'].str.slice(0, 16)
 df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'], utc=False, format='%Y-%m-%d %H:%M' )<feature_engineering>
im = Image.open(df['path'][1])
 width, height = im.size
print(width,height)
 im.show()
test = pd.read_csv('.. /input/test.csv' ).set_index('key')
 test['pickup_datetime'] = test['pickup_datetime'].str.slice(0, 16)
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'], utc=False, format='%Y-%m-%d %H:%M')
 test.head()<compute_test_metric>
bs = 64
sz=224
def dist(pickup_lat, pickup_long, dropoff_lat, dropoff_long):
distance = np.abs(dropoff_lat - pickup_lat)+ np.abs(dropoff_long - pickup_long)
 return distance<feature_engineering>
data.show_batch(rows=3, figsize=(7,6))
def transform(data):
data['hour'] = data['pickup_datetime'].dt.hour
data['day'] = data['pickup_datetime'].dt.day
data['year'] = data['pickup_datetime'].dt.year
nyc =(-74.0063889, 40.7141667)
 jfk =(-73.7822222222, 40.6441666667)
 ewr =(-74.175, 40.69)
 lgr =(-73.87, 40.77)
 data['distance_to_center'] = dist(nyc[1], nyc[0],
data['pickup_latitude'], data['pickup_longitude'])
 data['pickup_distance_to_jfk'] = dist(jfk[1], jfk[0],
data['pickup_latitude'], data['pickup_longitude'])
 data['dropoff_distance_to_jfk'] = dist(jfk[1], jfk[0],
data['dropoff_latitude'], data['dropoff_longitude'])
 data['pickup_distance_to_lgr'] = dist(lgr[1], lgr[0],
data['pickup_latitude'], data['pickup_longitude'])
 data['dropoff_distance_to_lgr'] = dist(lgr[1], lgr[0],
data['dropoff_latitude'], data['dropoff_longitude'])
 data['long_dist'] = data['pickup_longitude'] - data['dropoff_longitude']
data['lat_dist'] = data['pickup_latitude'] - data['dropoff_latitude']
data['dist'] = dist(data['pickup_latitude'], data['pickup_longitude'],
data['dropoff_latitude'], data['dropoff_longitude'])
 return data<categorify>
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0' )
df = transform(df)
 test = transform(test )<save_to_csv>
learn = cnn_learner(data, base_arch=models.resnet50, metrics = [quadratic_kappa] )
df.to_csv("taxiFare5M_train.csv.gz",index=False,compression="gzip")
 test.to_csv("taxiFare5M_test.csv.gz",index=False,compression="gzip" )<drop_column>
learn.lr_find()
df.drop('pickup_datetime', axis=1,inplace=True)
<import_modules>
learn.fit_one_cycle(5,max_lr = 1e-2 )
import xgboost as xgb
from bayes_opt import BayesianOptimization
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split<split>
learn.fit_one_cycle(6, max_lr=slice(1e-6,1e-3))
X_train, X_test, y_train, y_test = train_test_split(df.drop('fare_amount', axis=1),
df['fare_amount'], test_size=0.25)
 del(df)
 dtrain = xgb.DMatrix(X_train, label=y_train)
 del(X_train)
 dtest = xgb.DMatrix(X_test)
 del(X_test )<compute_train_metric>
learn.export()
learn.save('stage-2' )
def xgb_evaluate(max_depth, gamma, colsample_bytree):
params = {'eval_metric': 'rmse',
'max_depth': int(max_depth),
'subsample': 0.8,
'eta': 0.1,
'gamma': gamma,
'colsample_bytree': colsample_bytree}
cv_result = xgb.cv(params, dtrain, num_boost_round=60, nfold=2)
 return -1.0 * cv_result['test-rmse-mean'].iloc[-1]<choose_model_class>
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )
xgb_bo = BayesianOptimization(xgb_evaluate, {'max_depth':(3, 7),
'gamma':(0, 1),
'colsample_bytree':(0.3, 0.9)})
 xgb_bo.maximize(init_points=5, n_iter=6, acq='ei' )<find_best_params>
valid_preds = learn.get_preds(ds_type=DatasetType.Valid )
params = xgb_bo.res['max']['max_params']
params['max_depth'] = int(params['max_depth'])
 print(params )<predict_on_test>
import numpy as np
import pandas as pd
import os
import scipy as sp
from functools import partial
from sklearn import metrics
from collections import Counter
import json
model2 = xgb.train(params, dtrain, num_boost_round=250)
 y_pred = model2.predict(dtest)
 y_train_pred = model2.predict(dtrain)
 print(np.sqrt(mean_squared_error(y_test, y_pred)))
 print(np.sqrt(mean_squared_error(y_train, y_train_pred)) )<categorify>
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 = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 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')
 print(-loss_partial(self.coef_['x']))
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']
test = transform(test)
 test.drop('pickup_datetime', axis=1,inplace=True )<predict_on_test>
optR = OptimizedRounder()
optR.fit(valid_preds[0],valid_preds[1] )
dtest = xgb.DMatrix(test)
 y_pred_test = model2.predict(dtest )<save_to_csv>
sample_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 sample_df.head()
holdout = pd.DataFrame({'key': test.index, 'fare_amount': y_pred_test})
 holdout.to_csv('submission.csv', index=False )<load_from_csv>
learn.data.add_test(ImageList.from_df(sample_df,'.. /input/aptos2019-blindness-detection',folder='test_images',suffix='.png'))
df = pd.read_csv(".. /input/train.csv",nrows=1005000 )<load_from_csv>
preds,y = learn.TTA(ds_type=DatasetType.Test )
test = pd.read_csv(".. /input/test.csv" )<define_variables>
test_predictions = optR.predict(preds, coefficients )
testkey = test.key<drop_column>
sample_df.diagnosis = test_predictions.astype(int)
 sample_df.head()
df = df.dropna(how = 'any', axis = 'rows' )<filter>
sample_df.to_csv('submission.csv',index=False )