Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<feature_engineering>
from fastai import *
from fastai.vision import *
import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import os
import numpy as np
import pandas as pd
import glob
import matplotlib.pyplot as plt
import imagehash
import psutil
from PIL import Image
from joblib import Parallel, delayed
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from IPython import display
import time
df_data['x_age_built'] = df_data['YrSold'] - df_data['YearBuilt']
df_data['x_age_remod'] = df_data['YrSold'] - df_data['YearRemodAdd']
df_data['x_age_garage'] = df_data['YrSold'] - df_data['GarageYrBlt']
df_data['x_build_remod_sum'] = df_data['YearBuilt'] + df_data['YearRemodAdd']
df_data['x_build_remod_sub'] = df_data['YearRemodAdd'] - df_data['YearBuilt']
mask_base =(df_data[target].notnull())
 x_cols = [c for c in df_data.columns if c[:2] == 'x_']
xgb = XGBRegressor()
scores = _evaluate(xgb, df_data[x_cols][mask_base], df_data[target][mask_base])
 df_evaluation.loc[len(df_evaluation)] = 'property ages', np.mean(scores), np.std(scores)
 print('property ages: mean:', np.mean(scores), ', std:', np.std(scores))<compute_train_metric>
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 )
q1 = df_data[target].quantile(0.25)
 q3 = df_data[target].quantile(0.75)
 iqr = q3 - q1
min_quartile = q1 - 1.5 * iqr
max_quartile = q3 + 1.5 * iqr
mask_iqr =(df_data[target].notnull())&(df_data[target] >= min_quartile)&(df_data[target] <= max_quartile)
 x_cols = [c for c in df_data.columns if c[:2] == 'x_']
xgb = XGBRegressor()
scores = _evaluate(xgb, df_data[x_cols][mask_iqr], df_data[target][mask_iqr])
 df_evaluation.loc[len(df_evaluation)] = 'iqr', np.mean(scores), np.std(scores)
 print('iqr: mean:', np.mean(scores), ', std:', np.std(scores))<import_modules>
bs = 32
sz=245
from sklearn.linear_model import LinearRegression<init_hyperparams>
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.argmax(y_hat,1), y, weights='quadratic'),device='cuda:0' )
param_xgb = {'n_estimators': 2820, 'max_depth': 5, 'learning_rate': 0.014605889674267689, 'min_child_weight': 2, 'gamma': 0, 'colsample_bytree': 0.15000000000000002, 'subsample': 0.71}
param_svr = {'C': 0.23086859625727427, 'epsilon': 0.006673842511350357, 'coef0': 0.8994782389230459}
param_ridge = {'alpha': 0.9999894695819446, 'tol': 0.8042543139861018}
param_lasso = {'alpha': 0.0002789651831563553}
param_lgbm = {'n_estimators': 289, 'num_leaves': 6, 'max_depth': 8, 'min_data_in_leaf': 32, 'learning_rate': 0.10777894336456172, 'min_child_weight': 0.4011559466054581}
param_rf = {'n_estimators': 285, 'max_depth': 13, 'min_samples_split': 3, 'min_samples_leaf': 3, 'max_features': 55, 'ccp_alpha': 7.135644710065878e-07}
model_xgb = XGBRegressor(**param_xgb, random_state=random_state)
 model_svr = SVR(**param_svr)
 model_ridge = Ridge(**param_ridge, random_state=random_state)
 model_lasso = Lasso(**param_lasso, random_state=random_state, tol=0.001)
 model_lgbm = LGBMRegressor(**param_lgbm, random_state=random_state)
 model_rf = RandomForestRegressor(**param_rf, random_state=random_state)
 model_stack = StackingCVRegressor(
 regressors=[model_xgb, model_svr, model_ridge, model_lasso, model_lgbm, model_rf],
meta_regressor=LinearRegression(n_jobs=-1),
cv=KFold(n_splits=4, random_state=random_state, shuffle=True),
n_jobs=-1,
random_state=random_state 
)
 mask_base =(df_data[target].notnull())
 x_cols = [c for c in df_data.columns if c[:2] == 'x_']
scores_xgb = _evaluate(model_xgb, df_data[x_cols][mask_base], df_data[target][mask_base])
 scores_svr = _evaluate(model_svr, df_data[x_cols][mask_base], df_data[target][mask_base])
 scores_ridge = _evaluate(model_ridge, df_data[x_cols][mask_base], df_data[target][mask_base])
 scores_lasso = _evaluate(model_lasso, df_data[x_cols][mask_base], df_data[target][mask_base])
 scores_lgbm = _evaluate(model_lgbm, df_data[x_cols][mask_base], df_data[target][mask_base])
 scores_rf = _evaluate(model_rf, df_data[x_cols][mask_base], df_data[target][mask_base])
 scores_stack = _evaluate(model_stack, df_data[x_cols][mask_base], df_data[target][mask_base])
 sns.set_style('whitegrid')
 fig, ax = plt.subplots(figsize=(11, 9))
scores = [scores_xgb, scores_svr, scores_ridge, scores_lasso, scores_lgbm, scores_rf, scores_stack]
models = ['xgb','svr','ridge','lasso','lgbm','rf','stack']
sns.boxplot(y=models, x=scores)
 for i in range(0, len(scores)) :
ax.text(np.max([np.max(b)for b in [a for a in scores]])* 1.02, i, str(np.mean(scores[i])) , va='center')
 plt.show()<train_model>
learn= cnn_learner(data, base_arch=models.vgg19_bn, metrics = [accuracy,quadratic_kappa] )
mask_base =(df_data[target].notnull())
 x_cols = [c for c in df_data.columns if c[:2] == 'x_']
model_xgb.fit(df_data[x_cols][mask_base].values, df_data[target][mask_base].values)
 model_svr.fit(df_data[x_cols][mask_base].values, df_data[target][mask_base].values)
 model_ridge.fit(df_data[x_cols][mask_base].values, df_data[target][mask_base].values)
 model_lasso.fit(df_data[x_cols][mask_base].values, df_data[target][mask_base].values)
 model_lgbm.fit(df_data[x_cols][mask_base].values, df_data[target][mask_base].values)
 model_rf.fit(df_data[x_cols][mask_base].values, df_data[target][mask_base].values)
 model_stack.fit(df_data[x_cols][mask_base].values, df_data[target][mask_base].values)
 gc.collect()<predict_on_test>
learn.fit_one_cycle(4, max_lr=1e-2)
 learn.recorder.plot_losses()
def _blend_predictions(X):
return(( 0.2 * model_xgb.predict(X)) + \ 
(0.1 * model_svr.predict(X)) + \ 
(0.1 * model_ridge.predict(X)) + \ 
(0.1 * model_lasso.predict(X)) + \ 
(0.1 * model_lgbm.predict(X)) + \ 
(0.1 * model_rf.predict(X)) + \ 
(0.3 * model_stack.predict(np.array(X))))<predict_on_test>
sample_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 learn.data.add_test(ImageList.from_df(sample_df,'.. /input/aptos2019-blindness-detection/',folder='test_images',suffix='.png'))
preds,y = learn.get_preds(DatasetType.Test )
<load_from_csv><EOS> 
sample_df.diagnosis = preds.argmax(1)
 sample_df.head()
sample_df.to_csv('submission.csv',index=False )