Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
%matplotlib inline
sns.set_style('darkgrid' )
DATA_PATH = '/kaggle/input/efficientbx-melanoma-classification-with-tf'
history_files = [f for f in listdir(DATA_PATH)if isfile(join(DATA_PATH, f)) and f.split('_')[0] == 'history']
submit_files = [f for f in listdir(DATA_PATH)if isfile(join(DATA_PATH, f)) and f.split('_')[0] == 'submit']<load_from_csv>
train_df = pd.read_csv('.. /input/titanic/train.csv')
 test_df = pd.read_csv('.. /input/titanic/test.csv' )
list_results = []
for file_name_i in history_files:
model_name_i = file_name_i[8:22]
fold_name_i = file_name_i[23:29]
df_i = pd.read_csv(os.path.join(DATA_PATH, file_name_i), index_col=0)
 auc_i = df_i[df_i.val_loss == df_i.val_loss.min() ]['val_auc'].iloc[0]
loss_i = df_i.val_loss.min()
list_results.append([model_name_i, fold_name_i, auc_i, loss_i])
 df_results = pd.DataFrame(list_results, columns = ['Model', 'Fold', 'AUC', 'Loss'])
 for model_name_i in df_results.Model.unique() :
mean_auc_i = df_results.AUC[df_results.Model == model_name_i].mean()
mean_loss_i = df_results.Loss[df_results.Model == model_name_i].mean()
df_i = pd.DataFrame([[model_name_i, 'mean_fold', mean_auc_i, mean_loss_i]], columns = ['Model', 'Fold', 'AUC', 'Loss'])
 df_results = pd.concat([df_results, df_i])
 df_results = df_results.sort_values(by = ['Model', 'Fold'], ascending = True ).reset_index().drop(columns = ['index'] )<load_from_csv>
train_df.isnull().sum()
model_names = ['EfficientNetB' + str(i)for i in range(8)]
fold_names = ['fold_' + str(i)for i in range(4)]
dict_df_history = {}
for model_i in model_names:
dict_df_history[model_i] = {}
for fold_i in fold_names:
file_name_i = 'history_' + model_i + '_' + fold_i + '.csv'
df_history_i = pd.read_csv(os.path.join(DATA_PATH, file_name_i))
df_history_i=df_history_i.rename(columns = {
'Unnamed: 0': 'Epoch',
'loss': 'Train Loss',
'auc': 'Train AUC',
'val_loss': 'Valid Loss',
'val_auc': 'Valid AUC',
'lr': 'Learning Rate'
} 
)
 dict_df_history[model_i][fold_i] = df_history_i<save_to_csv>
test_df.isnull().sum()
sample_submit = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')
 for model_name_i in model_names:
target_list = []
for fold_i in fold_names:
target_i = pd.read_csv(os.path.join(DATA_PATH, 'submit_' + model_name_i + '_' + fold_i + '.csv')) ['target'].values
target_list.append(target_i)
 df_submit_i = sample_submit.copy()
df_submit_i['target'] = np.mean(target_list, axis=0)
 df_submit_i.to_csv('submit_' + model_name_i + '_mean_fold.csv', index=False)
 print(model_name_i + ' - Mean Fold submit file saved' )<install_modules>
comp_df = pd.concat([train_df, test_df])
 comp_df.reset_index(drop=True, inplace=True )
!pip install xgboost 
<load_from_csv>
comp_df.isnull().sum()
train= pd.read_csv('.. /input/siim-isic-melanoma-classification/train.csv')
 test= pd.read_csv('.. /input/siim-isic-melanoma-classification/test.csv')
 sub = pd.read_csv('.. /input/siim-isic-melanoma-classification/sample_submission.csv')
 train.head()
train.target.value_counts() 
<data_type_conversions>
comp_df.drop('PassengerId',axis=1, inplace=True)
 comp_df.drop('Cabin',axis=1,inplace=True)
 comp_df.drop("Ticket",axis=1, inplace=True )
train['sex'] = train['sex'].fillna('na')
 train['age_approx'] = train['age_approx'].fillna(0)
 train['anatom_site_general_challenge'] = train['anatom_site_general_challenge'].fillna('na')
 test['sex'] = test['sex'].fillna('na')
 test['age_approx'] = test['age_approx'].fillna(0)
 test['anatom_site_general_challenge'] = test['anatom_site_general_challenge'].fillna('na' )<data_type_conversions>
p1 = comp_df[comp_df.Pclass==1]['Age'].median()
p2 = comp_df[comp_df.Pclass==2]['Age'].median()
p3 = comp_df[comp_df.Pclass==3]['Age'].median()
def fill_age(row):
if np.isnan(row.Age):
if row.Pclass == 1:
return p1
elif row.Pclass == 2:
return p2
elif row.Pclass == 3:
return p3
else:
return row.Age
comp_df.Age = comp_df.apply(fill_age, axis=1 )
train['sex'] = train['sex'].astype("category" ).cat.codes +1
train['anatom_site_general_challenge'] = train['anatom_site_general_challenge'].astype("category" ).cat.codes +1
train.head()<data_type_conversions>
comp_df = comp_df[comp_df.Age<80]
test['sex'] = test['sex'].astype("category" ).cat.codes +1
test['anatom_site_general_challenge'] = test['anatom_site_general_challenge'].astype("category" ).cat.codes +1
test.head()<prepare_x_and_y>
comp_df.Fare.isnull().sum()
x_train = train[['sex', 'age_approx','anatom_site_general_challenge']]
y_train = train['target']
x_test = test[['sex', 'age_approx','anatom_site_general_challenge']]
train_DMatrix = xgb.DMatrix(x_train, label= y_train)
 test_DMatrix = xgb.DMatrix(x_test )<init_hyperparams>
comp_df.Fare.fillna(comp_df.Fare.median() , inplace=True )
param = {
'booster':'gbtree',
'eta': 0.3,
'num_class': 2,
'max_depth':
}
epochs = 100<choose_model_class>
upper_limit = comp_df.Fare.quantile(0.75)+(1.5 * iqr(comp_df.Fare))
lower_limit = comp_df.Fare.quantile(0.25)-(1.5 * iqr(comp_df.Fare))
clf = xgb.XGBClassifier(n_estimators=2000,
max_depth=8,
objective='multi:softprob',
seed=0,
nthread=-1,
learning_rate=0.15,
num_class = 2,
scale_pos_weight =(32542/584)) 
<train_model>
comp_df[(comp_df.Fare>upper_limit)&(comp_df.Survived.notnull())].shape
clf.fit(x_train, y_train )<predict_on_test>
comp_df[(comp_df.Fare>100)&(comp_df.Survived.notnull())].shape
clf.predict_proba(x_test)[:,1]
sub.target = clf.predict_proba(x_test)[:,1]
sub_tabular = sub.copy()<load_from_csv>
train_df = comp_df[comp_df.Survived.notnull() ]
test_df = comp_df[comp_df.Survived.isnull() ]
sub_public_merge = pd.read_csv('/kaggle/input/submission-9/submission_935.csv')
 sub_mean = pd.read_csv('/kaggle/input/siim-isic-multiple-model-training-stacking-923/submission_mean.csv' )<prepare_output>
train_df.shape, test_df.shape
train_df = train_df[train_df.Fare<=100]
comp_df = pd.concat([train_df,test_df] )
sub.target = sub_mean.target *0.1 + sub_public_merge.target *0.7 + sub_tabular.target *0.2<save_to_csv>
comp_df.Embarked.isnull().sum()
sub.head()
sub.to_csv('submission.csv', index = False )<install_modules>
comp_df.Embarked.value_counts()
!pip install.. /input/python-datatable/datatable-0.11.0-cp37-cp37m-manylinux2010_x86_64.whl > /dev/null 2>&1<import_modules>
comp_df.Embarked.fillna('S',inplace=True )
import numpy as np
import random
import pandas as pd
import joblib
import psutil<set_options>
comp_df.isnull().sum()
_ = np.seterr(divide='ignore', invalid='ignore' )<define_variables>
comp_df['family_size'] = comp_df.SibSp + comp_df.Parch
data_types_dict = {
'timestamp': 'int64',
'user_id': 'int32',
'content_id': 'int16',
'content_type_id':'int8',
'task_container_id': 'int16',
'answered_correctly': 'int8',
'prior_question_elapsed_time': 'float32',
'prior_question_had_explanation': 'bool'
}
target = 'answered_correctly'<load_from_csv>
comp_df.drop(['SibSp','Parch'], axis=1, inplace=True )
print('start read train data...')
 train_df = dt.fread('.. /input/riiid-test-answer-prediction/train.csv', columns=set(data_types_dict.keys())).to_pandas()<train_model>
comp_df['title'] = comp_df.Name.str.extract(r'([\w]+[.])' )
print('start handle lecture data...' )<load_from_csv>
comp_df.Sex = comp_df.Sex.map({'male':1,'female':0})
 comp_df.Embarked = comp_df.Embarked.map({'C':0, 'Q':1,'S':2} )
lectures_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/lectures.csv' )<categorify>
comp_df.family_size= comp_df.apply(lambda x:4 if x.family_size>4 else x.family_size,axis=1 )
lectures_df['type_of'] = lectures_df['type_of'].replace('solving question', 'solving_question')
 lectures_df = pd.get_dummies(lectures_df, columns=['part', 'type_of'])
 part_lectures_columns = [column for column in lectures_df.columns if column.startswith('part')]
types_of_lectures_columns = [column for column in lectures_df.columns if column.startswith('type_of_')]<merge>
comp_df['title'] = comp_df['title'].str.replace(r'Sir.', 'Mr.')
 comp_df['title'] = comp_df['title'].str.replace(r'Rev.','Mr.')
 comp_df['title'] = comp_df['title'].str.replace(r'Lady.','Ms.')
 comp_df['title'] = comp_df['title'].str.replace(r'Mrs.','Ms.')
 comp_df['title'] = comp_df['title'].str.replace(r'Miss.','Ms.')
 comp_df['title'] = comp_df['title'].str.replace(r'Mlle.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Mme.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Dona.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Countess.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Major.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Jonkheer.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Col.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Capt.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Master.','scholar')
 comp_df['title'] = comp_df['title'].str.replace(r'Dr.','scholar')
 comp_df['title'] = comp_df['title'].str.replace('Don.','scholar' )
train_lectures = train_df[train_df.content_type_id == True].merge(lectures_df, left_on='content_id', right_on='lecture_id', how='left' )<groupby>
comp_df.drop('Name',axis=1, inplace=True )
user_lecture_stats_part = train_lectures.groupby('user_id',as_index = False)[part_lectures_columns + types_of_lectures_columns].sum()<data_type_conversions>
comp_df.title = comp_df.title.map({'other':0, 'scholar':1, 'Ms.':2, 'Mr.':3} )
lecturedata_types_dict = {
'user_id': 'int32',
'part_1': 'int8',
'part_2': 'int8',
'part_3': 'int8',
'part_4': 'int8',
'part_5': 'int8',
'part_6': 'int8',
'part_7': 'int8',
'type_of_concept': 'int8',
'type_of_intention': 'int8',
'type_of_solving_question': 'int8',
'type_of_starter': 'int8'
}
user_lecture_stats_part = user_lecture_stats_part.astype(lecturedata_types_dict )<data_type_conversions>
comp_df.Age = pd.cut(comp_df.Age, 7, labels=[0,1,2,3,4,5,6])
 comp_df.Fare = pd.cut(comp_df.Fare, 7, labels=[0,1,2,3,4,5,6])
 comp_df.Age = comp_df.Age.astype('int')
 comp_df.Fare = comp_df.Fare.astype('int' )
for column in user_lecture_stats_part.columns:
if(column !='user_id'):
user_lecture_stats_part[column] =(user_lecture_stats_part[column] > 0 ).astype('int8' )<drop_column>
train_df = comp_df[comp_df.Survived.notnull() ]
test_df = comp_df[comp_df.Survived.isnull() ]
del(train_lectures)
 gc.collect()<categorify>
test_df.drop('Survived',axis=1, inplace=True )
user_lecture_agg = train_df.groupby('user_id')['content_type_id'].agg(['sum', 'count'])
 user_lecture_agg=user_lecture_agg.astype('int16' )<data_type_conversions>
x_train = train_df.drop('Survived',axis=1)
 y_train = train_df.Survived
cum = train_df.groupby('user_id')['content_type_id'].agg(['cumsum', 'cumcount'])
 cum['cumcount']=cum['cumcount']+1
train_df['user_interaction_count'] = cum['cumcount']
train_df['user_interaction_timestamp_mean'] = train_df['timestamp']/cum['cumcount']
train_df['user_lecture_sum'] = cum['cumsum']
train_df['user_lecture_lv'] = cum['cumsum'] / cum['cumcount']
train_df.user_lecture_lv=train_df.user_lecture_lv.astype('float16')
 train_df.user_lecture_sum=train_df.user_lecture_sum.astype('int16')
 train_df.user_interaction_count=train_df.user_interaction_count.astype('int16')
 train_df['user_interaction_timestamp_mean']=train_df['user_interaction_timestamp_mean']/(1000*3600)
 train_df.user_interaction_timestamp_mean=train_df.user_interaction_timestamp_mean.astype('float32')
<set_options>
from sklearn.model_selection import GridSearchCV, cross_val_score, RepeatedStratifiedKFold, train_test_split
from xgboost import XGBClassifier
from sklearn.metrics import classification_report
del cum
gc.collect()<train_model>
model = XGBClassifier()
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=5, random_state=11)
 scores = cross_val_score(model, x_train, y_train, cv=cv, n_jobs=-1, verbose=True, scoring='roc_auc')
 print(np.mean(scores),np.std(scores))
print('start handle train_df...' )<data_type_conversions>
best_model = XGBClassifier(n_estimators=100,
subsample=0.7,
max_depth=3,
learning_rate=0.01,
colsample_bytree=1 )
<groupby><EOS> 
best_model.fit(x_train,y_train)
 answer = pd.DataFrame(best_model.predict(test_df))
test_file = pd.read_csv('.. /input/titanic/test.csv')
 answer['PassengerId']= test_file.PassengerId.tolist()
answer.set_index('PassengerId',inplace=True)
 answer.columns=['Survived']
answer.Survived = answer.Survived.astype('int')
 answer.to_csv('answer_xgb_binning_new02_bin7_familysize02.csv' )