Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<load_from_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
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 y_train = train["label"]
x_train = train.drop(labels = ["label"],axis = 1)
 y_train.value_counts()<feature_engineering>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
x_train = x_train / 255.0
test = test / 255.0<define_variables>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
random_seed = 2<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=random_seed )<categorify>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
print('x_train shape:', x_train.shape)
 print(x_train.shape[0], 'train samples')
 print(x_val.shape[0], 'test samples')
 y_train = np_utils.to_categorical(y_train)
 y_val = np_utils.to_categorical(y_val)
 print("Number of Classes: " + str(y_val.shape[1]))
num_classes = y_val.shape[1]
num_pixels = x_train.shape[1] * x_train.shape[2]
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
padding = "same",
input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
padding = "same",
input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
optimizer = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
 model.compile(loss = 'categorical_crossentropy',
optimizer = optimizer,
metrics = ['accuracy'])
 print(model.summary())
<init_hyperparams>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
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 )<choose_model_class>
train_EB2_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB2_ns.npy')
 test_EB2_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB2_ns.npy' )
batch_size = 128
epochs = 50
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',
patience=2,
verbose=1,
factor=0.5,
min_lr=0.00001)
 earlystop = EarlyStopping(monitor = 'val_loss',
min_delta = 0,
patience = 5,
verbose = 1,
restore_best_weights = True)
 callbacks = [earlystop, learning_rate_reduction]
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs=epochs,
verbose=1,
callbacks=callbacks,
steps_per_epoch = x_train.shape[0] // batch_size,
validation_data =(x_val, y_val))
score = model.evaluate(x_val, y_val, verbose=0)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1] )<predict_on_test>
X_train, X_val, y_train, y_val = train_test_split(train_EB2_ns, target, test_size=0.1, random_state=42 )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
%%time
lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("2.csv",index=False )<load_from_csv>
val_preds = lr.predict_proba(X_val)[:,1]
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
roc_auc_score(y_val, val_preds )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
log_loss(y_val, val_preds )
X_train = train.drop('label',axis = 1)
 y_train = train.label<count_values>
test_preds = lr.predict_proba(test_EB2_ns)[:,1]
<count_missing_values><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()