{"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-11-01T20:57:51.378524Z","iopub.execute_input":"2022-11-01T20:57:51.379423Z","iopub.status.idle":"2022-11-01T20:57:51.404474Z","shell.execute_reply.started":"2022-11-01T20:57:51.379295Z","shell.execute_reply":"2022-11-01T20:57:51.403489Z"},"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-11-01T20:58:19.458508Z","iopub.execute_input":"2022-11-01T20:58:19.459083Z","iopub.status.idle":"2022-11-01T20:58:19.464400Z","shell.execute_reply.started":"2022-11-01T20:58:19.459049Z","shell.execute_reply":"2022-11-01T20:58:19.463029Z"},"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\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:01:18.932842Z","iopub.execute_input":"2022-11-01T21:01:18.933203Z","iopub.status.idle":"2022-11-01T21:01:18.940868Z","shell.execute_reply.started":"2022-11-01T21:01:18.933172Z","shell.execute_reply":"2022-11-01T21:01:18.939985Z"},"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-11-01T21:01:19.161525Z","iopub.execute_input":"2022-11-01T21:01:19.162100Z","iopub.status.idle":"2022-11-01T21:01:22.383641Z","shell.execute_reply.started":"2022-11-01T21:01:19.162067Z","shell.execute_reply":"2022-11-01T21:01:22.382644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:01:22.385558Z","iopub.execute_input":"2022-11-01T21:01:22.386200Z","iopub.status.idle":"2022-11-01T21:01:22.404953Z","shell.execute_reply.started":"2022-11-01T21:01:22.386158Z","shell.execute_reply":"2022-11-01T21:01:22.403809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:01:22.406666Z","iopub.execute_input":"2022-11-01T21:01:22.407036Z","iopub.status.idle":"2022-11-01T21:01:22.425439Z","shell.execute_reply.started":"2022-11-01T21:01:22.406993Z","shell.execute_reply":"2022-11-01T21:01:22.424319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 28*28\nimage_size","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:01:22.428658Z","iopub.execute_input":"2022-11-01T21:01:22.429306Z","iopub.status.idle":"2022-11-01T21:01:22.436679Z","shell.execute_reply.started":"2022-11-01T21:01:22.429273Z","shell.execute_reply":"2022-11-01T21:01:22.435679Z"},"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-11-01T21:01:22.437833Z","iopub.execute_input":"2022-11-01T21:01:22.439897Z","iopub.status.idle":"2022-11-01T21:01:22.681977Z","shell.execute_reply.started":"2022-11-01T21:01:22.439862Z","shell.execute_reply":"2022-11-01T21:01:22.680834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:01:22.683657Z","iopub.execute_input":"2022-11-01T21:01:22.684065Z","iopub.status.idle":"2022-11-01T21:01:25.221517Z","shell.execute_reply.started":"2022-11-01T21:01:22.684021Z","shell.execute_reply":"2022-11-01T21:01:25.220628Z"},"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-11-01T21:01:25.224656Z","iopub.execute_input":"2022-11-01T21:01:25.224931Z","iopub.status.idle":"2022-11-01T21:01:25.392377Z","shell.execute_reply.started":"2022-11-01T21:01:25.224906Z","shell.execute_reply":"2022-11-01T21:01:25.391290Z"},"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-11-01T21:01:25.394061Z","iopub.execute_input":"2022-11-01T21:01:25.394429Z","iopub.status.idle":"2022-11-01T21:01:25.399865Z","shell.execute_reply.started":"2022-11-01T21:01:25.394392Z","shell.execute_reply":"2022-11-01T21:01:25.398835Z"},"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-11-01T21:01:25.401593Z","iopub.execute_input":"2022-11-01T21:01:25.402335Z","iopub.status.idle":"2022-11-01T21:01:25.703639Z","shell.execute_reply.started":"2022-11-01T21:01:25.402300Z","shell.execute_reply":"2022-11-01T21:01:25.702415Z"},"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-11-01T21:01:25.710021Z","iopub.execute_input":"2022-11-01T21:01:25.710466Z","iopub.status.idle":"2022-11-01T21:01:25.884510Z","shell.execute_reply.started":"2022-11-01T21:01:25.710431Z","shell.execute_reply":"2022-11-01T21:01:25.883421Z"},"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-11-01T21:01:25.888260Z","iopub.execute_input":"2022-11-01T21:01:25.888694Z","iopub.status.idle":"2022-11-01T21:01:26.325310Z","shell.execute_reply.started":"2022-11-01T21:01:25.888659Z","shell.execute_reply":"2022-11-01T21:01:26.324361Z"},"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-11-01T21:01:26.326733Z","iopub.execute_input":"2022-11-01T21:01:26.327118Z","iopub.status.idle":"2022-11-01T21:01:26.333918Z","shell.execute_reply.started":"2022-11-01T21:01:26.327078Z","shell.execute_reply":"2022-11-01T21:01:26.332947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def CNN():\n    model = Sequential()\n\n    model.add(Conv2D(filters = 32, kernel_size = (4,4), input_shape = (28,28,1), activation = \"relu\"))\n    model.add(MaxPool2D(pool_size = (2,2)))\n\n    model.add(Flatten())\n\n    model.add(Dense(128, activation = \"relu\"))\n    model.add(Dense(256, activation = \"relu\"))\n    model.add(Dense(512, activation = \"relu\"))\n\n    model.add(Dense(10, activation = \"softmax\"))\n\n\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:01:26.335160Z","iopub.execute_input":"2022-11-01T21:01:26.335844Z","iopub.status.idle":"2022-11-01T21:01:26.344102Z","shell.execute_reply.started":"2022-11-01T21:01:26.335807Z","shell.execute_reply":"2022-11-01T21:01:26.343199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CNN()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:01:26.345490Z","iopub.execute_input":"2022-11-01T21:01:26.345854Z","iopub.status.idle":"2022-11-01T21:01:26.427239Z","shell.execute_reply.started":"2022-11-01T21:01:26.345820Z","shell.execute_reply":"2022-11-01T21:01:26.426310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = \"Adam\", loss = \"CategoricalCrossentropy\", metrics = \"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:01:26.428441Z","iopub.execute_input":"2022-11-01T21:01:26.428795Z","iopub.status.idle":"2022-11-01T21:01:26.441717Z","shell.execute_reply.started":"2022-11-01T21:01:26.428759Z","shell.execute_reply":"2022-11-01T21:01:26.440742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:01:45.739053Z","iopub.execute_input":"2022-11-01T21:01:45.739436Z","iopub.status.idle":"2022-11-01T21:01:45.745869Z","shell.execute_reply.started":"2022-11-01T21:01:45.739404Z","shell.execute_reply":"2022-11-01T21:01:45.744687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:02:16.433238Z","iopub.execute_input":"2022-11-01T21:02:16.434167Z","iopub.status.idle":"2022-11-01T21:02:16.440806Z","shell.execute_reply.started":"2022-11-01T21:02:16.434119Z","shell.execute_reply":"2022-11-01T21:02:16.439856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = EarlyStopping(monitor = \"val_loss\", patience = 1)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:02:25.048486Z","iopub.execute_input":"2022-11-01T21:02:25.048861Z","iopub.status.idle":"2022-11-01T21:02:25.053992Z","shell.execute_reply.started":"2022-11-01T21:02:25.048829Z","shell.execute_reply":"2022-11-01T21:02:25.052866Z"},"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, callbacks = [early_stop] )","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:02:58.334561Z","iopub.execute_input":"2022-11-01T21:02:58.334930Z","iopub.status.idle":"2022-11-01T21:03:03.432296Z","shell.execute_reply.started":"2022-11-01T21:02:58.334900Z","shell.execute_reply":"2022-11-01T21:03:03.431371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = pd.DataFrame(model.history.history)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:03:26.676117Z","iopub.execute_input":"2022-11-01T21:03:26.676913Z","iopub.status.idle":"2022-11-01T21:03:26.687200Z","shell.execute_reply.started":"2022-11-01T21:03:26.676867Z","shell.execute_reply":"2022-11-01T21:03:26.685983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics[[\"loss\",\"val_loss\"]].plot()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:03:36.546220Z","iopub.execute_input":"2022-11-01T21:03:36.546912Z","iopub.status.idle":"2022-11-01T21:03:36.813600Z","shell.execute_reply.started":"2022-11-01T21:03:36.546876Z","shell.execute_reply":"2022-11-01T21:03:36.812657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics[[\"accuracy\", \"val_accuracy\"]].plot()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:03:50.424639Z","iopub.execute_input":"2022-11-01T21:03:50.425013Z","iopub.status.idle":"2022-11-01T21:03:50.664191Z","shell.execute_reply.started":"2022-11-01T21:03:50.424981Z","shell.execute_reply":"2022-11-01T21:03:50.663308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.metrics_names","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:04:11.044041Z","iopub.execute_input":"2022-11-01T21:04:11.044496Z","iopub.status.idle":"2022-11-01T21:04:11.051702Z","shell.execute_reply.started":"2022-11-01T21:04:11.044459Z","shell.execute_reply":"2022-11-01T21:04:11.050764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:04:45.438679Z","iopub.execute_input":"2022-11-01T21:04:45.439028Z","iopub.status.idle":"2022-11-01T21:04:45.445982Z","shell.execute_reply.started":"2022-11-01T21:04:45.439000Z","shell.execute_reply":"2022-11-01T21:04:45.445043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_val.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:05:05.511169Z","iopub.execute_input":"2022-11-01T21:05:05.511769Z","iopub.status.idle":"2022-11-01T21:05:05.534428Z","shell.execute_reply.started":"2022-11-01T21:05:05.511715Z","shell.execute_reply":"2022-11-01T21:05:05.525570Z"},"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-11-01T21:23:33.188077Z","iopub.execute_input":"2022-11-01T21:23:33.188477Z","iopub.status.idle":"2022-11-01T21:23:33.195813Z","shell.execute_reply.started":"2022-11-01T21:23:33.188443Z","shell.execute_reply":"2022-11-01T21:23:33.194846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict(model, X_test, 25)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T21:23:34.530766Z","iopub.execute_input":"2022-11-01T21:23:34.531650Z","iopub.status.idle":"2022-11-01T21:23:37.223125Z","shell.execute_reply.started":"2022-11-01T21:23:34.531603Z","shell.execute_reply":"2022-11-01T21:23:37.222163Z"},"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-11-01T21:23:51.115676Z","iopub.execute_input":"2022-11-01T21:23:51.116039Z","iopub.status.idle":"2022-11-01T21:23:52.463520Z","shell.execute_reply.started":"2022-11-01T21:23:51.116002Z","shell.execute_reply":"2022-11-01T21:23:52.462493Z"},"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-11-01T21:24:44.276680Z","iopub.execute_input":"2022-11-01T21:24:44.277061Z","iopub.status.idle":"2022-11-01T21:24:44.313232Z","shell.execute_reply.started":"2022-11-01T21:24:44.277010Z","shell.execute_reply":"2022-11-01T21:24:44.312309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}