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
submit_df.to_csv('submission.csv', index=False )<load_from_csv>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 train_labels = train_data["label"]
train_data = train_data.drop(['label'], axis = 1)
 train_data = train_data /255.
 test_data = test_data / 255.
 train_data = train_data.values.reshape(train_data.shape[0],28,28,1)
 test_data = test_data.values.reshape(test_data.shape[0],28,28,1)
 labels = to_categorical(train_labels)
 X_train, X_test, y_train, y_test = train_test_split(train_data, labels, test_size = 0.2, random_state = 42 )<choose_model_class>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
learning_rate_cb = ReduceLROnPlateau(monitor = 'val_acc',
patience = 2,
verbose = 1,
factor = 0.5,
min_lr = 1e-5 )<train_model>
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' )
datagen = ImageDataGenerator(
 zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1,
rotation_range = 10 
)
 datagen.fit(X_train )<choose_model_class>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
batchsize = 512
num_epochs = 30
n_model_runs = 10
modellist = list()
for i in range(n_model_runs):
print("+++++++++ running model number", i+1)
 model = models.Sequential([
Conv2D(16, [5,5], activation = 'relu', padding = 'same', input_shape = [28,28,1]),
MaxPooling2D([2,2]),
Conv2D(32, [5,5], activation = 'relu', padding = 'same'),
MaxPooling2D([2,2]),
Conv2D(64, [3,3], activation = 'relu', padding = 'same'),
MaxPooling2D([2,2]),
Conv2D(64, [3,3], activation = 'relu', padding = 'same'),
MaxPooling2D([2,2]),
Flatten() ,
Dense(512, activation = 'relu'),
Dropout(0.3),
Dense(1024, activation = 'relu'),
Dropout(0.5),
Dense(10, activation = 'softmax')
 ])
 model.compile(optimizer = Adam(lr=1e-3),
loss='categorical_crossentropy',
metrics = ['accuracy'])
 model.fit_generator(datagen.flow(X_train, y_train, batchsize),
epochs = num_epochs,
steps_per_epoch = X_train.shape[0]/batchsize,
validation_data =(X_test, y_test),
callbacks=[learning_rate_cb],
verbose = 1)
 modellist.append(model )<predict_on_test>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
prediction = [model.predict(test_data)for model in modellist]
prediction = np.sum(prediction, axis=0)
 prediction = np.argmax(prediction,axis=1 )<save_to_csv>
train_EB4 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/train_EB4.npy')
 test_EB4 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/test_EB4.npy' )
submission = pd.DataFrame({"ImageId": list(range(1, len(prediction)+1)) , "Label": prediction})
 submission.to_csv('submission.csv', index = False, header = True )<save_to_csv>
X_train, X_val, y_train, y_val = train_test_split(train_EB4, target, test_size=0.1, random_state=42 )
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv()
b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD')) 
<load_from_csv>
%%time
lr = LogisticRegression(C=0.015, max_iter=10000)
 lr.fit(X_train, y_train )
train_data = pd.read_csv(".. /input/train.csv")
 test_data = pd.read_csv(".. /input/test.csv" )<define_variables>
val_preds = lr.predict_proba(X_val)[:,1]
passenger_id = test_data["PassengerId"]<sort_values>
roc_auc_score(y_val, val_preds )
train_data[['SibSp','Survived']].groupby(['SibSp'],as_index=False ).count().sort_values(by='Survived',ascending=False)
<sort_values>
log_loss(y_val, val_preds )
train_data[['Parch','Survived']].groupby(['Parch'],as_index=False ).count().sort_values(by='Survived',ascending=False)
<sort_values>
test_preds = lr.predict_proba(test_EB4)[:,1]
<sort_values><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()