Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<load_from_csv>
!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
train = pd.read_csv('.. /input/train.csv')
 train.info()<categorify>
xgb.__version__
def prep_data(df):
to_be_dropped = ['Name', 'Cabin', 'Ticket']
df['Embarked'] = df['Embarked'].fillna('S')
 df['Age'] = df['Age'].fillna(median_age)
 df['Fare'] = df['Fare'].fillna(median_fare)
 df['Companions'] = df['Parch'] + df['SibSp']
to_be_dropped.extend(['Parch', 'SibSp'])
 df.loc[ df['Age'] <= 16, 'Age'] = 0
df.loc[(df['Age'] > 16)&(df['Age'] <= 32), 'Age'] = 1
df.loc[(df['Age'] > 32)&(df['Age'] <= 48), 'Age'] = 2
df.loc[(df['Age'] > 48)&(df['Age'] <= 64), 'Age'] = 3
df.loc[ df['Age'] > 64, 'Age'] = 4
df['Age'] = df['Age'].astype(int)
 df.loc[ df['Fare'] <= 7.91, 'Fare'] = 0
df.loc[(df['Fare'] > 7.91)&(df['Fare'] <= 14.454), 'Fare'] = 1
df.loc[(df['Fare'] > 14.454)&(df['Fare'] <= 31), 'Fare'] = 2
df.loc[ df['Fare'] > 31, 'Fare'] = 3
df['Fare'] = df['Fare'].astype(int)
 df['Title'] = df['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 df['Title'] = df['Title'].fillna('NoTitle')
 df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Ms', 'Miss')
 df['Title'] = df['Title'].replace('Mme', 'Mrs')
 df['Embarked'] = df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 df['Sex'] = df['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 df['Title'] = df['Title'].map({'NoTitle': 0, 'Mr': 1, 'Miss': 2, 'Mrs': 3, 'Master': 4, 'Rare': 5} ).astype(int)
 df['CabinCode'] = df['Cabin'].astype(str ).str[0]
df['CabinCode'] = df['CabinCode'].map({'A': 1, 'B': 1, 'C': 1, 'D': 1, 'E':1, 'F':1, 'G': 1, 'T': 0, 'n' : 0} ).astype(int)
 df = df.set_index('PassengerId')
 df = df.drop(to_be_dropped, axis = 1)
 return df<split>
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
y = train['Survived']
X = train.drop('Survived', axis = 1)
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.35, random_state=7 )<train_on_grid>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
warnings.filterwarnings("ignore", category=DeprecationWarning)
 warnings.filterwarnings("ignore", category=UserWarning)
 lgbm = lgb.LGBMClassifier(nthread = 4, boosting_type = 'dart')
 param_grid = {'learning_rate': [0.08, 0.09, 0.1]}
grid_search = GridSearchCV(lgbm, param_grid, scoring='roc_auc', cv=10)
 grid_result = grid_search.fit(X, y)
 lgbm.set_params(learning_rate = grid_result.best_params_['learning_rate'])
 lgbm_param = lgbm.get_params()
lgbm_train = lgb.Dataset(X_train, y_train,
categorical_feature = ['Pclass', 'Sex', 'Age', 'Fare', 'Embarked', 'Title', 'CabinCode'])
 cvresult = lgb.cv(lgbm_param, lgbm_train, num_boost_round=1000, metrics=['auc'], early_stopping_rounds=50)
 lgbm.set_params(n_estimators=len(cvresult['auc-mean']))
lgbm.fit(X_train, y_train)
 print(lgbm )<compute_test_metric>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
accuracy = accuracy_score(y_test, lgbm.predict(X_test))
print("Accuracy: %.2f%%" %(accuracy * 100.0))<load_from_csv>
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' )
test = pd.read_csv('.. /input/test.csv')
 test.info()<save_to_csv>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
predictions = lgbm.predict(test)
 submission = pd.DataFrame({
"PassengerId": test.index,
"Survived": predictions
})
 submission.to_csv('submission.csv', index=False )<data_type_conversions>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
print("Last updated:")
 print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))<set_options>
train_NAS_large = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_2/train_NAS_large.npy')
 test_NAS_large = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_2/test_NAS_large.npy' )
init_notebook_mode(connected=True)
<load_from_csv>
X_train, X_val, y_train, y_val = train_test_split(train_NAS_large, target, test_size=0.1, random_state=42 )
train_df=pd.read_csv(PATH+'train.csv')
 test_df=pd.read_csv(PATH+'test.csv' )<train_model>
xgb_params = {
'eta': 0.05,
'max_depth': 4,
'subsample': 0.8,
'colsample_bytree': 0.7,
'objective': 'reg:logistic',
'eval_metric': 'auc',
'verbosity': 0,
'tree_method': 'hist',
'predictor': 'cpu_predictor'
}
dtrain = xgb.DMatrix(X_train, y_train)
 dval = xgb.DMatrix(X_val, y_val)
 dtest = xgb.DMatrix(test_NAS_large )
print("Train: rows:{} cols:{}".format(train_df.shape[0], train_df.shape[1]))
print("Test: rows:{} cols:{}".format(test_df.shape[0], test_df.shape[1]))<count_missing_values>
del train_NAS_large, test_NAS_large, X_train, X_val
gc.collect()
def missing_data(data):
total = data.isnull().sum().sort_values(ascending = False)
 percent =(data.isnull().sum() /data.isnull().count() *100 ).sort_values(ascending = False)
 return pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data(train_df )<count_missing_values>
%%time
model = xgb.train(xgb_params, dtrain, num_boost_round=110 )
missing_data(test_df )<create_dataframe>
val_preds = model.predict(dval )
def get_categories(data, val):
tmp = data[val].value_counts()
return pd.DataFrame(data={'Number': tmp.values}, index=tmp.index ).reset_index()<create_dataframe>
roc_auc_score(y_val, val_preds )
def get_survived_categories(data, val):
tmp = data.groupby('Survived')[val].value_counts()
return pd.DataFrame(data={'Number': tmp.values}, index=tmp.index ).reset_index()<count_values>
log_loss(y_val, val_preds )
train_df['Ticket'].value_counts().head(10 )<count_values>
%%time
val_shap_preds = model.predict(dval, pred_contribs=True )
train_df['Cabin'].value_counts().head(10 )<create_dataframe>
%%time
test_shap_preds = model.predict(dtest, pred_contribs=True)
tmp = train_df.groupby(['SibSp', 'Parch'])['Survived'].value_counts()
df = pd.DataFrame(data={'Passengers': tmp.values}, index=tmp.index ).reset_index()
hover_text = []
for index, row in df.iterrows() :
hover_text.append(( 'Sibilings: {}
'+
'Parents/Children: {}
'+
'Survived: {}
'+
'Passengers: {}' ).format(row['SibSp'],
row['Parch'],
row['Survived'],
row['Passengers']))
df['hover_text'] = hover_text<concatenate>
%%time
train_shap_preds = model.predict(dtrain, pred_contribs=True )
test_df['Survived'] = None
all_df = pd.concat([train_df, test_df], axis=0 )<categorify>
test_preds = model.predict(dtest )
<categorify><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()