Kaggle competition: Digit Recognizer
sub = pd.read_csv(dfk.iloc[6, 2])
 tmp = sub['site_path_timestamp'].apply(lambda s : pd.Series(s.split('_')))
 sub['site'] = tmp[0]
sub['path'] = tmp[1]
sub['timestamp'] = tmp[2].astype(float)
 processes = multiprocessing.cpu_count()
with multiprocessing.Pool(processes=processes)as pool:
dfs = pool.imap_unordered(correct_path, sub.groupby('path'))
dfs = tqdm(dfs)
 dfs = list(dfs)
 sub = pd.concat(dfs ).sort_values('site_path_timestamp' )<save_to_csv>
data_dir = '.. /input/'
train_path = os.path.join(data_dir, 'train.csv')
 test_path = os.path.join(data_dir, 'test.csv')
 train = pd.read_csv(train_path)
 test = pd.read_csv(test_path)
 print(len(train), len(test))
y_train = train['label']
x_train = train.drop(labels=['label'], axis=1)
 del train
x_train /= 255.
 test /= 255.
 x_train = x_train.values.reshape(-1, 28, 28, 1)
 test = test.values.reshape(-1, 28, 28, 1)
 y_train = to_categorical(y_train)
 random_seed = 2
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.1, random_state=random_seed)
 x_train, y_train
simple_accurate_99 = pd.read_csv(dfk.iloc[5, 2])
 sub['floor'] = simple_accurate_99['floor'].values
sub.to_csv('g7745cm99.csv', index=False)
 g7745cm99 = sub<feature_engineering>
model = models.Sequential()
model.add(layers.Conv2D(filters=32, kernel_size=(5, 5), padding='Same',
activation='relu', input_shape=(28, 28, 1)))
 model.add(layers.Conv2D(filters=32, kernel_size=(5, 5), padding='Same',
activation='relu'))
model.add(layers.MaxPool2D(pool_size=(2, 2)))
 model.add(layers.Dropout(0.25))
model.add(layers.Conv2D(filters=64, kernel_size=(3, 3), padding='Same',
activation='relu'))
model.add(layers.Conv2D(filters=64, kernel_size=(3, 3), padding='Same',
activation='relu'))
model.add(layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
 model.add(layers.Dense(256, activation="relu"))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, activation="softmax"))
optimizer = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-8, decay=0.0)
 model.compile(
 optimizer=optimizer,
loss=losses.categorical_crossentropy,
metrics=['acc'])
 model.summary()
sub = pd.read_csv(dfk.iloc[7, 2])
 tmp = sub['site_path_timestamp'].apply(lambda s : pd.Series(s.split('_')))
 sub['site'] = tmp[0]
sub['path'] = tmp[1]
sub['timestamp'] = tmp[2].astype(float)
 processes = multiprocessing.cpu_count()
with multiprocessing.Pool(processes=processes)as pool:
dfs = pool.imap_unordered(correct_path, sub.groupby('path'))
dfs = tqdm(dfs)
 dfs = list(dfs)
 sub = pd.concat(dfs ).sort_values('site_path_timestamp' )<save_to_csv>
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)
 learning_rate_reduction = ReduceLROnPlateau(
 monitor='val_acc', patience=3, verbose=1, factor=0.5, min_lr=1e-5)
 epochs = 10
batch_size = 86
history = model.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,
callbacks=[learning_rate_reduction])
 acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc)+ 1)
 plt.plot(epochs, acc, 'bo', label='Training acc')
 plt.plot(epochs, val_acc, 'b', label='Validation acc')
 plt.title("Training and validation accuracy")
 plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
 plt.plot(epochs, val_loss, 'b', label='Validation loss')
 plt.title("Training and validation loss")
 plt.legend()
<feature_engineering><EOS> 
results = model.predict(test)
 results = np.argmax(results, axis=1)
 results = pd.Series(results, name="Label")
 submission = pd.concat(
 [pd.Series(range(1, len(test)+ 1), name="ImageId"), results], axis=1)
 submission.to_csv("results.csv", index=False)