Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
train_df = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_df = pd.read_csv("/kaggle/input/titanic/test.csv")
 train_df.shape, test_df.shape
dac = np.vstack(train.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)train['dac_year'] = dac[:,0] train['dac_month'] = dac[:,1] train['dac_day'] = dac[:,2] train.drop(['date_account_created'], axis=1,inplace=True)
 dac = np.vstack(test.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)test['dac_year'] = dac[:,0] test['dac_month'] = dac[:,1] test['dac_day'] = dac[:,2] test.drop(['date_account_created'], axis=1,inplace=True)
 test[(test.age < 14)&(test.age > 110)]=np.nan test['age'].fillna(train['age'].mean() , inplace=True)
 for categorical_feature in categorical_features: train[categorical_feature] = train[categorical_feature].astype('category')for categorical_feature in categorical_features: test[categorical_feature] = test[categorical_feature].astype('category')
 transform the dataset
smotetomek = SMOTETomek(sampling_strategy='auto')
 split the dataset into train and test sets
X_train1, X_test1, y_train1, y_test1 = train_test_split(X, y, test_size=0.30, random_state=1,shuffle=True)X_train, y_train = smotetomek.fit_resample(X_train1, y_train1)
 xgb = XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bynode=1, colsample_bytree=0.5, gamma=0, gpu_id=-1, importance_type='gain', interaction_constraints=None, learning_rate=0.3, max_delta_step=0, max_depth=6, min_child_weight=1, monotone_constraints=None, n_estimators=25, n_jobs=0, num_parallel_tree=1, objective='multi:softprob', random_state=0, reg_alpha=0, reg_lambda=1, scale_pos_weight=None, seed=0, subsample=0.5, tree_method=None, validate_parameters=False, verbosity=None)xgb.fit(X_train, y_train)target_names = ['NDF', 'US', 'other', 'FR', 'CA', 'GB', 'ES', 'IT', 'PT', 'NL','DE', 'AU'] y_predxgb=xgb.predict(X_test1)
<load_from_csv>
y = train_df['Survived']
train_df.Age[train_df.Age.isnull() ] = train_df.Age.median()
train_df.Age = train_df.Age.astype(str)
 train_df.Fare[train_df.Fare.isnull() ] = train_df.Fare.median()
train_df.Fare = train_df.Fare.astype(str)
 train_df.Cabin[train_df.Cabin.isnull() ] = 'NA'
MaxPassEmbarked = train_df.groupby('Embarked' ).count() ['PassengerId']
train_df.Embarked[train_df.Embarked.isnull() ] = 'NAA'
X = train_df.drop(columns=['Survived','Name','PassengerId'])
 X.head(100 )
print("reading data
")
 df_train = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/train_users_2.csv')
 df_test = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/test_users.csv')
 labels = df_train['country_destination'].values
df_train = df_train.drop(['country_destination'], axis=1)
 id_test = df_test['id']
piv_train = df_train.shape[0]
df_all = pd.concat(( df_train, df_test), axis=0, ignore_index=True)
 df_all = df_all.drop(['id', 'date_first_booking'], axis=1)
 df_all = df_all.fillna(-1)
 dac = np.vstack(df_all.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 df_all['dac_year'] = dac[:,0]
df_all['dac_month'] = dac[:,1]
df_all['dac_day'] = dac[:,2]
df_all = df_all.drop(['date_account_created'], axis=1)
 tfa = np.vstack(df_all.timestamp_first_active.astype(str ).apply(lambda x: list(map(int, [x[:4],x[4:6],x[6:8],x[8:10],x[10:12],x[12:14]])) ).values)
 df_all['tfa_year'] = tfa[:,0]
df_all['tfa_month'] = tfa[:,1]
df_all['tfa_day'] = tfa[:,2]
df_all = df_all.drop(['timestamp_first_active'], axis=1)
 av = df_all.age.values
df_all['age'] = np.where(np.logical_or(av<14, av>100), -1, av)
 ohe_feats = ['gender', 'signup_method', 'signup_flow', 'language', 'affiliate_channel', 'affiliate_provider', 'first_affiliate_tracked', 'signup_app', 'first_device_type', 'first_browser']
for f in ohe_feats:
df_all_dummy = pd.get_dummies(df_all[f], prefix=f)
 df_all = df_all.drop([f], axis=1)
 df_all = pd.concat(( df_all, df_all_dummy), axis=1)
 vals = df_all.values
X = vals[:piv_train]
le = LabelEncoder()
y = le.fit_transform(labels)
 X_test = vals[piv_train:]
xgb = XGBClassifier(max_depth=6, learning_rate=0.2, n_estimators=43,
objective='multi:softprob', subsample=1, colsample_bytree=0.5, seed=1)
 xgb.fit(X, y)
 y_pred = xgb.predict_proba(X_test)
 ids = []
cts = []
for i in range(len(id_test)) :
idx = id_test[i]
ids += [idx] * 5
cts += le.inverse_transform(np.argsort(y_pred[i])[::-1])[:5].tolist()
sub = pd.DataFrame(np.column_stack(( ids, cts)) , columns=['id', 'country'])
 sub.to_csv('submission.csv',index=False )<define_variables>
SEED = 1
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master' 
<import_modules>
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.40, random_state=SEED)
 cat_features = list(range(X.shape[1]))
from glob import glob
from sklearn.model_selection import GroupKFold, StratifiedKFold
import cv2
from skimage import io
import torch
from torch import nn
import os
from datetime import datetime
import time
import random
import cv2
import torchvision
from torchvision import transforms
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from torch.utils.data import Dataset,DataLoader
from torch.utils.data.sampler import SequentialSampler, RandomSampler
from torch.cuda.amp import autocast, GradScaler
import sklearn
import warnings
import joblib
from sklearn.metrics import roc_auc_score, log_loss
from sklearn import metrics
import warnings
import cv2
import pydicom
import timm
from scipy.ndimage.interpolation import zoom
from sklearn.metrics import log_loss<init_hyperparams>
train_data = Pool(data=X_train,
label=y_train,
cat_features=cat_features 
)
 valid_data = Pool(data=X_valid,
label=y_valid,
cat_features=cat_features
)
CFG = {
'fold_num': 10,
'seed': 719,
'model_arch': 'tf_efficientnet_b3_ns',
'img_size': 512,
'epochs': 32,
'train_bs': 28,
'valid_bs': 32,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 3,
'used_epochs': [6,7,8,9],
'weights': [1,1,1,1]
}<load_from_csv>
%%time
params = {'loss_function':'Logloss',
'eval_metric':'AUC',
'early_stopping_rounds': 200,
'verbose': 200,
'random_seed': SEED
}
cbc_7 = CatBoostClassifier(**params)
 cbc_7.fit(train_data,
eval_set=valid_data,
use_best_model=True,
plot=True 
);
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head()<count_values>
cbc_7.get_feature_importance(prettified=True )
train.label.value_counts()<load_from_csv>
test_df.Age[test_df.Age.isnull() ] = test_df.Age.median()
test_df.Age = test_df.Age.astype(str)
 test_df.Fare[test_df.Fare.isnull() ] = test_df.Fare.median()
test_df.Fare = test_df.Fare.astype(str)
 test_df.Cabin[test_df.Cabin.isnull() ] = 'NA'
MaxPassEmbarked = test_df.groupby('Embarked' ).count() ['PassengerId']
test_df.Embarked[test_df.Embarked.isnull() ] = 'NAA'
X_test = test_df.drop(columns=['Name','PassengerId'])
 X_test.head(100 )
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()<categorify>
%%time
n_fold = 10
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=SEED)
 params = {'loss_function':'Logloss',
'eval_metric':'AUC',
'early_stopping_rounds': 200,
'verbose': False,
'random_seed': SEED
}
test_data = Pool(data=X_test,
cat_features=cat_features)
 scores = []
prediction = np.zeros(X_test.shape[0])
 for fold_n,(train_index, valid_index)in enumerate(folds.split(X, y)) :
X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y[train_index], y[valid_index]
train_data = Pool(data=X_train,
label=y_train,
cat_features=cat_features)
 valid_data = Pool(data=X_valid,
label=y_valid,
cat_features=cat_features)
 model = CatBoostClassifier(**params)
 model.fit(train_data,
eval_set=valid_data,
use_best_model=True 
)
 score = model.get_best_score() ['validation']['AUC']
scores.append(score)
 y_pred = model.predict_proba(test_data)[:, 1]
prediction += y_pred
prediction /= n_fold
print('CV mean: {:.4f}, CV std: {:.4f}'.format(np.mean(scores), np.std(scores)) )
<import_modules><EOS> 
prediction = np.round(prediction,decimals=0)
 prediction = prediction.astype(int)
 output = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': prediction})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )