Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<sort_values>
!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_data[['Age','Survived']].groupby(['Age'],as_index=False ).count().sort_values(by='Survived',ascending=False ).head() 
<prepare_x_and_y>
xgb.__version__
target_variable = train_data["Survived"]
train_data.drop(["Survived"], axis = 1, inplace=True )<concatenate>
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
all_data = pd.concat([train_data, test_data], axis = 0 )<drop_column>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
all_data.drop(["PassengerId", "Ticket" ], axis=1, inplace = True )<sort_values>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
total = all_data.isnull().sum().sort_values(ascending=False)
 percent =(all_data.isnull().sum() /all_data.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data[total > 0]<data_type_conversions>
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' )
all_data["Cabin"] = all_data["Cabin"].fillna("None" )<feature_engineering>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
all_data["Age"] = all_data["Age"].fillna(all_data["Age"].mean())
 all_data["Fare"] = all_data["Fare"].fillna(all_data["Fare"].mean() )<categorify>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
embarked_mode = all_data["Embarked"].mode() [0]
all_data["Embarked"] = all_data["Embarked"].fillna(embarked_mode )<concatenate>
train_InceptionResNetV2 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_InceptionResNetV2.npy')
 test_InceptionResNetV2 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_InceptionResNetV2.npy' )
simplify_ages(all_data )<concatenate>
X_train, X_val, y_train, y_val = train_test_split(train_InceptionResNetV2, target, test_size=0.1, random_state=42 )
simplify_fares(all_data )<feature_engineering>
xgb_params = {
'eta': 0.025,
'max_depth': 7,
'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_InceptionResNetV2 )
def format_name(df):
df['Lname'] = df.Name.apply(lambda x: x.split(' ')[0])
 df['NamePrefix'] = df.Name.apply(lambda x: x.split(' ')[1])
 return df<drop_column>
del train_InceptionResNetV2, test_InceptionResNetV2, X_train, X_val
gc.collect()
format_name(all_data)
 all_data.drop(['Name'], axis=1, inplace=True )<drop_column>
%%time
model = xgb.train(xgb_params, dtrain, num_boost_round=200 )
all_data["family_members"] = all_data["SibSp"] + all_data["Parch"]
all_data.drop(["SibSp", "Parch" ], axis=1 )<categorify>
val_preds = model.predict(dval )
all_data.head()<categorify>
roc_auc_score(y_val, val_preds )
def encode_features(df):
features = ['Sex', 'Age', 'Fare', 'Embarked', 'Lname', 'NamePrefix', 'Cabin']
for feature in features:
le = preprocessing.LabelEncoder()
le = le.fit(df[feature])
 df[feature] = le.transform(df[feature])
 return df
all_data = encode_features(all_data)
 all_data.head()<prepare_x_and_y>
log_loss(y_val, val_preds )
train_data = all_data[:train_data.shape[0]]
test_data = all_data[train_data.shape[0]:]
y = target_variable<split>
%%time
val_shap_preds = model.predict(dval, pred_contribs=True )
X_train, X_test, y_train, y_test = train_test_split(train_data, y, test_size=0.2, random_state=42 )<define_search_space>
%%time
test_shap_preds = model.predict(dtest, pred_contribs=True)
clf = RandomForestClassifier()
parameters = {'n_estimators': [4, 6, 9],
'max_features': ['log2', 'sqrt','auto'],
'criterion': ['entropy', 'gini'],
'max_depth': [2, 3, 5, 10],
'min_samples_split': [2, 3, 5],
'min_samples_leaf': [1,5,8]
}
acc_scorer = make_scorer(accuracy_score)
 grid_obj = GridSearchCV(clf, parameters, scoring=acc_scorer)
 grid_obj = grid_obj.fit(X_train, y_train)
 clf = grid_obj.best_estimator_
clf.fit(X_train, y_train )<predict_on_test>
%%time
train_shap_preds = model.predict(dtrain, pred_contribs=True )
predictions = clf.predict(X_test)
 print("Accuracy of Random forest classifier" , accuracy_score(y_test, predictions))<choose_model_class>
test_preds = model.predict(dtest )
<save_to_csv><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()