Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<find_best_params>
%reload_ext autoreload
%autoreload 2
env = riiideducation.make_env()<feature_engineering>
SEED = 1234
SIZE = 224
PATH = ".. /input/aptos2019-blindness-detection"
def mikes(df):
user = df['user_id']
content=df['content_id']
if df['content_type_id'] != 0: content = 0
p = prob(user, content)
 df['timestamp']=p
return df['timestamp'] 
<predict_on_test>
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_everything(SEED )
iter_test = env.iter_test()
for(test_df, sample_prediction_df)in iter_test:
test_df['timestamp'] = test_df.apply(mikes,axis=1)
 test_df=test_df.rename(columns = {'timestamp':'answered_correctly'})
 env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']])
 print("done - yeah!")
<install_modules>
train_df = pd.read_csv(PATH+"/train.csv")
 sub = pd.read_csv(PATH+"/sample_submission.csv" )
!pip install.. /input/python-datatable/datatable-0.11.0-cp37-cp37m-manylinux2010_x86_64.whl<load_from_csv>
train = ImageList.from_df(train_df, path=PATH, cols='id_code', folder="train_images", suffix='.png')
 test = ImageList.from_df(sub, path=PATH, cols='id_code', folder="test_images", suffix='.png' )
train = dt.fread(".. /input/riiid-test-answer-prediction/train.csv" ).to_pandas()<load_from_csv>
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0' )
<data_type_conversions>
from sklearn.model_selection import StratifiedKFold
from tqdm import tqdm
data_types_dict = {
'row_id': 'int64',
'timestamp': 'int64',
'user_id': 'int32',
'content_id': 'int16',
'content_type_id': 'int8',
'answered_correctly': 'int8',
'prior_question_elapsed_time': 'float32',
'prior_question_had_explanation': 'boolean'
}
train = train.astype(data_types_dict )<define_variables>
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state = SEED)
 model_name = 'densenet201'
bs = 64
predictions = torch.from_numpy(np.zeros(( len(sub))))
for fold,(train_index, val_index)in tqdm(enumerate(skf.split(train_df["id_code"], train_df["diagnosis"]))):
print(fold)
 filename = model_name + "fold_" + str(fold)+".pkl"
print("Fold:", filename)
 print("TRAIN:", train_index, "VALIDATE:", val_index)
 data_fold =(ImageList.from_df(train_df,
PATH,
folder='train_images'
,cols="id_code",suffix='.png')
.split_by_idxs(train_index, val_index)
.label_from_df(cols='diagnosis', label_cls=FloatList)
.transform(get_transforms() , size=SIZE)
.databunch(bs=bs ).normalize(imagenet_stats)
)
 learn = cnn_learner(data_fold, models.densenet201, metrics=[quadratic_kappa], pretrained=True)
 learn.fit_one_cycle(5, 1e-2)
 learn.data.add_test(ImageList.from_df(sub ,PATH ,folder='test_images',suffix='.png'))
test_predsx, _ = learn.get_preds(ds_type=DatasetType.Test)
 if(fold == 0):
test_preds = test_predsx
else:
test_preds = test_predsx + test_preds
if(fold == 2):
valid_preds, valid_y = learn.get_preds(ds_type=DatasetType.Valid )
train_part_len = int(9 /10 * len(train))<feature_engineering>
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 = 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')
 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']
def convertBoolean(x):
if str(x)== "False":
return 0
elif str(x)== "True":
return 1
else:
return 0
def TOEICSection(part):
if part >= 1 and part <= 4:
return "Listening"
elif part >= 5 and part <= 7:
return "Reading"
else:
return "Missing"
def tagsCount(tags):
arr = str(tags ).split(" ")
 return len(arr )<load_from_csv>
test_preds = test_preds/5
optR = OptimizedRounder()
optR.fit(valid_preds, valid_y)
 coefficients = optR.coefficients()
valid_predictions = optR.predict(valid_preds, coefficients)[:,0].astype(int)
 test_predictions = optR.predict(test_preds, coefficients)[:,0].astype(int)
 valid_score = cohen_kappa_score(valid_y.numpy().astype(int), valid_predictions, weights="quadratic" )
questions = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/questions.csv' )<string_transform>
valid_score = cohen_kappa_score(valid_y.numpy().astype(int), valid_predictions, weights="quadratic" )
tag = questions["tags"].str.split(" ", expand = True)
 tag.columns = ['tags1','tags2','tags3','tags4','tags5','tags6']<data_type_conversions>
print("coefficients:", coefficients)
 print("validation score:", valid_score )
questions = pd.concat([questions,tag],axis=1)
 questions['tags1'] = pd.to_numeric(questions['tags1'], errors='coerce')
 questions['tags2'] = pd.to_numeric(questions['tags2'], errors='coerce')
 questions['tags3'] = pd.to_numeric(questions['tags3'], errors='coerce')
 questions['tags4'] = pd.to_numeric(questions['tags4'], errors='coerce')
 questions['tags5'] = pd.to_numeric(questions['tags5'], errors='coerce')
 questions['tags6'] = pd.to_numeric(questions['tags6'], errors='coerce' )<load_from_csv>
sub.diagnosis = test_predictions
sub.to_csv("submission.csv", index=None)
 sub.head()