{"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)\nfrom matplotlib import pyplot as plt\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# You can write up to 5GB 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-09-06T08:44:18.906105Z","iopub.execute_input":"2022-09-06T08:44:18.906463Z","iopub.status.idle":"2022-09-06T08:44:18.915672Z","shell.execute_reply.started":"2022-09-06T08:44:18.906431Z","shell.execute_reply":"2022-09-06T08:44:18.914237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# > **DATA EXTRACTION**","metadata":{}},{"cell_type":"code","source":"train_folder = '/kaggle/input/digit-recognizer/train.csv'\ntrain_data = pd.read_csv(train_folder)\ntrain_data = np.array(train_data)\n\ntrain_labels = train_data[:, 0]\ntrain_images = train_data[:, 1:]\ntrain_images = train_images.reshape((-1, 28, 28, 1))\n\nplt.imshow(train_images[0, :, :, 0])\nplt.show()\n\ntest_folder = '/kaggle/input/digit-recognizer/test.csv'\ntest_data = pd.read_csv(test_folder)\ntest_data = np.array(test_data)\n\ntest_images = test_data.reshape((-1, 28, 28, 1))\n\nplt.imshow(test_images[45, :, :, 0])\nplt.show()","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2022-09-06T08:44:18.929579Z","iopub.execute_input":"2022-09-06T08:44:18.929925Z","iopub.status.idle":"2022-09-06T08:44:25.017309Z","shell.execute_reply.started":"2022-09-06T08:44:18.929896Z","shell.execute_reply":"2022-09-06T08:44:25.016192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **CREATE MODEL**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Activation, Flatten, Dense\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (2,2), activation='relu', padding='same', input_shape=(28, 28, 1)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy', optimizer='adamax', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-06T08:44:25.019398Z","iopub.execute_input":"2022-09-06T08:44:25.019695Z","iopub.status.idle":"2022-09-06T08:44:31.453417Z","shell.execute_reply.started":"2022-09-06T08:44:25.019666Z","shell.execute_reply":"2022-09-06T08:44:31.452457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **COMPILE THE MODEL**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\nhistory = model.fit(train_images, to_categorical(train_labels), batch_size=128, epochs=10, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-09-06T08:44:31.454895Z","iopub.execute_input":"2022-09-06T08:44:31.455216Z","iopub.status.idle":"2022-09-06T08:45:45.232210Z","shell.execute_reply.started":"2022-09-06T08:44:31.455175Z","shell.execute_reply":"2022-09-06T08:45:45.231180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (loss_ax, acc_ax) = plt.subplots(ncols=2)\n\n\nloss_ax.set_title('Loss')\nloss_ax.plot(history.history['loss'], label='train loss')\nloss_ax.plot(history.history['val_loss'], label='val loss')\nloss_ax.legend()\n\nacc_ax.set_title('Accuracy')\nacc_ax.plot(history.history['accuracy'], label='train acc')\nacc_ax.plot(history.history['val_accuracy'], label='val acc')\nacc_ax.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-06T08:45:45.233724Z","iopub.execute_input":"2022-09-06T08:45:45.234076Z","iopub.status.idle":"2022-09-06T08:45:45.536055Z","shell.execute_reply.started":"2022-09-06T08:45:45.234044Z","shell.execute_reply":"2022-09-06T08:45:45.534935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    img = test_images[i]\n    pred = model.predict_classes(img[None, :])\n    \n    img = np.squeeze(img)\n    \n    plt.title(f\"Pred Class: {pred}\")\n    plt.imshow(img, cmap='gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-06T08:45:45.538467Z","iopub.execute_input":"2022-09-06T08:45:45.538790Z","iopub.status.idle":"2022-09-06T08:45:46.471515Z","shell.execute_reply.started":"2022-09-06T08:45:45.538760Z","shell.execute_reply":"2022-09-06T08:45:46.470452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}