Kaggle competition: Histopathologic Cancer Detection
<SOS> metric: AUC Kaggle data source: histopathologic-cancer-detection<categorify>
from fastai.tabular import *
from fastai.callbacks import ReduceLROnPlateauCallback, EarlyStoppingCallback, SaveModelCallback
from sklearn.metrics import roc_auc_score
import gc
encoded=pd.get_dummies(all_data, columns=all_data.columns, sparse=True)
 encoded=encoded.sparse.to_coo()
encoded=encoded.tocsr()<prepare_x_and_y>
dense161 = pd.read_csv(".. /input/cancer-densenet161-v2-for-ensemble/validation_0.976066529750824.csv")
 dense161_test = pd.read_csv(".. /input/cancer-densenet161-v2-for-ensemble/submission_0.976066529750824.csv" )
Xtrain=encoded[:len(train)]
Xtest=encoded[len(train):]<choose_model_class>
dense201 = pd.read_csv(".. /input/cancer-densenet201-v2-for-ensemble/validation_0.9749373197555542.csv")
 dense201_test = pd.read_csv(".. /input/cancer-densenet201-v2-for-ensemble/submission_0.9749373197555542.csv" )
kf=StratifiedKFold(n_splits=10 )<compute_train_metric>
res50 = pd.read_csv(".. /input/cancer-resnet50-v2-for-ensemble/validation_0.9727705717086792.csv")
 res50_test = pd.read_csv(".. /input/cancer-resnet50-v2-for-ensemble/submission_0.9727705717086792.csv" )
def objective(trial):
C=trial.suggest_loguniform('C', 10e-10, 10)
 model=LogisticRegression(C=C, class_weight='balanced',max_iter=10000, solver='lbfgs', n_jobs=-1)
 score=-cross_val_score(model, Xtrain, Ytrain, cv=kf, scoring='roc_auc' ).mean()
return score
study=optuna.create_study()<save_to_csv>
model=LogisticRegression(C=0.09536298444122952, class_weight='balanced',max_iter=10000, solver='lbfgs', n_jobs=-1)
 model.fit(Xtrain, Ytrain)
 predictions=model.predict_proba(Xtest)[:,1]
submission['target']=predictions
submission.to_csv('Result.csv')
 submission.head()<set_options>
def softmax_df(df, model_name, test=False):
if test:
df[model_name+'_0'] = np.exp(df['pred_0'])
 df[model_name+'_1'] = np.exp(df['pred_1'])
 else:
df[model_name+'_0'] = np.exp(df['val_0'])
 df[model_name+'_1'] = np.exp(df['val_1'])
 df[model_name+'sum'] = df[model_name+'_0'] + df[model_name+'_1']
df[model_name+'softmax'] = df[model_name+'_1'] / df[model_name+'sum']
return df[model_name+'softmax']
warnings.filterwarnings('ignore')
 PATH = '/kaggle/input/cat-in-the-dat/'<load_from_csv>
train = pd.read_csv(f'{PATH}train.csv')
 test = pd.read_csv(f'{PATH}test.csv' )<drop_column>
train = pd.DataFrame({'dense161_0':dense161.val_0, 'dense161_1':dense161.val_1,
'dense201_0':dense201.val_0, 'dense201_1':dense201.val_1,
'res50_0':res50.val_0, 'res50_1':res50.val_1,
"y":dense161.ground_truth_label})
 test = pd.DataFrame({'dense161_0':dense161_test.pred_0, 'dense161_1':dense161_test.pred_1,
'dense201_0':dense201_test.pred_0, 'dense201_1':dense201_test.pred_1,
'res50_0':res50_test.pred_0, 'res50_1':res50_test.pred_1})
 test.y=0
%%time
target = train['target']
train_id = train['id']
test_id = test['id']
train.drop(['target', 'id', 'bin_0'], axis=1, inplace=True)
 test.drop(['id','bin_0'], axis=1, inplace=True)
 print(train.shape)
 print(test.shape )<categorify>
def roc_score(inp, target):
_, indices = inp.max(1)
 return torch.Tensor([roc_auc_score(target, indices)])[0]
%%time
traintest = pd.concat([train, test])
 dummies = pd.get_dummies(traintest, columns=traintest.columns, drop_first=True, sparse=True)
 train_ohe = dummies.iloc[:train.shape[0], :]
test_ohe = dummies.iloc[train.shape[0]:, :]
print(train_ohe.shape)
 print(test_ohe.shape )<categorify>
learn = tabular_learner(data, layers=[10, 10, 10], metrics=[accuracy, roc_score], ps=0.5, wd=1e-1, model_dir='./' ).to_fp16()
%%time
train_ohe = train_ohe.sparse.to_coo().tocsr()
test_ohe = test_ohe.sparse.to_coo().tocsr()<train_model>
ES = EarlyStoppingCallback(learn, monitor='roc_score',patience = 5)
 RLR = ReduceLROnPlateauCallback(learn, monitor='roc_score',patience = 2)
 SAVEML = SaveModelCallback(learn, every='improvement', monitor='roc_score', name='best')
 learn.fit_one_cycle(20, 1e-3, callbacks = [ES, RLR, SAVEML] )
print('
print('StratifiedKFold training...')
 folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=10)
 model = LogisticRegression(solver='lbfgs', max_iter=200, C=0.095)
 score = []
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_ohe, target, groups=target)) :
print('Fold:',fold_+1)
 tr_x, tr_y = train_ohe[trn_idx,:], target[trn_idx]
vl_x, v_y = train_ohe[val_idx,:], target[val_idx]
%time model.fit(tr_x, tr_y)
 y_pred = model.predict(vl_x)
 score.append(roc_auc_score(v_y, y_pred))
print(f'AUC score : {roc_auc_score(v_y, y_pred)}')
 print('Average AUC score', np.mean(score))
print('<train_model>
learn.load('best' )
%time model.fit(train_ohe, target )<predict_on_test>
preds, _ = learn.get_preds(DatasetType.Test)
 preds = torch.softmax(preds, dim=1)[:, 1].numpy()
pred_test = model.predict_proba(test_ohe)[:,0]<save_to_csv>
sub = pd.read_csv(".. /input/histopathologic-cancer-detection/sample_submission.csv")
 sub.head()
sub = pd.read_csv(f'{PATH}sample_submission.csv')
 sub['target'] = pred_test
sub.to_csv('log_reg_0.1.csv', index=None, header=True )<import_modules>
sub['label'] = preds
<load_from_csv><EOS> 
sub.to_csv(f'submission_{auc_val}.csv', header=True, index=False )