Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<train_on_grid>
warnings.filterwarnings('ignore')
 seed_val = 42
random.seed(seed_val)
 np.random.seed(seed_val )
param={'knn__n_neighbors':np.arange(1,20)}
steps=[('scaler',StandardScaler()),('knn',KNeighborsClassifier())]
knn_pipe=Pipeline(steps)
 grid_knn=GridSearchCV(estimator=knn_pipe,param_grid=param,cv=10,n_jobs=-1)
 grid_knn.fit(ccX_train,cy_train)
 y_pred=grid_knn.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
print(grid_knn.best_estimator_ )<train_on_grid>
black_red = [
'
]
plt.style.use('fivethirtyeight' )
param={'max_depth':np.arange(3,8),'min_samples_leaf':[0.04,0.06,0.08],'max_features':[0.2,0.4,0.6,0.8]}
dt=DecisionTreeClassifier(random_state=12)
 grid_dt=GridSearchCV(estimator=dt,param_grid=param,cv=10,n_jobs=-1)
 grid_dt.fit(ccX_train,cy_train)
 y_pred=grid_dt.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
print(grid_dt.best_params_ )<train_on_grid>
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' )
param={'n_estimators':[200],'max_depth':np.arange(3,6),'min_samples_leaf':[0.04,0.06,0.08],'max_features':[0.2,0.4,0.6,0.8]}
rf=RandomForestClassifier(random_state=12)
 grid_rf=GridSearchCV(estimator=rf,param_grid=param,cv=10,n_jobs=-1)
 grid_rf.fit(ccX_train,cy_train)
 y_pred=grid_rf.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
print(grid_rf.best_params_ )<train_model>
train.columns = [
'img_name', 'sex', 'age', 'location', 'target','width','height'
]
test.columns = ['img_name', 'sex', 'age', 'location','width','height']
xg_cl=xgb.XGBClassifier(objective='binary:logistic',n_estimators=4,seed=123)
 xg_cl.fit(ccX_train,cy_train)
 y_pred=xg_cl.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))<train_on_grid>
for df in [train, test]:
df['location'].fillna('unknown', inplace=True)
 train['sex'].fillna('unknown', inplace=True)
 train['age'].fillna(-1, inplace=True )
xg=xgb.XGBClassifier(objective='reg:logistic',seed=123)
 params={'n_estimators':[100,200],'max_depth':np.arange(2,6),'alpha':[0.01,0.1,1,10]}
grid_xg=GridSearchCV(estimator=xg,param_grid=params,cv=10,n_jobs=-1)
 grid_xg.fit(ccX_train,cy_train)
 y_pred=grid_xg.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))
print(grid_xg.best_params_ )<train_model>
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)))}')
dt=DecisionTreeClassifier(max_depth=1,random_state=1)
 ada=AdaBoostClassifier(base_estimator=dt,n_estimators=300,learning_rate=0.05)
 ada.fit(ccX_train,cy_train)
 y_pred=ada.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))<train_model>
train.replace(['anterior torso','lateral torso','posterior torso'], 'torso', inplace=True )
grad=GradientBoostingClassifier(n_estimators=500,learning_rate=0.01)
 grad.fit(ccX_train,cy_train)
 y_pred=grad.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))<compute_train_metric>
train['res']= train['width'].astype(str)+'x'+train['height'].astype(str)
 test['res']= test['width'].astype(str)+'x'+test['height'].astype(str )
svc=SVC(C=100,random_state=12)
 svc.fit(ccX_train,cy_train)
 y_pred=svc.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))<choose_model_class>
train.drop(['res'], axis=1, inplace=True)
 test.drop(['res'], axis=1, inplace=True )
lr=LogisticRegression(random_state=12)
 knn=KNeighborsClassifier()
dt=DecisionTreeClassifier(random_state=12)
 classifiers=[('Logistic',lr_pipe), 
('knn',grid_knn), 
('dt',grid_dt), 
('gradient',grad), 
('RF',grid_rf), 
('Ada',ada), 
('XGb',xg_cl), 
('XgbGrid',grid_xg)]
vc=VotingClassifier(estimators=classifiers)
 vc.fit(ccX_train,cy_train)
 y_pred=vc.predict(ccX_test)
 print(accuracy_score(cy_test,y_pred))<save_to_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 )
test_1=cctest
test_2=pd.DataFrame(test_1,columns=test_1.columns)
 ans=vc.predict(test_2)
 sub=pd.DataFrame({
'PassengerId':test_id.astype(int),
'Survived':ans.astype(int)
 })
 print(sub.head())
 sub.to_csv('submissions.csv',index=False )<import_modules>
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 )
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV<load_from_csv>
for df in [train, test]:
df.drop('img_name', axis=1, inplace=True )
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 y = train.Survived
passengerid = test.PassengerId
titanic = train.append(test, ignore_index = True )<split>
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
train_index = len(train)
 test_index = len(titanic)- len(test )<feature_engineering>
X = train.drop('target', axis=1)
 y = train.target
titanic['Title'] = titanic.Name.apply(lambda x: x.split(',')[1].split('.')[0].strip() )<count_values>
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 )
titanic.Title.value_counts()<define_variables>
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,
)
normalized_title = {
'Mr':"Mr",
'Mrs': "Mrs",
'Ms': "Mrs",
'Mme':"Mrs",
'Mlle':"Miss",
'Miss':"Miss",
'Master':"Master",
'Dr':"Officer",
'Rev':"Officer",
'Col':"Officer",
'Capt':"Officer",
'Major':"Officer",
'Lady':"Royalty",
'Sir':"Royalty",
'the Countess':"Royalty",
'Dona':"Royalty",
'Don':"Royalty",
'Jonkheer':"Royalty"
}<categorify>
estimators = [xg]
titanic.Title = titanic.Title.map(normalized_title )<count_values>
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
print(titanic.Title.value_counts() )<count_missing_values>
xg.fit(X_train, y_train)
 validation = xg.predict_proba(X_test)[:, 1]
roc_auc_score(y_test, validation )
titanic.Age = grouped.Age.apply(lambda x: x.fillna(x.median()))
titanic.isnull().sum()<data_type_conversions>
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']
titanic.Fare = titanic.Fare.fillna(titanic.Fare.mean())
 titanic.Cabin = titanic.Cabin.fillna('U')
 most_Embarked = titanic.Embarked.value_counts().index[0]
titanic.Embarked = titanic.Embarked.fillna(most_Embarked )<count_missing_values>
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 )
titanic.isnull().sum()<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]
titanic['FamilySize'] = titanic['SibSp'] + titanic['Parch'] + 1<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]
titanic['Cabin'].value_counts()<categorify>
X_train.drop(['sex_unknown', 'width','height'], axis=1, inplace=True)
 test.drop(['width','height'], axis=1, inplace=True )
titanic.Cabin = titanic.Cabin.map(lambda x: x[0])
 titanic.Cabin.head()<categorify>
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,
)
titanic.Sex = titanic.Sex.map({"male":0,"female":1})
 title_dummies = pd.get_dummies(titanic.Title , prefix = "Title")
 cabin_dummies = pd.get_dummies(titanic.Cabin , prefix = "Cabin")
 pclass_dummies = pd.get_dummies(titanic.Pclass , prefix ="Pclass")
 embarked_dummies = pd.get_dummies(titanic.Embarked , prefix = "Embarked" )<concatenate>
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 )
<drop_column><EOS> 
effnet = pd.read_csv('.. /input/blended-effnets-from-previous-notebook/blended_effnets.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 )