Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<save_to_csv>
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
result[['PassengerId', 'Survived']].to_csv('submission.csv', header=True, index=False )<load_from_csv>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
y_train = df_train['label']
x_train = df_train.drop(labels = ['label'] , axis=1)
 del df_train<count_values>
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' )
y_train.value_counts()<feature_engineering>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
x_train = x_train/255.0
df_test = df_test/255.0<categorify>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
y_train = to_categorical(y_train , num_classes = 10 )<split>
train_EB6_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB6_ns.npy')
 test_EB6_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB6_ns.npy' )
x_train , x_val , y_train , y_val = train_test_split(x_train , y_train , test_size =.1 , random_state = 0 )<choose_model_class>
X_train, X_val, y_train, y_val = train_test_split(train_EB6_ns, target, test_size=0.1, random_state=42 )
classifier = Sequential()
classifier.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 classifier.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
classifier.add(MaxPool2D(pool_size=(2,2)))
 classifier.add(Dropout(0.25))
classifier.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
classifier.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
classifier.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 classifier.add(Dropout(0.25))
classifier.add(Flatten())
 classifier.add(Dense(256, activation = "relu"))
classifier.add(Dropout(0.5))
classifier.add(Dense(10, activation = "softmax"))<choose_model_class>
%%time
lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train )
classifier.compile(optimizer = 'adam' , loss = "categorical_crossentropy", metrics=["accuracy"] )<define_variables>
val_preds = lr.predict_proba(X_val)[:,1]
epochs = 30
batch_size = 126<init_hyperparams>
roc_auc_score(y_val, val_preds )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x_train )<train_model>
log_loss(y_val, val_preds )
history = classifier.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(x_val,y_val),
verbose = 2, steps_per_epoch=x_train.shape[0] // batch_size
)<predict_on_test>
test_preds = lr.predict_proba(test_EB6_ns)[:,1]
<save_to_csv><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()