Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<normalization>
import pandas as pd, numpy as np, os
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
def scale(data):
print("
Scaling Data:
")
 min_max_scaler = preprocessing.MinMaxScaler()
data_scale = min_max_scaler.fit_transform(data)
 data_scale=pd.DataFrame(data_scale, columns=data.columns.values, index=data.index.values)
 print(data_scale.head())
 return(data_scale)
 def cat_to_num(data):
print("
Converting Categorical Data To Numerical:
")
 obj_columns=[]
nonobj_columns=[]
for col in data.columns.values:
if data[col].dtype=='object':
obj_columns.append(col)
 else:
nonobj_columns.append(col)
 print(len(obj_columns)," Object Columns are
",obj_columns,'
')
 print(len(nonobj_columns),"Non-object columns are
",nonobj_columns)
 data_obj=data[obj_columns]
data_nonobj=data[nonobj_columns]
for col in data_obj.columns.values:
data_obj[col]=data_obj[col].astype('category' ).cat.codes
data_merge=pd.concat([data_nonobj,data_obj],axis=1)
 print("
Data after conversion:
",data_merge.head())
 return data_merge
def data_preprocess(data):
data=cat_to_num(data)
 data=scale(data)
 return(data )<prepare_x_and_y>
PATH = '.. /input/melanoma-oof-and-sub/'
FILES = os.listdir(PATH)
 OOF = np.sort([f for f in FILES if 'oof' in f])
 OOF_CSV = [pd.read_csv(PATH+k)for k in OOF]
print('We have %i oof files...'%len(OOF))
print() ; print(OOF )
X=data_preprocess(train)
 test_data=data_preprocess(test)
 y=label<concatenate>
x = np.zeros(( len(OOF_CSV[0]),len(OOF)))
 for k in range(len(OOF)) :
x[:,k] = OOF_CSV[k].pred.values
TRUE = OOF_CSV[0].target.values
temp=pd.concat([X,label],axis=1 )<set_options>
all = []
for k in range(x.shape[1]):
auc = roc_auc_score(OOF_CSV[0].target,x[:,k])
 all.append(auc)
 print('Model %i has OOF AUC = %.4f'%(k,auc))
m = [np.argmax(all)]; w = []
html=
display(HTML(html))<split>
old = np.max(all);
RES = 200;
PATIENCE = 10;
TOL = 0.0003
DUPLICATES = False
print('Ensemble AUC = %.4f by beginning with model %i'%(old,m[0]))
print()
for kk in range(len(OOF)) :
md = x[:,m[0]]
for i,k in enumerate(m[1:]):
md = w[i]*x[:,k] +(1-w[i])*md
mx = 0; mx_k = 0; mx_w = 0
print('Searching for best model to add...')
 for k in range(x.shape[1]):
print(k,', ',end='')
 if not DUPLICATES and(k in m): continue
bst_j = 0; bst = 0; ct = 0
for j in range(RES):
tmp = j/RES*x[:,k] +(1-j/RES)*md
auc = roc_auc_score(TRUE,tmp)
 if auc>bst:
bst = auc
bst_j = j/RES
else: ct += 1
if ct>PATIENCE: break
if bst>mx:
mx = bst
mx_k = k
mx_w = bst_j
inc = mx-old
if inc<=TOL:
print() ; print('No increase.Stopping.')
 break
print() ;
print('Ensemble AUC = %.4f after adding model %i with weight %.3f.Increase of %.4f'%(mx,mx_k,mx_w,inc))
print()
old = mx; m.append(mx_k); w.append(mx_w )
train, test,train_labels, test_labels = train_test_split(X, y, train_size=0.8, random_state=42 )<predict_on_test>
print('We are using models',m)
 print('with weights',w)
 print('and achieve ensemble AUC = %.4f'%old )
clf_dt = DecisionTreeClassifier(random_state=42)
 clf_dt = clf_dt.fit(train, train_labels)
 y_pred=clf_dt.predict(test)
 accuracy_score(test_labels, y_pred )<train_model>
df = OOF_CSV[0].copy()
df.pred = md
df.to_csv('ensemble_oof.csv',index=False )
clf_rf = RandomForestClassifier(
 random_state=42, 
)
 clf_rf = clf_rf.fit(train, train_labels)
 y_pred=clf_rf.predict(test)
 accuracy_score(test_labels, y_pred )<compute_train_metric>
SUB = np.sort([f for f in FILES if 'sub' in f])
 SUB_CSV = [pd.read_csv(PATH+k)for k in SUB]
print('We have %i submission files...'%len(SUB))
print() ; print(SUB )
cv_value=5
scores=cross_val_score(clf_dt, X, y, cv=cv_value)
 print("Decision Trees Accuracy: %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores=cross_val_score(clf_rf, X, y, cv=cv_value)
 print("Random Forest Accuracy: %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))<train_model>
a = np.array([ int(x.split('_')[1].split('.')[0])for x in SUB ])
 b = np.array([ int(x.split('_')[1].split('.')[0])for x in OOF ])
 if len(a)!=len(b):
print('ERROR submission files dont match oof files')
 else:
for k in range(len(a)) :
if a[k]!=b[k]: print('ERROR submission files dont match oof files' )
clf=clf_rf
print("Default Parameters of estimator are:
",clf.get_params )<define_search_space>
y = np.zeros(( len(SUB_CSV[0]),len(SUB)))
 for k in range(len(SUB)) :
y[:,k] = SUB_CSV[k].target.values
<train_model><EOS> 
df = SUB_CSV[0].copy()
df.target = md2
df.to_csv('ensemble_sub.csv',index=False )