Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<categorify>
warnings.filterwarnings('ignore')
 seed_val = 42
random.seed(seed_val)
 np.random.seed(seed_val )
target_col = ["Survived"]
id_dataset = ["Type"]
cat_cols = data.nunique() [data.nunique() < 12].keys().tolist()
cat_cols = [x for x in cat_cols ]
num_cols = [x for x in data.columns if x not in cat_cols + target_col + id_dataset]
bin_cols = data.nunique() [data.nunique() == 2].keys().tolist()
multi_cols = [i for i in cat_cols if i not in bin_cols]
le = LabelEncoder()
for i in bin_cols :
data[i] = le.fit_transform(data[i])
 data = pd.get_dummies(data = data,columns = multi_cols)
 std = StandardScaler()
scaled = std.fit_transform(data[num_cols])
 scaled = pd.DataFrame(scaled,columns=num_cols)
 df_data_og = data.copy()
data = data.drop(columns = num_cols,axis = 1)
 data = data.merge(scaled,left_index=True,right_index=True,how = "left")
 data = data.drop(columns = ['PassengerId'],axis = 1 )<drop_column>
black_red = [
'
]
plt.style.use('fivethirtyeight' )
train = data[data['Type'] == 1]
test = data[data['Type'] == 0]
train = train.drop(columns = ['Type'])
 test = test.drop(columns = ['Type'] )<prepare_x_and_y>
train = pd.read_csv('.. /input/melanomaextendedtabular/external_upsampled_tabular.csv')
 test = pd.read_csv('.. /input/melanomaextendedtabular/test_tabular.csv')
 sample = pd.read_csv('.. /input/melanomaextendedtabular/sample_submission.csv' )
X = train.drop('Survived', 1)
 y = train['Survived']
X_test = test
X_test = X_test.drop(columns = ['Survived'
] )<split>
train.columns = [
'img_name', 'sex', 'age', 'location', 'target','width','height'
]
test.columns = ['img_name', 'sex', 'age', 'location','width','height']
random_state = 42
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size = 0.2, random_state = random_state )<train_model>
for df in [train, test]:
df['location'].fillna('unknown', inplace=True)
 train['sex'].fillna('unknown', inplace=True)
 train['age'].fillna(-1, inplace=True )
fit_params = {"early_stopping_rounds" : 100,
"eval_metric" : 'auc',
"eval_set" : [(X_train,y_train)],
'eval_names': ['valid'],
'verbose': 0,
'categorical_feature': 'auto'}
param_test = {'learning_rate' : [0.01, 0.02, 0.03, 0.04, 0.05, 0.08, 0.1, 0.2, 0.3, 0.4],
'n_estimators' : [100, 200, 300, 400, 500, 600, 800, 1000, 1500, 2000],
'num_leaves': sp_randint(6, 50),
'min_child_samples': sp_randint(100, 500),
'min_child_weight': [1e-5, 1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4],
'subsample': sp_uniform(loc=0.2, scale=0.8),
'max_depth': [-1, 1, 2, 3, 4, 5, 6, 7],
'colsample_bytree': sp_uniform(loc=0.4, scale=0.6),
'reg_alpha': [0, 1e-1, 1, 2, 5, 7, 10, 50, 100],
'reg_lambda': [0, 1e-1, 1, 5, 10, 20, 50, 100]}
n_iter = 500
lgbm_clf = lgbm.LGBMClassifier(random_state=random_state, silent=True, metric='None', n_jobs=4)
 grid_search = RandomizedSearchCV(
 estimator=lgbm_clf, param_distributions=param_test,
n_iter=n_iter,
scoring='accuracy',
cv=5,
refit=True,
random_state=random_state,
verbose=True)
 grid_search.fit(X, y, **fit_params)
 print('Best score reached: {} with params: {} '.format(grid_search.best_score_, grid_search.best_params_))
opt_parameters = grid_search.best_params_<predict_on_test>
ids_train = train.location.values
ids_test = test.location.values
ids_train_set = set(ids_train)
 ids_test_set = set(ids_test)
 location_not_overlap = list(ids_train_set.symmetric_difference(ids_test_set))
n_overlap = len(location_not_overlap)
 if n_overlap == 0:
print(
 f'There are no different body parts occuring between train and test set...' 
)
 else:
print('There are some non-overlapping values between train and test set!
')
 print(f'Different ones are:
{pd.Series(np.setdiff1d(( train.location.value_counts().index), pd.Series(test.location.value_counts().index)))}')
%%time
lgbm_clf = lgbm.LGBMClassifier(**opt_parameters)
 lgbm_clf.fit(X_train, y_train)
 y_pred = lgbm_clf.predict(X_valid)
 y_score = lgbm_clf.predict_proba(X_valid)[:,1]
model_performance('lgbm_clf' )<predict_on_test>
train.replace(['anterior torso','lateral torso','posterior torso'], 'torso', inplace=True )
lgbm_clf = lgbm.LGBMClassifier(**opt_parameters)
 lgbm_clf.fit(X, y)
 y_pred = lgbm_clf.predict(X_test )<train_model>
train['res']= train['width'].astype(str)+'x'+train['height'].astype(str)
 test['res']= test['width'].astype(str)+'x'+test['height'].astype(str )
visualizer = DiscriminationThreshold(lgbm_clf)
 visualizer.fit(X, y)
 visualizer.poof()<save_to_csv>
train.drop(['res'], axis=1, inplace=True)
 test.drop(['res'], axis=1, inplace=True )
temp = pd.DataFrame(pd.read_csv(".. /input/test.csv")['PassengerId'])
 temp['Survived'] = y_pred
temp.to_csv(".. /working/submission.csv", index = False )<load_from_csv>
sex_dummies = pd.get_dummies(train['sex'], prefix='sex')
 train = pd.concat([train, sex_dummies], axis=1)
 sex_dummies = pd.get_dummies(test['sex'], prefix='sex')
 test = pd.concat([test, sex_dummies], axis=1)
 train.drop(['sex'], axis=1, inplace=True)
 test.drop(['sex'], axis=1, inplace=True )
training = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 training['train_test'] = 1
test['train_test'] = 0
test['Survived'] = np.NaN
all_data = pd.concat([training,test])
 %matplotlib inline
all_data.columns<define_variables>
anatom_dummies = pd.get_dummies(train['location'], prefix='anatom')
 train = pd.concat([train, anatom_dummies], axis=1)
 anatom_dummies = pd.get_dummies(test['location'], prefix='anatom')
 test = pd.concat([test, anatom_dummies], axis=1)
 train.drop('location', axis=1, inplace=True)
 test.drop(['location'], axis=1, inplace=True )
df_num = training[['Age','SibSp','Parch','Fare']]
df_cat = training[['Survived','Pclass','Sex','Ticket','Cabin','Embarked']]<create_dataframe>
for df in [train, test]:
df.drop('img_name', axis=1, inplace=True )
pd.pivot_table(training, index = 'Survived', values = ['Age','SibSp','Parch','Fare'] )<create_dataframe>
import xgboost as xgb
from sklearn.model_selection import StratifiedKFold, train_test_split, cross_val_score, cross_validate
from sklearn.metrics import roc_auc_score, roc_curve
print(pd.pivot_table(training, index = 'Survived', columns = 'Pclass', values = 'Ticket' ,aggfunc ='count'))
print()
print(pd.pivot_table(training, index = 'Survived', columns = 'Sex', values = 'Ticket' ,aggfunc ='count'))
print()
print(pd.pivot_table(training, index = 'Survived', columns = 'Embarked', values = 'Ticket' ,aggfunc ='count'))<count_values>
X = train.drop('target', axis=1)
 y = train.target
df_cat.Cabin
training['cabin_multiple'] = training.Cabin.apply(lambda x: 0 if pd.isna(x)else len(x.split(' ')))
 training['cabin_multiple'].value_counts()<create_dataframe>
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.15,
stratify=y,
random_state=42)
 cv = StratifiedKFold(5, shuffle=True, random_state=42 )
pd.pivot_table(training, index = 'Survived', columns = 'cabin_multiple', values = 'Ticket' ,aggfunc ='count' )<feature_engineering>
xg = xgb.XGBClassifier(
 n_estimators=750,
min_child_weight=0.81,
learning_rate=0.025,
max_depth=2,
subsample=0.80,
colsample_bytree=0.42,
gamma=0.10,
random_state=42,
n_jobs=-1,
)
training['cabin_adv'] = training.Cabin.apply(lambda x: str(x)[0])
<create_dataframe>
estimators = [xg]
print(training.cabin_adv.value_counts())
 pd.pivot_table(training,index='Survived',columns='cabin_adv', values = 'Name', aggfunc='count' )<feature_engineering>
def model_check(X_train, y_train, estimators, cv):
model_table = pd.DataFrame()
row_index = 0
for est in estimators:
MLA_name = est.__class__.__name__
model_table.loc[row_index, 'Model Name'] = MLA_name
cv_results = cross_validate(est,
X_train,
y_train,
cv=cv,
scoring='roc_auc',
return_train_score=True,
n_jobs=-1)
 model_table.loc[row_index,
'Train roc Mean'] = cv_results['train_score'].mean()
model_table.loc[row_index,
'Test roc Mean'] = cv_results['test_score'].mean()
model_table.loc[row_index, 'Test Std'] = cv_results['test_score'].std()
model_table.loc[row_index, 'Time'] = cv_results['fit_time'].mean()
row_index += 1
model_table.sort_values(by=['Test roc Mean'],
ascending=False,
inplace=True)
 return model_table
training['numeric_ticket'] = training.Ticket.apply(lambda x: 1 if x.isnumeric() else 0)
 training['ticket_letters'] = training.Ticket.apply(lambda x: ''.join(x.split(' ')[:-1] ).replace('.','' ).replace('/','' ).lower() if len(x.split(' ')[:-1])>0 else 0)
<count_values>
xg.fit(X_train, y_train)
 validation = xg.predict_proba(X_test)[:, 1]
roc_auc_score(y_test, validation )
training['numeric_ticket'].value_counts()<count_values>
adv_train = train.copy()
adv_train.drop('target', axis=1, inplace=True)
 adv_test = test.copy()
adv_train['dataset_label'] = 0
adv_test['dataset_label'] = 1
adv_master = pd.concat([adv_train, adv_test], axis=0)
 adv_X = adv_master.drop('dataset_label', axis=1)
 adv_y = adv_master['dataset_label']
pd.set_option("max_rows", None)
 training['ticket_letters'].value_counts() 
<create_dataframe>
adv_X_train, adv_X_test, adv_y_train, adv_y_test = train_test_split(adv_X,
adv_y,
test_size=0.4,
stratify=adv_y,
random_state=42 )
pd.pivot_table(training,index='Survived',columns='numeric_ticket', values = 'Ticket', aggfunc='count' )<feature_engineering>
xg_adv = xgb.XGBClassifier(
 random_state=42,
n_jobs=-1, 
)
 xg_adv.fit(adv_X_train, adv_y_train)
 validation = xg_adv.predict_proba(adv_X_test)[:,1]
training.Name.head(50)
 training['name_title'] = training.Name.apply(lambda x: x.split(',')[1].split('.')[0].strip())
<count_values>
adv_X.drop(['sex_unknown', 'height', 'width'], axis=1, inplace=True)
 adv_X_train, adv_X_test, adv_y_train, adv_y_test = train_test_split(adv_X,
adv_y,
test_size=0.4,
stratify=adv_y,
random_state=42)
 xg_adv.fit(adv_X_train, adv_y_train)
 validation = xg_adv.predict_proba(adv_X_test)[:,1]
training['name_title'].value_counts()<feature_engineering>
X_train.drop(['sex_unknown', 'width','height'], axis=1, inplace=True)
 test.drop(['width','height'], axis=1, inplace=True )
all_data['cabin_multiple'] = all_data.Cabin.apply(lambda x: 0 if pd.isna(x)else len(x.split(' ')))
 all_data['cabin_adv'] = all_data.Cabin.apply(lambda x: str(x)[0])
 all_data['numeric_ticket'] = all_data.Ticket.apply(lambda x: 1 if x.isnumeric() else 0)
 all_data['ticket_letters'] = all_data.Ticket.apply(lambda x: ''.join(x.split(' ')[:-1] ).replace('.','' ).replace('/','' ).lower() if len(x.split(' ')[:-1])>0 else 0)
 all_data['name_title'] = all_data.Name.apply(lambda x: x.split(',')[1].split('.')[0].strip())
 all_data.Age = all_data.Age.fillna(training.Age.median())
 all_data.Fare = all_data.Fare.fillna(training.Fare.median())
 all_data.dropna(subset=['Embarked'],inplace = True)
 all_data['norm_sibsp'] = np.log(all_data.SibSp+1)
 all_data['norm_sibsp'].hist()
all_data['norm_fare'] = np.log(all_data.Fare+1)
 all_data['norm_fare'].hist()
all_data.Pclass = all_data.Pclass.astype(str)
 all_dummies = pd.get_dummies(all_data[['Pclass','Sex','Age','SibSp','Parch','norm_fare','Embarked','cabin_adv','cabin_multiple','numeric_ticket','name_title','train_test']])
 X_train = all_dummies[all_dummies.train_test == 1].drop(['train_test'], axis =1)
 X_test = all_dummies[all_dummies.train_test == 0].drop(['train_test'], axis =1)
 y_train = all_data[all_data.train_test==1].Survived
y_train.shape<prepare_x_and_y>
xg= xgb.XGBClassifier(
 n_estimators=750,
learning_rate=0.015,
min_child_weight= 59,
max_delta_step= 6,
max_depth= 6,
subsample= 0.751,
colsample_bytree= 0.8595,
gamma= 0,
reg_lambda= 37,
random_state=42,
n_jobs=-1,
)
scale = StandardScaler()
all_dummies_scaled = all_dummies.copy()
all_dummies_scaled[['Age','SibSp','Parch','norm_fare']]= scale.fit_transform(all_dummies_scaled[['Age','SibSp','Parch','norm_fare']])
 all_dummies_scaled
X_train_scaled = all_dummies_scaled[all_dummies_scaled.train_test == 1].drop(['train_test'], axis =1)
 X_test_scaled = all_dummies_scaled[all_dummies_scaled.train_test == 0].drop(['train_test'], axis =1)
 y_train = all_data[all_data.train_test==1].Survived 
<import_modules>
xg.fit(X_train, y_train)
 predictions = xg.predict_proba(test)[:, 1]
meta_df = pd.DataFrame(columns=['image_name', 'target'])
 meta_df['image_name'] = sample['image_name']
meta_df['target'] = predictions
meta_df.to_csv('external_tabular_predicts.csv', header=True, index=False )
<compute_train_metric><EOS> 
effnet = pd.read_csv('.. /input/melanoma-effnet-ensemble/effnet_ensemble.csv')
 meta = pd.read_csv('./external_tabular_predicts.csv')
 sample['target'] =(
 effnet['target'] * 0.9 +
meta['target'] * 0.1 
)
 sample.to_csv('external_meta_ensembled.csv', header=True, index=False )