{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        \n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-28T19:38:06.697381Z","iopub.execute_input":"2022-10-28T19:38:06.697852Z","iopub.status.idle":"2022-10-28T19:38:06.709240Z","shell.execute_reply.started":"2022-10-28T19:38:06.697815Z","shell.execute_reply":"2022-10-28T19:38:06.708263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras as keras\nfrom tensorflow.keras import layers\nkeras.backend.set_image_data_format('channels_last')","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:07.179879Z","iopub.execute_input":"2022-10-28T19:38:07.180493Z","iopub.status.idle":"2022-10-28T19:38:07.185575Z","shell.execute_reply.started":"2022-10-28T19:38:07.180454Z","shell.execute_reply":"2022-10-28T19:38:07.184621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:07.579777Z","iopub.execute_input":"2022-10-28T19:38:07.580063Z","iopub.status.idle":"2022-10-28T19:38:07.584988Z","shell.execute_reply.started":"2022-10-28T19:38:07.580037Z","shell.execute_reply":"2022-10-28T19:38:07.583834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:07.988788Z","iopub.execute_input":"2022-10-28T19:38:07.989095Z","iopub.status.idle":"2022-10-28T19:38:10.959368Z","shell.execute_reply.started":"2022-10-28T19:38:07.989068Z","shell.execute_reply":"2022-10-28T19:38:10.958388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:10.962005Z","iopub.execute_input":"2022-10-28T19:38:10.963805Z","iopub.status.idle":"2022-10-28T19:38:10.981522Z","shell.execute_reply.started":"2022-10-28T19:38:10.963764Z","shell.execute_reply":"2022-10-28T19:38:10.980520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:10.983129Z","iopub.execute_input":"2022-10-28T19:38:10.983643Z","iopub.status.idle":"2022-10-28T19:38:11.001662Z","shell.execute_reply.started":"2022-10-28T19:38:10.983609Z","shell.execute_reply":"2022-10-28T19:38:11.000332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 28*28\nimage_size","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:11.004602Z","iopub.execute_input":"2022-10-28T19:38:11.004990Z","iopub.status.idle":"2022-10-28T19:38:11.012690Z","shell.execute_reply.started":"2022-10-28T19:38:11.004956Z","shell.execute_reply":"2022-10-28T19:38:11.011679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.drop(\"label\", axis = 1).copy()\nX_test = test.copy()\nY_train = train[\"label\"].copy()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:11.013852Z","iopub.execute_input":"2022-10-28T19:38:11.014438Z","iopub.status.idle":"2022-10-28T19:38:11.250864Z","shell.execute_reply.started":"2022-10-28T19:38:11.014404Z","shell.execute_reply":"2022-10-28T19:38:11.249896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:11.252207Z","iopub.execute_input":"2022-10-28T19:38:11.252780Z","iopub.status.idle":"2022-10-28T19:38:13.293785Z","shell.execute_reply.started":"2022-10-28T19:38:11.252743Z","shell.execute_reply":"2022-10-28T19:38:13.292695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalize values\nX_train = X_train/255.0\nX_test = X_test/255.0","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:13.296448Z","iopub.execute_input":"2022-10-28T19:38:13.296848Z","iopub.status.idle":"2022-10-28T19:38:13.470244Z","shell.execute_reply.started":"2022-10-28T19:38:13.296812Z","shell.execute_reply":"2022-10-28T19:38:13.469063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reshape to 28 * 28 so that we can see the images\nX_train = X_train.values.reshape(-1, 28, 28, 1)\nX_test = X_test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:38:13.471959Z","iopub.execute_input":"2022-10-28T19:38:13.472321Z","iopub.status.idle":"2022-10-28T19:38:13.478497Z","shell.execute_reply.started":"2022-10-28T19:38:13.472283Z","shell.execute_reply":"2022-10-28T19:38:13.477625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nno_images=len(X_train)\n\n# Display random Image\nfig, ax = plt.subplots(figsize=(10, 10))\n\nplt.imshow(X_train[random.randint(0,no_images), :, :, 0], cmap='Greys', interpolation='nearest') \n\n# replace random.randint(0,no_images) in code above with a number if you want to see specific image. \n#This dispalys a random image each time\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:40:39.573453Z","iopub.execute_input":"2022-10-28T19:40:39.574468Z","iopub.status.idle":"2022-10-28T19:40:39.808980Z","shell.execute_reply.started":"2022-10-28T19:40:39.574415Z","shell.execute_reply":"2022-10-28T19:40:39.808092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(2,2))\n\nplt.imshow(X_train[random.randint(0, no_images), :, :, 0], cmap = \"Greys\", interpolation = \"nearest\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:44:30.564598Z","iopub.execute_input":"2022-10-28T19:44:30.565006Z","iopub.status.idle":"2022-10-28T19:44:30.709701Z","shell.execute_reply.started":"2022-10-28T19:44:30.564974Z","shell.execute_reply":"2022-10-28T19:44:30.708494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_size = 0.2\n\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = validation_size)","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:47:01.317222Z","iopub.execute_input":"2022-10-28T19:47:01.317578Z","iopub.status.idle":"2022-10-28T19:47:01.392233Z","shell.execute_reply.started":"2022-10-28T19:47:01.317532Z","shell.execute_reply":"2022-10-28T19:47:01.391154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get one-hot encoding\nY_train = keras.utils.to_categorical(Y_train, num_classes = 10)\nY_val = keras.utils.to_categorical(Y_val, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:49:10.378056Z","iopub.execute_input":"2022-10-28T19:49:10.378424Z","iopub.status.idle":"2022-10-28T19:49:10.385917Z","shell.execute_reply.started":"2022-10-28T19:49:10.378393Z","shell.execute_reply":"2022-10-28T19:49:10.384910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def CNN():\n    model = keras.Sequential()\n    # CONV > CONV > BN > RELU > MAXPOOLING > DROPOUT\n    model.add(layers.Conv2D(32, (3, 3), (1, 1), padding='valid', input_shape=(28, 28, 1), name='conv2d_1_1'))\n    model.add(layers.Conv2D(32, (3, 3), (1, 1), padding='same', name='conv2d_1_2'))\n    model.add(layers.BatchNormalization(name='bn_1'))\n    model.add(layers.Activation('relu', name='relu_1'))\n    model.add(layers.MaxPooling2D((2, 2), (2, 2), padding='valid', name='mp2d_1'))\n    model.add(layers.Dropout(0.2, name='drop_1'))\n    # CONV > CONV > BN > RELU > MAXPOOLING > DROPOUT\n    model.add(layers.Conv2D(64, (3, 3), (1, 1), padding='valid', name='conv2d_2_1'))\n    model.add(layers.Conv2D(64, (3, 3), (1, 1), padding='same', name='conv2d_2_2'))\n    model.add(layers.BatchNormalization(name='bn_2'))\n    model.add(layers.Activation('relu', name='relu_2'))\n    model.add(layers.MaxPooling2D((2, 2), (2, 2), padding='valid', name='mp2d_2'))\n    model.add(layers.Dropout(0.2, name='drop_2'))\n    # FLATTEN > DENSE > CLASSIFICATION\n    model.add(layers.Flatten())\n    model.add(layers.Dense(100, activation='relu'))\n    model.add(layers.Dense(10, activation='softmax'))\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:56:07.219725Z","iopub.execute_input":"2022-10-28T19:56:07.220100Z","iopub.status.idle":"2022-10-28T19:56:07.230873Z","shell.execute_reply.started":"2022-10-28T19:56:07.220067Z","shell.execute_reply":"2022-10-28T19:56:07.229581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CNN()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:57:04.804550Z","iopub.execute_input":"2022-10-28T19:57:04.804986Z","iopub.status.idle":"2022-10-28T19:57:07.652979Z","shell.execute_reply.started":"2022-10-28T19:57:04.804951Z","shell.execute_reply":"2022-10-28T19:57:07.651439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = \"Adam\", loss = \"CategoricalCrossentropy\", metrics = \"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:58:13.886237Z","iopub.execute_input":"2022-10-28T19:58:13.890293Z","iopub.status.idle":"2022-10-28T19:58:13.912564Z","shell.execute_reply.started":"2022-10-28T19:58:13.890259Z","shell.execute_reply":"2022-10-28T19:58:13.911618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T19:58:35.069702Z","iopub.execute_input":"2022-10-28T19:58:35.070057Z","iopub.status.idle":"2022-10-28T19:58:35.077140Z","shell.execute_reply.started":"2022-10-28T19:58:35.070029Z","shell.execute_reply":"2022-10-28T19:58:35.076025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training = model.fit(X_train, Y_train, validation_data = (X_val, Y_val), batch_size = 64, epochs = 50, verbose = 1 )","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:00:35.677242Z","iopub.execute_input":"2022-10-28T20:00:35.677607Z","iopub.status.idle":"2022-10-28T20:02:58.625891Z","shell.execute_reply.started":"2022-10-28T20:00:35.677572Z","shell.execute_reply":"2022-10-28T20:02:58.624907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(model, X, imgs):\n    s = int(np.sqrt(imgs))\n    fig, ax = plt.subplots(s, s, sharex=True, sharey=True, figsize=(15, 15))\n    ax = ax.flatten()\n    preds = model.predict(X[:imgs])\n    for i in range(imgs):\n        y_pred = np.argmax(preds[i])\n        img = X[i].reshape(28, 28)\n        ax[i].imshow(img, cmap='Greys', interpolation='nearest')\n        ax[i].set_title(f'p: {y_pred}')","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:13:37.605675Z","iopub.execute_input":"2022-10-28T20:13:37.606373Z","iopub.status.idle":"2022-10-28T20:13:37.617249Z","shell.execute_reply.started":"2022-10-28T20:13:37.606328Z","shell.execute_reply":"2022-10-28T20:13:37.615486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict(model, X_test, 25)","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:14:04.263465Z","iopub.execute_input":"2022-10-28T20:14:04.264491Z","iopub.status.idle":"2022-10-28T20:14:06.730703Z","shell.execute_reply.started":"2022-10-28T20:14:04.264443Z","shell.execute_reply":"2022-10-28T20:14:06.729609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)\ny_pred = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:14:23.223151Z","iopub.execute_input":"2022-10-28T20:14:23.223529Z","iopub.status.idle":"2022-10-28T20:14:26.098582Z","shell.execute_reply.started":"2022-10-28T20:14:23.223492Z","shell.execute_reply":"2022-10-28T20:14:26.097582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_name = \"submission.csv\"\ny_pred = pd.Series(y_pred, name='Label')\nsub = pd.concat([pd.Series(range(1, 28001), name=\"ImageId\"), y_pred], axis=1)\nsub.to_csv(file_name, index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:15:15.823113Z","iopub.execute_input":"2022-10-28T20:15:15.823467Z","iopub.status.idle":"2022-10-28T20:15:15.857748Z","shell.execute_reply.started":"2022-10-28T20:15:15.823436Z","shell.execute_reply":"2022-10-28T20:15:15.856682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}