# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5" _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19"
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

import tensorflow as tf
import keras
from keras import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from keras.utils.np_utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.callbacks import ReduceLROnPlateau

pd.options.display.max_rows = 10
# %matplotlib inline

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

import os
print(os.listdir("../input"))

# Any results you write to the current directory are saved as output.

# + _cell_guid="79c7e3d0-c299-4dcb-8224-4455121ee9b0" _uuid="d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
train = pd.read_csv('../input/train.csv')
print('train.shape', train.shape)

test = pd.read_csv('../input/test.csv')
print('test.shape',test.shape)

# + _uuid="06ea297a9077df663a01bce78d7bf0f5d598570d"
y_train = train.iloc[:, 0]
X_train = train.iloc[:, 1:]

X_test = test

del train, test

# + _uuid="0462ee1473d6a669cb42fa18e2837d1da9946888"
X_train.shape, y_train.shape, X_test.shape

# + _uuid="79d26a57e3e0f9db200e0e8b788bf55043ee99cb"
X_train = (X_train/255.0).values.reshape(42000, 28, 28, 1)
X_test = (X_test/255.0).values.reshape(28000, 28, 28, 1)
y_train = to_categorical(y_train, num_classes=10)

X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1)

X_train.shape, y_train.shape, X_val.shape, y_val.shape, X_test.shape

# + _uuid="f826e64cb32821b09c7bc106be3b3551b28e0ac2"
plt.imshow(X_train[11].reshape(28, 28))
plt.title(np.argmax(y_train[11]))
plt.axis('off')

# + _uuid="aaf76323c069b980baa09a4b9d78409bcf0f0595"
plt.imshow(X_val[0].reshape(28, 28))
plt.title(np.argmax(y_val[0]))
plt.axis('off')

# + _uuid="d7e72418862b15998015dfef742831f9efccdd0b"
model = Sequential()

model.add(Conv2D(filters=32, kernel_size=(3, 3), input_shape=(28, 28, 1), activation = 'relu'))
model.add(Conv2D(filters=32, kernel_size=(3, 3), input_shape=(28, 28, 1), activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(filters=64, kernel_size=(3, 3), activation = 'relu', padding = 'Same'))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation = 'relu', padding = 'Same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(filters=128, kernel_size=(3, 3), activation = 'relu'))

model.add(Flatten())
model.add(Dense(units = 128, activation = 'relu'))
model.add(BatchNormalization())
model.add(Dropout(0.2))
model.add(Dense(units = 10, activation = 'softmax'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
model.summary()

# + _uuid="712280a9e70d8465c5e9a5e83b86a3d667cc1c85"
data_augmentation = 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
)

data_augmentation.fit(X_train)

# + _uuid="e66c116aa06c680cbda7813b565f9e69d367a547"
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', 
                                            patience=3, 
                                            verbose=1, 
                                            factor=0.5, 
                                            min_lr=0.00001)

# + _uuid="7aa72a30f1f7b7b703caea7f978927ca1856909b"
X_train.shape, y_train.shape, X_val.shape, y_val.shape, X_test.shape

# + _uuid="bbe9094b3514c9f1d52ce515cff686fc2adadaa6"
batch_size = 128
epochs = 2

# + _uuid="cee3d0da4eaf924d95edb0d020f80c0750ca4897"
hist = model.fit_generator(data_augmentation.flow(X_train, y_train, batch_size=batch_size),
                          epochs = epochs, validation_data = (X_val, y_val),
                          steps_per_epoch=X_train.shape[0], callbacks=[learning_rate_reduction])

# + _uuid="5cf7be37cb4d17aa2f44a2a495987977f680db86"
hist = pd.DataFrame(hist.history)

# + _uuid="4b52ece82dafdd90b469890d078913cf49f82759"
plt.figure(figsize=(10,2))

plt.subplot(1, 2, 1)
plt.plot(hist['acc'])
plt.plot(hist['val_acc'])
plt.title('acc')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(hist['loss'])
plt.plot(hist['val_loss'])
plt.title('loss')
plt.legend()

plt.show()

# + _uuid="729d84efb8adc4747b91fda81c881d818b8d8e25"
predict_y = model.predict(X_test)

# + _uuid="0d7f771cd138a80cee42c444ea100e2f4e8610b5"
predict_y = np.apply_along_axis(np.argmax, 1, predict_y)
predict_y[:10]

# + _uuid="c0d742f63c2d1202f511000a4a46f325e9e68134"
plt.figure(figsize=(20, 6))

n = 10
for i in range(n):
  plt.subplot(1, n, i+1)
  plt.imshow(X_test[i].reshape(28, 28))
  plt.title(np.argmax(predict_y[i]))
  plt.axis('off')

# + _uuid="2290fb8a7abef84a0f995dc96162d9e76e1a799f"


# + _uuid="9d5598ba66df365c423e47a32c7ea9ae52159588"
output = pd.DataFrame()
output['ImageId'] = np.arange(1, 28000+1)
output['Label'] = predict_y

output.head()

# + _uuid="1b85908ff84e81417e4f6723f4354f62aa820673"
output.to_csv('submission.csv', index=False)

# + _uuid="8ce19ffe2fd700b15db76bc48bb20192710298ff"


StarcoderdataJupyterScriptsDedupFiltered