{"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":"markdown","source":"# Digit recognition with CNN <a class=\"anchor\" id=\"bullet-0\"></a>","metadata":{}},{"cell_type":"markdown","source":"## Table of contents:\n1. [Imports](#bullet-1)\n2. [Data processing](#bullet-2)\n    * [Load Data](#bullet-2.1)\n    * [Reshape and Normalization](#bullet-2.2)\n    * [Split Data](#bullet-2.3)\n3. [CNN](#bullet-3)\n    * [Model](#bullet-3.1)\n    * [Plot Loss and Accuracy](#bullet-3.2)\n    * [Plot Confusion Matrix](#bullet-3.3)\n4. [Predictions](#bullet-4)","metadata":{}},{"cell_type":"markdown","source":"# Imports <a class=\"anchor\" id=\"bullet-1\"></a>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\nimport os\n\nfrom sklearn.model_selection import train_test_split\n\nfrom keras.models import Sequential \nfrom keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten, Dropout\nfrom keras.utils import np_utils","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:50:52.393238Z","iopub.execute_input":"2022-03-05T23:50:52.393516Z","iopub.status.idle":"2022-03-05T23:50:52.398370Z","shell.execute_reply.started":"2022-03-05T23:50:52.393482Z","shell.execute_reply":"2022-03-05T23:50:52.397595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data processing <a class=\"anchor\" id=\"bullet-2\"></a>\n[Back to top](#bullet-0)","metadata":{}},{"cell_type":"markdown","source":"## Load data <a class=\"anchor\" id=\"bullet-2.1\"></a>","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv('../input/digit-recognizer/train.csv')\nvalidation = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:51:49.527092Z","iopub.execute_input":"2022-03-05T23:51:49.527347Z","iopub.status.idle":"2022-03-05T23:51:54.142786Z","shell.execute_reply.started":"2022-03-05T23:51:49.527320Z","shell.execute_reply":"2022-03-05T23:51:54.141930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:51:57.404081Z","iopub.execute_input":"2022-03-05T23:51:57.404371Z","iopub.status.idle":"2022-03-05T23:51:57.435616Z","shell.execute_reply.started":"2022-03-05T23:51:57.404338Z","shell.execute_reply":"2022-03-05T23:51:57.434871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# y_train_and_test = data['label']\n# X_train_and_test = data.drop(labels = [\"label\"], axis = 1) \n\ny_train_and_test = data.pop('label')\nX_train_and_test = data","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:52:00.462815Z","iopub.execute_input":"2022-03-05T23:52:00.463444Z","iopub.status.idle":"2022-03-05T23:52:00.473614Z","shell.execute_reply.started":"2022-03-05T23:52:00.463398Z","shell.execute_reply":"2022-03-05T23:52:00.472639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reshape and Normalization <a class=\"anchor\" id=\"bullet-2.2\"></a>","metadata":{}},{"cell_type":"code","source":"# Reshape image in 3 dimensions (height = 28px, width = 28px , canal = 1)\nX_train_and_test = X_train_and_test.values.reshape(-1,28,28,1)\nvalidation = validation.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:52:03.875507Z","iopub.execute_input":"2022-03-05T23:52:03.875825Z","iopub.status.idle":"2022-03-05T23:52:04.097861Z","shell.execute_reply.started":"2022-03-05T23:52:03.875788Z","shell.execute_reply":"2022-03-05T23:52:04.096979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_and_test = X_train_and_test / 255.0\nvalidation = validation / 255.0","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:52:06.085347Z","iopub.execute_input":"2022-03-05T23:52:06.085674Z","iopub.status.idle":"2022-03-05T23:52:06.277793Z","shell.execute_reply.started":"2022-03-05T23:52:06.085640Z","shell.execute_reply":"2022-03-05T23:52:06.276862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# one hot encoding\n# y_train = to_categorical(y_train, num_classes = 10)\n# or\n# y_train_encoded=np.eye(y_train.max()+1)[y_train]\n# y_test_encoded=np.eye(y_test.max()+1)[y_test]\n# not needed if we use sparse_categorical_crossentropy instead of categorical_crossentropy","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:52:08.442397Z","iopub.execute_input":"2022-03-05T23:52:08.443212Z","iopub.status.idle":"2022-03-05T23:52:08.447154Z","shell.execute_reply.started":"2022-03-05T23:52:08.443167Z","shell.execute_reply":"2022-03-05T23:52:08.446288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split Data <a class=\"anchor\" id=\"bullet-2.3\"></a>","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X_train_and_test, y_train_and_test, test_size = 0.1, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:52:10.057813Z","iopub.execute_input":"2022-03-05T23:52:10.058071Z","iopub.status.idle":"2022-03-05T23:52:10.468444Z","shell.execute_reply.started":"2022-03-05T23:52:10.058044Z","shell.execute_reply":"2022-03-05T23:52:10.467581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print example digit from train data set\nimage = X_train[0][:,:,0]\nplt.imshow(image, cmap='gray_r')\nplt.title(f\"Digit: {y_train.iloc[0]}\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:52:12.077903Z","iopub.execute_input":"2022-03-05T23:52:12.078186Z","iopub.status.idle":"2022-03-05T23:52:12.267322Z","shell.execute_reply.started":"2022-03-05T23:52:12.078153Z","shell.execute_reply":"2022-03-05T23:52:12.266471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN <a class=\"anchor\" id=\"bullet-3\"></a>\n[Back to top](#zero-bullet)","metadata":{}},{"cell_type":"markdown","source":"## Model <a class=\"anchor\" id=\"bullet-3.1\"></a>","metadata":{}},{"cell_type":"code","source":"np.random.seed(0)\ninput_size = 784\nbatch_size = 100\nhidden_size = 100\nhidden_neurons = 200\nclasses = 10\nepochs = 8","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:52:26.512595Z","iopub.execute_input":"2022-03-05T23:52:26.513433Z","iopub.status.idle":"2022-03-05T23:52:26.517892Z","shell.execute_reply.started":"2022-03-05T23:52:26.513393Z","shell.execute_reply":"2022-03-05T23:52:26.517086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define the model\nmodel = Sequential()\nmodel.add(Convolution2D(32, (3,3), input_shape=(28,28,1)))\nmodel.add(Activation('relu'))\nmodel.add(Convolution2D(32, (3,3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(hidden_size))\nmodel.add(Activation('relu'))\nmodel.add(Dense(classes))\nmodel.add(Activation('softmax'))\n\nmodel.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'], optimizer='adam')","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:52:27.950865Z","iopub.execute_input":"2022-03-05T23:52:27.951494Z","iopub.status.idle":"2022-03-05T23:52:28.112506Z","shell.execute_reply.started":"2022-03-05T23:52:27.951449Z","shell.execute_reply":"2022-03-05T23:52:28.111592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback_history = model.fit(X_train, y_train, validation_data=(X_test, y_test), batch_size=batch_size, epochs=epochs)\nscore = model.evaluate(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:52:39.693647Z","iopub.execute_input":"2022-03-05T23:52:39.694605Z","iopub.status.idle":"2022-03-05T23:55:04.108630Z","shell.execute_reply.started":"2022-03-05T23:52:39.694549Z","shell.execute_reply":"2022-03-05T23:55:04.107801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Dokładność treningu:', score[1])\nscore = model.evaluate(X_test, y_test)\nprint('Dokładność testu:', score[1])","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:55:33.927505Z","iopub.execute_input":"2022-03-05T23:55:33.927960Z","iopub.status.idle":"2022-03-05T23:55:34.789486Z","shell.execute_reply.started":"2022-03-05T23:55:33.927927Z","shell.execute_reply":"2022-03-05T23:55:34.788623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback_history.history['accuracy'][-1], callback_history.history['val_accuracy'][-1]","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:55:37.547856Z","iopub.execute_input":"2022-03-05T23:55:37.548831Z","iopub.status.idle":"2022-03-05T23:55:37.554918Z","shell.execute_reply.started":"2022-03-05T23:55:37.548775Z","shell.execute_reply":"2022-03-05T23:55:37.554246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot Loss and Accuracy <a class=\"anchor\" id=\"bullet-3.2\"></a>","metadata":{}},{"cell_type":"code","source":"pd.DataFrame(callback_history.history)[['loss', 'val_loss']].plot()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:55:43.585643Z","iopub.execute_input":"2022-03-05T23:55:43.586431Z","iopub.status.idle":"2022-03-05T23:55:43.825343Z","shell.execute_reply.started":"2022-03-05T23:55:43.586395Z","shell.execute_reply":"2022-03-05T23:55:43.824356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(callback_history.history)[['accuracy', 'val_accuracy']].plot()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:55:45.886032Z","iopub.execute_input":"2022-03-05T23:55:45.886318Z","iopub.status.idle":"2022-03-05T23:55:46.125929Z","shell.execute_reply.started":"2022-03-05T23:55:45.886286Z","shell.execute_reply":"2022-03-05T23:55:46.125271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot Confusion Matrix <a class=\"anchor\" id=\"bullet-3.3\"></a>","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix\n\ny_test_pred = model.predict(X_test)\n\ncm = confusion_matrix(\n    y_true=y_test, y_pred=y_test_pred.argmax(axis=1)\n)\ndisp = ConfusionMatrixDisplay(cm)\nfig, ax = plt.subplots(figsize=(10, 10))\ndisp.plot(ax=ax)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:55:51.038585Z","iopub.execute_input":"2022-03-05T23:55:51.039111Z","iopub.status.idle":"2022-03-05T23:55:52.598166Z","shell.execute_reply.started":"2022-03-05T23:55:51.039076Z","shell.execute_reply":"2022-03-05T23:55:52.597116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions <a class=\"anchor\" id=\"bullet-4\"></a>\n[Back to top](#bullet-0)","metadata":{}},{"cell_type":"code","source":"# predict results\nresults = model.predict(validation)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:55:56.396287Z","iopub.execute_input":"2022-03-05T23:55:56.396576Z","iopub.status.idle":"2022-03-05T23:56:01.204487Z","shell.execute_reply.started":"2022-03-05T23:55:56.396535Z","shell.execute_reply":"2022-03-05T23:56:01.203506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(results)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:56:01.206341Z","iopub.execute_input":"2022-03-05T23:56:01.206672Z","iopub.status.idle":"2022-03-05T23:56:01.213342Z","shell.execute_reply.started":"2022-03-05T23:56:01.206619Z","shell.execute_reply":"2022-03-05T23:56:01.212544Z"},"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('./submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-05T23:56:54.663807Z","iopub.execute_input":"2022-03-05T23:56:54.664607Z","iopub.status.idle":"2022-03-05T23:56:54.702553Z","shell.execute_reply.started":"2022-03-05T23:56:54.664560Z","shell.execute_reply":"2022-03-05T23:56:54.701603Z"},"trusted":true},"execution_count":null,"outputs":[]}]}