Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<concatenate>
import numpy as np
import pandas as pd
import scipy.stats
import matplotlib.pyplot as plt
from scipy.stats import spearmanr
from matplotlib.colors import LogNorm
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold
import tensorflow as tf
test_id=test['PassengerId']
df=pd.concat([train,test],axis=0)
 df.head()
print(df.info() )<data_type_conversions>
orange_black = [
'
]
plt.style.use('ggplot' )
df=df.drop(['PassengerId','Cabin','Ticket'],axis=1)
 df['Age'].fillna(df['Age'].median() ,inplace=True)
 df['Fare'].fillna(df['Fare'].median() ,inplace=True)
 df['Embarked'].fillna(df['Embarked'].mode() [0],inplace=True )<feature_engineering>
n_tta = 1500
file = 'tta-exploration-128x128-b0'
all_tta_test = pd.read_csv('.. /input/'+file+'/all_tta_test.csv')
 all_tta_test.columns.values[0] = 'image_name'
submission = pd.read_csv('.. /input/'+file+'/submission.csv')
 tta_keys = [str(i)for i in range(n_tta)]
oof = pd.read_csv('.. /input/'+file+'/oof.csv' )
df['Familysize']=df['SibSp']+df['Parch']
df['IsAlone']=1
df['IsAlone'].loc[df['Familysize']>=1]=0
df['FareBin']=pd.cut(df['Fare'],4)
 df['AgeBin']=pd.cut(df['Age'].astype(int),5)
 df['Title']=df['Name'].str.split(",",expand=True)[1].str.split('.',expand=True)[0]
min=10
title_names = df['Title'].value_counts() < min
df['Title']=df['Title'].apply(lambda x: 'Misc' if title_names[x]==True else x)
 df.head()<categorify>
def IQR(data,ax=1):
return np.subtract(*np.percentile(data, [75, 25],axis=ax))
label=LabelEncoder()
df['Sex_Code']=label.fit_transform(df['Sex'])
 df['Embarked_Code']=label.fit_transform(df['Embarked'])
 df['Title_Code']=label.fit_transform(df['Title'])
 df['FareBin_Code']=label.fit_transform(df['FareBin'])
 df['Age_Code']=label.fit_transform(df['AgeBin'])
 df.head()<categorify>
tta_arr = np.array(tta_preds)
 n_samp = tta_arr.shape[1]
average_t=[];max_t=[];std_t=[]
error_std=[]
for TTA in range(2,100):
end = int(n_tta//TTA)
 samples = tta_arr[:,:TTA*end].reshape(-1,TTA)
 all_avs = np.mean(samples,axis=1 ).reshape(len(tta_arr),end)
 if all_avs.shape[1]>10:
all_stds = np.std(samples,axis=1 ).reshape(len(tta_arr),end)
 all_e_std = np.std(all_stds,axis=1)
 error_std+=[np.mean(all_e_std/std_pred)]
if all_avs.shape[1] > 30:
std_errors = np.std(all_avs,axis=1)
 else:
std_errors = np.mean(all_stds/np.sqrt(TTA),axis=1)
 std_errors = std_errors/mn_pred
std_t+=[np.std(std_errors)]
average_t+=[np.mean(std_errors)]
max_t+=[np.max(std_errors)]
df=df.drop(['Name'],axis=1)
 df=pd.get_dummies(df)
 df.head()<prepare_x_and_y>
tta_preds = oof[tta_keys]
mns = dict(
 amean = np.mean(tta_preds,axis=1),
gmean = scipy.stats.gmean(tta_preds,axis=1),
median = np.median(tta_preds,axis=1)
)
 for mn in mns:
print('{} AUC = {}'.format(mn,roc_auc_score(oof['target'],mns[mn])) )
cX=ctrain.drop(['Survived'],axis=1)
 cy=ctrain[['Survived']]<split>
def make_sub(func,name='submission'):
submission = pd.DataFrame(dict(image_name=test_tta['0'], target=func(preds_all,axis=1)))
 submission.to_csv(name+'.csv', index=False)
 submission.head()
test_tta = pd.read_csv('.. /input/tta-exploration-128x128-b0/all_tta_test.csv')
 FOLDS=3; n_test_tta=11
preds_all = [np.mean(test_tta[[str(i)for i in range(j*n_test_tta,(j+1)*n_test_tta)]],axis=1)for j in range(FOLDS)]
preds_all = np.vstack(preds_all ).T
make_sub(np.mean,name='amean_submission')
 make_sub(scipy.stats.gmean,name='gmean_submission')
 make_sub(np.median,name='median_submission' )
cX_train, cX_test, cy_train, cy_test = train_test_split(cX,cy,stratify=cy,test_size=0.2,random_state=1 )<train_on_grid>
n_tta = 11
FOLDS=3
files = ['tta-exploration-128x128-b0','tta-exploration-128x128-b3']
all_tta_list = [pd.read_csv('.. /input/'+file+'/all_tta_test.csv')for file in files]
for df in all_tta_list:
df.columns.values[0] = 'image_name'
sub_list= [pd.read_csv('.. /input/'+file+'/submission.csv')for file in files]
tta_keys = [str(i)for i in range(n_tta)]
oof_list = [pd.read_csv('.. /input/'+file+'/oof.csv')for file in files]
lcv=LassoCV()
lcv.fit(cX_train,cy_train)
 lcv_mask=lcv.coef_!=0
print(sum(lcv_mask))<train_model>
def set_right(df,tta_keys):
df['std']=np.std(df[tta_keys],axis=1)
 return df.drop(tta_keys,axis=1)
 oof1 = [set_right(df,tta_keys ).set_index('image_name')for df in oof_list]
rfe_rf=RFE(estimator=RandomForestClassifier() ,n_features_to_select=12,verbose=1)
 rfe_rf.fit(cX_train,cy_train)
 rf_mask=rfe_rf.support_<train_model>
df_oof = pd.concat(oof1,axis=1)
 labels = df_oof['target'].iloc[:,0]
rfe_gb=RFE(estimator=GradientBoostingClassifier() ,n_features_to_select=12,verbose=1)
 rfe_gb.fit(cX_train,cy_train)
 gb_mask=rfe_gb.support_<groupby>
roc=[]
WGTS=np.linspace(0,1,11)
 for wgt in WGTS:
pred = wgt*df_oof['pred'].iloc[:,0] +(1-wgt)*df_oof['pred'].iloc[:,1]
roc+=[roc_auc_score(labels,pred)]
best_roc = max(roc)
 loc = np.where(roc==best_roc)[0][0]
best_weight = WGTS[loc]
print('The best roc = {:.6f} with weights :({},{})'.format(best_roc, best_weight,(1-best_weight)) )
votes=np.sum([lcv_mask,rf_mask,gb_mask],axis=0)
 print(votes)
 mask=votes>1<choose_model_class>
from xgboost import XGBClassifier
lr=LogisticRegression()
lr.fit(ccX_train,cy_train)
 y_pred=lr.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))<train_model>
ids = df_oof.index
fold_finder = df_oof['fold'].iloc[:,0]
def get_oof(DATA,LABELS,folds=10,PRINT=0,SEED=42):
skf = StratifiedKFold(n_splits=FOLDS,shuffle=True,random_state=SEED)
 oof_pred = []; oof_tar = []; oof_val = []; oof_names = []; oof_folds = []
for fold,(idxT,idxV)in enumerate(skf.split(DATA,LABELS)) :
X = DATA[idxT]; y = LABELS[idxT]
X_val = DATA[idxV]; y_val = LABELS[idxV]
model = XGBClassifier(objective='binary:logistic',
seed=0,
nthread=-1,
learning_rate=0.01,
n_estimators=1200,
scale_pos_weight=1,
min_child_weight=1,
max_depth=2,
subsample=0.7,
colsample_bytree=0.9,
gamma=5,
random_state=42, 
)
 model.fit(X,y)
 pred = model.predict_proba(X_val)[:,1]
oof_pred.append(pred)
 oof_tar.append(y_val)
 oof_names.append(ids[idxV])
 oof_folds.append(np.ones_like(oof_tar[-1],dtype='int8')*fold)
 auc = roc_auc_score(oof_tar[-1],oof_pred[-1])
 if PRINT:
print('
oof = np.concatenate(oof_pred); true = np.concatenate(oof_tar);
names = np.concatenate(oof_names); folds = np.concatenate(oof_folds)
 auc = roc_auc_score(true,oof)
 print('Overall OOF AUC with TTA = %.5f'%auc )
<predict_on_test><EOS> 
FOLDS = 20
print('Including confidence estimate:')
 get_oof(np.concatenate(( df_oof['pred'],df_oof['std']),axis=1),labels,folds=FOLDS)
 print('
Only predictions:')
 get_oof(np.array(df_oof['pred']),labels,folds=FOLDS )