{"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":"This notebook is made for learning Convolutional Neural Network on the MNIST data.\n\nData:\n   * The data contains thousands of hand written images. The images are of pixel `28 * 28.`\n   * The `train` data has labels `(0-9)` column.\n\nGoal:\n   * Try to fit a CNN model to the dataset.\n   * Train for best accuracy.","metadata":{}},{"cell_type":"markdown","source":"# 1. Imports.","metadata":{}},{"cell_type":"code","source":"# Import libraries\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\nimport itertools\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras import layers, models\n\nfrom sklearn.metrics import confusion_matrix\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import RMSprop\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:04.778737Z","iopub.execute_input":"2022-12-27T14:41:04.779256Z","iopub.status.idle":"2022-12-27T14:41:04.798341Z","shell.execute_reply.started":"2022-12-27T14:41:04.779221Z","shell.execute_reply":"2022-12-27T14:41:04.796637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import datasets\n\ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nsample_submission = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:04.800325Z","iopub.execute_input":"2022-12-27T14:41:04.800718Z","iopub.status.idle":"2022-12-27T14:41:08.963723Z","shell.execute_reply.started":"2022-12-27T14:41:04.800685Z","shell.execute_reply":"2022-12-27T14:41:08.962480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Data processing.","metadata":{}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:08.966286Z","iopub.execute_input":"2022-12-27T14:41:08.967111Z","iopub.status.idle":"2022-12-27T14:41:08.996603Z","shell.execute_reply.started":"2022-12-27T14:41:08.967060Z","shell.execute_reply":"2022-12-27T14:41:08.995209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for col in train.columns:\n#     if train[col].dtype == \"int64\":\n#         print(f\"{col} : True\")\n#     else:\n#         print(f\"{col} : False\")","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:08.999746Z","iopub.execute_input":"2022-12-27T14:41:09.000223Z","iopub.status.idle":"2022-12-27T14:41:09.005823Z","shell.execute_reply.started":"2022-12-27T14:41:09.000184Z","shell.execute_reply":"2022-12-27T14:41:09.004321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"label\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.008044Z","iopub.execute_input":"2022-12-27T14:41:09.009377Z","iopub.status.idle":"2022-12-27T14:41:09.024298Z","shell.execute_reply.started":"2022-12-27T14:41:09.009304Z","shell.execute_reply":"2022-12-27T14:41:09.023168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"label\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.026072Z","iopub.execute_input":"2022-12-27T14:41:09.026850Z","iopub.status.idle":"2022-12-27T14:41:09.039387Z","shell.execute_reply.started":"2022-12-27T14:41:09.026801Z","shell.execute_reply":"2022-12-27T14:41:09.038343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(train[\"label\"]);","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.040858Z","iopub.execute_input":"2022-12-27T14:41:09.041340Z","iopub.status.idle":"2022-12-27T14:41:09.297241Z","shell.execute_reply.started":"2022-12-27T14:41:09.041295Z","shell.execute_reply":"2022-12-27T14:41:09.295991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The dataset is very clean and no preprocessing is required.\nWe can move foreward to model building.","metadata":{}},{"cell_type":"markdown","source":"# 3. Model building and Experimentation.","metadata":{}},{"cell_type":"code","source":"images = train.drop([\"label\"], axis=1).to_numpy()\nlabels = train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.298733Z","iopub.execute_input":"2022-12-27T14:41:09.299237Z","iopub.status.idle":"2022-12-27T14:41:09.436648Z","shell.execute_reply.started":"2022-12-27T14:41:09.299199Z","shell.execute_reply":"2022-12-27T14:41:09.434944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = images.reshape(-1,28,28,1)\nimages.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.438145Z","iopub.execute_input":"2022-12-27T14:41:09.438573Z","iopub.status.idle":"2022-12-27T14:41:09.452588Z","shell.execute_reply.started":"2022-12-27T14:41:09.438537Z","shell.execute_reply":"2022-12-27T14:41:09.451269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images[1]","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.454427Z","iopub.execute_input":"2022-12-27T14:41:09.455165Z","iopub.status.idle":"2022-12-27T14:41:09.464272Z","shell.execute_reply.started":"2022-12-27T14:41:09.455124Z","shell.execute_reply":"2022-12-27T14:41:09.463387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.get_dummies(labels)\nlabels = labels.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.469008Z","iopub.execute_input":"2022-12-27T14:41:09.470108Z","iopub.status.idle":"2022-12-27T14:41:09.484067Z","shell.execute_reply.started":"2022-12-27T14:41:09.470053Z","shell.execute_reply":"2022-12-27T14:41:09.482776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# labels[1]","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.485390Z","iopub.execute_input":"2022-12-27T14:41:09.486681Z","iopub.status.idle":"2022-12-27T14:41:09.496771Z","shell.execute_reply.started":"2022-12-27T14:41:09.486630Z","shell.execute_reply":"2022-12-27T14:41:09.495341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(images, train['label'], test_size=0.2, random_state=46)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.498425Z","iopub.execute_input":"2022-12-27T14:41:09.498964Z","iopub.status.idle":"2022-12-27T14:41:09.977537Z","shell.execute_reply.started":"2022-12-27T14:41:09.498893Z","shell.execute_reply":"2022-12-27T14:41:09.976453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"X_train shape: \", X_train.shape)\nprint(\"X_val shape: \", X_val.shape)\nprint(\"y_train shape: \", y_train.shape)\nprint(\"y_val shape: \", y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.978866Z","iopub.execute_input":"2022-12-27T14:41:09.979199Z","iopub.status.idle":"2022-12-27T14:41:09.985573Z","shell.execute_reply.started":"2022-12-27T14:41:09.979170Z","shell.execute_reply":"2022-12-27T14:41:09.984157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train.value_counts().sort_index())\nprint(y_val.value_counts().sort_index())","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:09.987339Z","iopub.execute_input":"2022-12-27T14:41:09.987858Z","iopub.status.idle":"2022-12-27T14:41:10.005471Z","shell.execute_reply.started":"2022-12-27T14:41:09.987813Z","shell.execute_reply":"2022-12-27T14:41:10.004137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = models.Sequential([\n    # CNN\n    layers.Convolution2D(filters=32, activation=\"relu\", input_shape = (28,28,1), kernel_size = (3,3)),\n    layers.MaxPooling2D((2,2)),\n\n    layers.Convolution2D(filters=32, activation=\"relu\", kernel_size = (3,3)),\n    layers.MaxPooling2D((2,2)),\n\n\n    # Dense\n    layers.Flatten(),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(10, activation='softmax')\n    \n])\n\n\ncnn.compile(optimizer=\"adam\",\n            loss=\"sparse_categorical_crossentropy\",\n            metrics=[\"accuracy\"])\n\n\ncnn.fit(X_train, y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:41:10.007098Z","iopub.execute_input":"2022-12-27T14:41:10.007715Z","iopub.status.idle":"2022-12-27T14:43:03.650994Z","shell.execute_reply.started":"2022-12-27T14:41:10.007658Z","shell.execute_reply":"2022-12-27T14:43:03.649340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.evaluate(X_val, y_val)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:43:03.652493Z","iopub.execute_input":"2022-12-27T14:43:03.652871Z","iopub.status.idle":"2022-12-27T14:43:05.132556Z","shell.execute_reply.started":"2022-12-27T14:43:03.652837Z","shell.execute_reply":"2022-12-27T14:43:05.131062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = cnn.predict(X_val)\ny_pred[:5]","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:43:05.134658Z","iopub.execute_input":"2022-12-27T14:43:05.135152Z","iopub.status.idle":"2022-12-27T14:43:06.319793Z","shell.execute_reply.started":"2022-12-27T14:43:05.135104Z","shell.execute_reply":"2022-12-27T14:43:06.318108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_classes = [np.argmax(element) for element in y_pred]\ny_classes[:5]","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:43:06.321964Z","iopub.execute_input":"2022-12-27T14:43:06.322524Z","iopub.status.idle":"2022-12-27T14:43:06.350560Z","shell.execute_reply.started":"2022-12-27T14:43:06.322475Z","shell.execute_reply":"2022-12-27T14:43:06.349307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_classes)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:43:06.352303Z","iopub.execute_input":"2022-12-27T14:43:06.353411Z","iopub.status.idle":"2022-12-27T14:43:06.361938Z","shell.execute_reply.started":"2022-12-27T14:43:06.353367Z","shell.execute_reply":"2022-12-27T14:43:06.360419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val[:5]","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:43:06.364532Z","iopub.execute_input":"2022-12-27T14:43:06.365754Z","iopub.status.idle":"2022-12-27T14:43:06.378552Z","shell.execute_reply.started":"2022-12-27T14:43:06.365699Z","shell.execute_reply":"2022-12-27T14:43:06.377562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\nprint('Accuracy: ', accuracy_score(y_val, np.argmax(cnn.predict(X_val), axis=1)))\nprint('Precision: ', precision_score(y_val, np.argmax(cnn.predict(X_val), axis=1), average='macro'))\nprint('Recall: ', recall_score(y_val, np.argmax(cnn.predict(X_val), axis=1), average='macro'))\nprint('F1 Score: ', f1_score(y_val, np.argmax(cnn.predict(X_val), axis=1), average='macro'))","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:43:06.380046Z","iopub.execute_input":"2022-12-27T14:43:06.380649Z","iopub.status.idle":"2022-12-27T14:43:10.632075Z","shell.execute_reply.started":"2022-12-27T14:43:06.380613Z","shell.execute_reply":"2022-12-27T14:43:10.631046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': np.arange(1, 8401), 'Label': y_classes})\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:43:10.633591Z","iopub.execute_input":"2022-12-27T14:43:10.634213Z","iopub.status.idle":"2022-12-27T14:43:10.652303Z","shell.execute_reply.started":"2022-12-27T14:43:10.634178Z","shell.execute_reply":"2022-12-27T14:43:10.651449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:43:10.653609Z","iopub.execute_input":"2022-12-27T14:43:10.654613Z","iopub.status.idle":"2022-12-27T14:43:10.667174Z","shell.execute_reply.started":"2022-12-27T14:43:10.654573Z","shell.execute_reply":"2022-12-27T14:43:10.665726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Yet to work on...","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:43:10.668830Z","iopub.execute_input":"2022-12-27T14:43:10.670026Z","iopub.status.idle":"2022-12-27T14:43:10.678524Z","shell.execute_reply.started":"2022-12-27T14:43:10.669987Z","shell.execute_reply":"2022-12-27T14:43:10.677517Z"},"trusted":true},"execution_count":null,"outputs":[]}]}