Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<feature_engineering>
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
for dataset in all_df:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1<groupby>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
train_df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean()<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
for dataset in all_df:
dataset['Surname'] = dataset.Name.str.extract('([A-Za-z]+)\,', expand=False )<create_dataframe>
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' )
tmp = train_df.groupby(['Surname'])['Survived'].value_counts()
df = pd.DataFrame(data={'Size of group with same Surname': tmp.values}, index=tmp.index ).reset_index().sort_values(['Size of group with same Surname', 'Surname'], ascending=False )<create_dataframe>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
tmp = df.groupby(['Size of group with same Surname'])['Survived'].value_counts()
df = pd.DataFrame(data={'Number': tmp.values}, index=tmp.index ).reset_index().sort_values(['Size of group with same Surname', 'Survived'], ascending=False)
 df<feature_engineering>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
for dataset in all_df:
dataset['Deck'] = dataset.Cabin.str.extract('^([A-Za-z]+)', expand=False )<groupby>
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' )
train_df[['Deck', 'Survived']].groupby(['Deck'], as_index=False ).mean()<data_type_conversions>
X_train, X_val, y_train, y_val = train_test_split(train_NAS_large, target, test_size=0.1, random_state=42 )
for dataset in all_df:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int )<feature_engineering>
%%time
lr = LogisticRegression(C=0.0075, max_iter=10000)
 lr.fit(X_train, y_train )
age_aprox = np.zeros(( 2,3))
for dataset in all_df:
for i in range(0, 2):
for j in range(0, 3):
aprox_age = dataset[(dataset['Sex'] == i)& \ 
(dataset['Pclass'] == j+1)]['Age'].dropna()
age_aprox[i,j] = aprox_age.median()
for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)&(dataset.Pclass == j+1),'Age'] = \
age_aprox[i,j]
dataset['Age'] = dataset['Age'].astype(int )<create_dataframe>
val_preds = lr.predict_proba(X_val)[:,1]
tmp = train_df.groupby(['Title', 'Pclass'])['Survived'].value_counts()
df = pd.DataFrame(data={'Passengers': tmp.values}, index=tmp.index ).reset_index()
df<categorify>
roc_auc_score(y_val, val_preds )
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in all_df:
dataset['Title'] = dataset['Title'].map(title_mapping )<feature_engineering>
log_loss(y_val, val_preds )
for dataset in all_df:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3<feature_engineering>
test_preds = lr.predict_proba(test_NAS_large)[:,1]
for dataset in all_df:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4<feature_engineering>
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()