Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<categorify>
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
from sklearn.linear_model import LogisticRegression
import os
title_encoder = LabelEncoder().fit(train.title)
 train.title = title_encoder.transform(train.title)
 test.title = title_encoder.transform(test.title )<categorify>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
sex_encoder = LabelEncoder().fit(train.Sex)
 train.Sex = sex_encoder.transform(train.Sex)
 test.Sex = sex_encoder.transform(test.Sex )<feature_engineering>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
age_translator = pd.concat([train, test], sort=False ).groupby('title' ).Age.median().to_dict()
train['completeAge'] = train.apply(lambda x: age_translator[x.title], axis=1)
 train.loc[train.Age.notnull() , 'completeAge'] = train.loc[train.Age.notnull() , 'Age']
test['completeAge'] = test.apply(lambda x: age_translator[x.title], axis=1)
 test.loc[test.Age.notna() , 'completeAge'] = test.loc[test.Age.notna() , 'Age'] 
<feature_engineering>
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' )
train['familysize'] = train.Parch + train.SibSp + 1
test['familysize'] = test.Parch + test.SibSp + 1
train.drop(['Parch', 'SibSp'], inplace=True, axis=1)
 train['hasfamily'] = 0
train.loc[train.familysize == 1, 'hasfamily'] = 1
test['hasfamily'] = 0
test.loc[test.familysize == 1, 'hasfamily'] = 1<data_type_conversions>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
train['ticketno'] = train.Ticket.apply(lambda x: x.split() [-1])
 train.ticketno.replace('LINE', "0", inplace=True)
 train.ticketno = train['ticketno'].astype(np.int)
 test['ticketno'] = test.Ticket.apply(lambda x: x.split() [-1])
 test.ticketno = test['ticketno'].astype(np.int )<categorify>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
train['decklevel'] = train.Cabin.dropna().apply(lambda x: str(x)[0])
 test['decklevel'] = test.Cabin.dropna().apply(lambda x: str(x)[0])
 decklevel_encoder = LabelEncoder().fit(train.decklevel.dropna())
 train.loc[train.decklevel.notna() , 'decklevel'] = decklevel_encoder.transform(train.decklevel.dropna())
 test.loc[test.decklevel.notna() , 'decklevel'] = decklevel_encoder.transform(test.decklevel.dropna() )<categorify>
train_EB0_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB0_ns.npy')
 test_EB0_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB0_ns.npy' )
train.Embarked.fillna('S', inplace=True)
 embarked_encoder = LabelEncoder().fit(train.Embarked)
 train.Embarked = embarked_encoder.transform(train.Embarked)
 test.Embarked = embarked_encoder.transform(test.Embarked )<feature_engineering>
X_train, X_val, y_train, y_val = train_test_split(train_EB0_ns, target, test_size=0.1, random_state=42 )
test.loc[test.Fare.isna() , 'Fare'] = train.Fare.median()<define_variables>
%%time
lr = LogisticRegression(C=0.021, max_iter=10000)
 lr.fit(X_train, y_train )
featurelist = ['Pclass', 'Fare', 'title', 'Sex', 'Embarked', 'rich_small_families', 'completeAge', 'hasfamily']<train_on_grid>
val_preds = lr.predict_proba(X_val)[:,1]
parameters = {'n_estimators': [40, 50, 60, 70],
'max_depth': [7, 10, 13],
'max_features': range(3, len(featurelist)) ,
'min_samples_leaf': [1, 2, 3],
'min_samples_split': [7, 10, 12]}
model = RandomForestClassifier(random_state=42)
 grid = GridSearchCV(model, param_grid=parameters, cv=5, n_jobs=-1, scoring='accuracy')
 grid.fit(train[featurelist], train.Survived )<find_best_params>
roc_auc_score(y_val, val_preds )
grid.best_params_<find_best_score>
log_loss(y_val, val_preds )
grid.best_score_<compute_train_metric>
test_preds = lr.predict_proba(test_EB0_ns)[:,1]
<predict_on_test><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()