{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np \nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\n%matplotlib inline\n\nnp.random.seed(2)\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flatten\nfrom keras.optimizers import RMSprop\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.preprocessing.image import ImageDataGenerator\n\nsns.set(style='white', context='notebook', palette='deep')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-01T02:53:19.597703Z","iopub.execute_input":"2021-09-01T02:53:19.598154Z","iopub.status.idle":"2021-09-01T02:53:19.618976Z","shell.execute_reply.started":"2021-09-01T02:53:19.598113Z","shell.execute_reply":"2021-09-01T02:53:19.617705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:19.624752Z","iopub.execute_input":"2021-09-01T02:53:19.626531Z","iopub.status.idle":"2021-09-01T02:53:23.406284Z","shell.execute_reply.started":"2021-09-01T02:53:19.626497Z","shell.execute_reply":"2021-09-01T02:53:23.405416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.408337Z","iopub.execute_input":"2021-09-01T02:53:23.408712Z","iopub.status.idle":"2021-09-01T02:53:23.425838Z","shell.execute_reply.started":"2021-09-01T02:53:23.408674Z","shell.execute_reply":"2021-09-01T02:53:23.424977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train['label']\nX_train = train.drop(labels=['label'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.427426Z","iopub.execute_input":"2021-09-01T02:53:23.427949Z","iopub.status.idle":"2021-09-01T02:53:23.513804Z","shell.execute_reply.started":"2021-09-01T02:53:23.427906Z","shell.execute_reply":"2021-09-01T02:53:23.512804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del train\ng = sns.countplot(y_train)\ny_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.515173Z","iopub.execute_input":"2021-09-01T02:53:23.515754Z","iopub.status.idle":"2021-09-01T02:53:23.735257Z","shell.execute_reply.started":"2021-09-01T02:53:23.515716Z","shell.execute_reply":"2021-09-01T02:53:23.734493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.737482Z","iopub.execute_input":"2021-09-01T02:53:23.737821Z","iopub.status.idle":"2021-09-01T02:53:23.757288Z","shell.execute_reply.started":"2021-09-01T02:53:23.737786Z","shell.execute_reply":"2021-09-01T02:53:23.756362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.758503Z","iopub.execute_input":"2021-09-01T02:53:23.758862Z","iopub.status.idle":"2021-09-01T02:53:23.775834Z","shell.execute_reply.started":"2021-09-01T02:53:23.758827Z","shell.execute_reply":"2021-09-01T02:53:23.774872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train /255.0\ntest = test/255.0","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.778463Z","iopub.execute_input":"2021-09-01T02:53:23.778798Z","iopub.status.idle":"2021-09-01T02:53:23.938155Z","shell.execute_reply.started":"2021-09-01T02:53:23.778763Z","shell.execute_reply":"2021-09-01T02:53:23.937256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reshape\nX_train = X_train.values.reshape(-1, 28, 28, 1)\ntest= test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.940431Z","iopub.execute_input":"2021-09-01T02:53:23.940702Z","iopub.status.idle":"2021-09-01T02:53:23.945856Z","shell.execute_reply.started":"2021-09-01T02:53:23.940675Z","shell.execute_reply":"2021-09-01T02:53:23.945076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.947141Z","iopub.execute_input":"2021-09-01T02:53:23.947685Z","iopub.status.idle":"2021-09-01T02:53:23.963446Z","shell.execute_reply.started":"2021-09-01T02:53:23.947634Z","shell.execute_reply":"2021-09-01T02:53:23.962690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_seed = 2","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.964956Z","iopub.execute_input":"2021-09-01T02:53:23.965349Z","iopub.status.idle":"2021-09-01T02:53:23.970551Z","shell.execute_reply.started":"2021-09-01T02:53:23.965313Z","shell.execute_reply":"2021-09-01T02:53:23.969711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size= 0.1)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:23.973222Z","iopub.execute_input":"2021-09-01T02:53:23.973497Z","iopub.status.idle":"2021-09-01T02:53:24.344404Z","shell.execute_reply.started":"2021-09-01T02:53:23.973463Z","shell.execute_reply":"2021-09-01T02:53:24.343523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = plt.imshow(X_train[0][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:24.345757Z","iopub.execute_input":"2021-09-01T02:53:24.346136Z","iopub.status.idle":"2021-09-01T02:53:24.806569Z","shell.execute_reply.started":"2021-09-01T02:53:24.346096Z","shell.execute_reply":"2021-09-01T02:53:24.805591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, kernel_size=(3, 3), padding='Same', activation='relu', input_shape=(28,28,1)))\nmodel.add(Conv2D(32, kernel_size=(3, 3), padding='Same', activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(64, kernel_size=(3, 3), padding='Same', activation='relu'))\nmodel.add(Conv2D(64, kernel_size=(3, 3), padding='Same', activation='relu'))\nmodel.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))\n# model.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:58:40.804298Z","iopub.execute_input":"2021-09-01T02:58:40.804631Z","iopub.status.idle":"2021-09-01T02:58:40.873796Z","shell.execute_reply.started":"2021-09-01T02:58:40.804599Z","shell.execute_reply":"2021-09-01T02:58:40.873030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = RMSprop(learning_rate=0.001, rho= 0.9, epsilon=1e-08, decay=0.0)\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1, factor=0.5, min_lr=0.000001)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:54:37.511945Z","iopub.execute_input":"2021-09-01T02:54:37.512259Z","iopub.status.idle":"2021-09-01T02:54:37.525848Z","shell.execute_reply.started":"2021-09-01T02:54:37.512230Z","shell.execute_reply":"2021-09-01T02:54:37.524786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 15\nbatch_size = 86","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:54:38.477777Z","iopub.execute_input":"2021-09-01T02:54:38.478110Z","iopub.status.idle":"2021-09-01T02:54:38.483680Z","shell.execute_reply.started":"2021-09-01T02:54:38.478080Z","shell.execute_reply":"2021-09-01T02:54:38.481032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    featurewise_center=False,\n    samplewise_center=False,\n    featurewise_std_normalization=False,\n    samplewise_std_normalization=False,\n    zca_epsilon=False,\n    rotation_range=10,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=False,\n    vertical_flip=False\n)\n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:54:39.534830Z","iopub.execute_input":"2021-09-01T02:54:39.535137Z","iopub.status.idle":"2021-09-01T02:54:39.617584Z","shell.execute_reply.started":"2021-09-01T02:54:39.535110Z","shell.execute_reply":"2021-09-01T02:54:39.616736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(datagen.flow(X_train, y_train, batch_size=batch_size),\n                             epochs=epochs, validation_data=(X_test, y_test),\n                             verbose=2, steps_per_epoch=X_train.shape[0]//batch_size,\n                             callbacks=[learning_rate_reduction])","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:54:41.491896Z","iopub.execute_input":"2021-09-01T02:54:41.494238Z","iopub.status.idle":"2021-09-01T02:57:15.240689Z","shell.execute_reply.started":"2021-09-01T02:54:41.494194Z","shell.execute_reply":"2021-09-01T02:57:15.239924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test)\n\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:24.826786Z","iopub.status.idle":"2021-09-01T02:53:24.827416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-01T02:53:24.828637Z","iopub.status.idle":"2021-09-01T02:53:24.829311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}