Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<predict_on_test>
import lightgbm as lgb
from sklearn.model_selection import train_test_split
from sklearn import metrics
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
test_preds = lr.predict_proba(test_EB7_ns)[:,1]<save_to_csv>
train = pd.read_csv('.. /input/train.csv')
 train.head()
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<install_modules>
test = pd.read_csv('.. /input/test.csv')
 test.head()
!pip install --use-feature=2020-resolver https://s3-us-west-2.amazonaws.com/xgboost-nightly-builds/xgboost-1.3.0_SNAPSHOT%2B00b0ad1293b4fa74d6aca5da4e9ab7a9d16777f0-py3-none-manylinux2010_x86_64.whl<import_modules>
train_y = train['Survived']
train_y
xgb.__version__<import_modules>
del train['Name'], train['Ticket'], train['Cabin'], train['Survived']
del test['Name'], test['Ticket'], test['Cabin']
train.head()
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
import gc
import os 
<load_from_csv>
train['Sex'] = train['Sex'].replace({'male': 0, 'female':1})
 train['Embarked'] = train['Embarked'].replace({'Q': 0, 'S':1, 'C':1})
 test['Sex'] = test['Sex'].replace({'male': 0, 'female':1})
 test['Embarked'] = test['Embarked'].replace({'Q': 0, 'S':1, 'C':1} )
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
train[train['Pclass'].isnull() ]
train[train['Sex'].isnull() ]
train[train['Age'].isnull() ]
train[train['SibSp'].isnull() ]
train[train['Parch'].isnull() ]
train[train['Fare'].isnull() ]
train[train['Embarked'].isnull() ]
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224<load_pretrained>
test[test['Pclass'].isnull() ]
test[test['Sex'].isnull() ]
test[test['Age'].isnull() ]
test[test['SibSp'].isnull() ]
test[test['Parch'].isnull() ]
test[test['Fare'].isnull() ]
test[test['Embarked'].isnull() ]
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )<define_variables>
X, y = train, train_y
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)
 print("X_train len:{}".format(len(X_train)))
 print("X_test len:{}".format(len(X_test)))
 print("y_train len:{}".format(len(y_train)))
 print("y_test len:{}".format(len(y_test)) )
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
y_train.value_counts(normalize=True)
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
y_test.value_counts(normalize=True )
train_EB6 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/train_EB6.npy')
 test_EB6 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/test_EB6.npy' )<split>
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
 lgbm_params = {
'objective': 'binary',
'metric': 'auc',
}
model = lgb.train(lgbm_params, lgb_train, valid_sets=lgb_eval)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration)
 fpr, tpr, thresholds = metrics.roc_curve(y_test, y_pred)
 auc = metrics.auc(fpr, tpr)
 print(auc)
 y_pred
X_train, X_val, y_train, y_val = train_test_split(train_EB6, target, test_size=0.1, random_state=42 )<train_model>
y_test_pred = model.predict(test, num_iteration=model.best_iteration)
 y_test_pred
xgb_params = {
'eta': 0.05,
'max_depth': 3,
'subsample': 0.8,
'colsample_bytree': 0.7,
'objective': 'reg:logistic',
'eval_metric': 'auc',
'verbosity': 0,
'tree_method': 'gpu_hist',
'predictor': 'gpu_predictor'
}
dtrain = xgb.DMatrix(X_train, y_train)
 dval = xgb.DMatrix(X_val, y_val)
 dtest = xgb.DMatrix(test_EB6 )<drop_column>
preds = np.round(y_test_pred ).astype(int)
 preds
del train_EB6, test_EB6, X_train, X_val
gc.collect()<train_model>
for i in range(0, len(preds)) :
print("{0},{1}".format(test['PassengerId'][i], preds[i]))
%%time
model = xgb.train(xgb_params, dtrain, num_boost_round=120 )<predict_on_test>
gender_submission = pd.read_csv('.. /input/gender_submission.csv')
 gender_submission.head()
val_preds = model.predict(dval )<compute_test_metric>
gender_submission['Survived'] = preds
gender_submission.head()
roc_auc_score(y_val, val_preds )<compute_test_metric>
gender_submission.to_csv("submit.csv", index=False )