Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<count_missing_values>
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
test.isnull().any().sum()<feature_engineering>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
X_train /= 255.
 test /= 255 .<categorify>
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 = to_categorical(y_train, num_classes = 10 )<split>
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' )
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state= 3 )<import_modules>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization<choose_model_class>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
model = Sequential()
model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu',
input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))<choose_model_class>
train_EB3_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB3_ns.npy')
 test_EB3_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB3_ns.npy' )
optimizer = Adamax(lr=0.001 )<choose_model_class>
X_train, X_val, y_train, y_val = train_test_split(train_EB3_ns, target, test_size=0.1, random_state=42 )
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
%%time
lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<train_model>
val_preds = lr.predict_proba(X_val)[:,1]
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1)
 datagen.fit(X_train )<train_model>
roc_auc_score(y_val, val_preds )
%%time
model.fit_generator(datagen.flow(X_train,y_train, batch_size= 86),
epochs = 100, validation_data =(X_val,y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // 86,
callbacks=[learning_rate_reduction] )<predict_on_test>
log_loss(y_val, val_preds )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
test_preds = lr.predict_proba(test_EB3_ns)[:,1]
<set_options><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()