{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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# 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":"2023-12-25T04:54:39.247485Z","iopub.execute_input":"2023-12-25T04:54:39.248609Z","iopub.status.idle":"2023-12-25T04:54:39.691097Z","shell.execute_reply.started":"2023-12-25T04:54:39.248566Z","shell.execute_reply":"2023-12-25T04:54:39.689931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:54:39.695521Z","iopub.execute_input":"2023-12-25T04:54:39.696001Z","iopub.status.idle":"2023-12-25T04:54:53.618090Z","shell.execute_reply.started":"2023-12-25T04:54:39.695968Z","shell.execute_reply":"2023-12-25T04:54:53.617207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_txt = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_txt = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:54:53.619186Z","iopub.execute_input":"2023-12-25T04:54:53.620237Z","iopub.status.idle":"2023-12-25T04:55:00.461629Z","shell.execute_reply.started":"2023-12-25T04:54:53.620202Z","shell.execute_reply":"2023-12-25T04:55:00.460547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_txt","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:00.463289Z","iopub.execute_input":"2023-12-25T04:55:00.463740Z","iopub.status.idle":"2023-12-25T04:55:00.505719Z","shell.execute_reply.started":"2023-12-25T04:55:00.463700Z","shell.execute_reply":"2023-12-25T04:55:00.504552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_txt = train_txt.label\nx_train_txt = train_txt.drop('label', axis=1)\nx_train_txt","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:00.509491Z","iopub.execute_input":"2023-12-25T04:55:00.509846Z","iopub.status.idle":"2023-12-25T04:55:00.647691Z","shell.execute_reply.started":"2023-12-25T04:55:00.509814Z","shell.execute_reply":"2023-12-25T04:55:00.646599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_txt = x_train_txt.to_numpy()\ny_train_txt = y_train_txt.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:00.648965Z","iopub.execute_input":"2023-12-25T04:55:00.649385Z","iopub.status.idle":"2023-12-25T04:55:00.653843Z","shell.execute_reply.started":"2023-12-25T04:55:00.649341Z","shell.execute_reply":"2023-12-25T04:55:00.652704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_txt","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:00.655054Z","iopub.execute_input":"2023-12-25T04:55:00.655960Z","iopub.status.idle":"2023-12-25T04:55:00.666832Z","shell.execute_reply.started":"2023-12-25T04:55:00.655904Z","shell.execute_reply":"2023-12-25T04:55:00.666099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_txt = x_train_txt.reshape(42000,28,28)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:00.668450Z","iopub.execute_input":"2023-12-25T04:55:00.668760Z","iopub.status.idle":"2023-12-25T04:55:00.676787Z","shell.execute_reply.started":"2023-12-25T04:55:00.668734Z","shell.execute_reply":"2023-12-25T04:55:00.675550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_txt","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:00.678558Z","iopub.execute_input":"2023-12-25T04:55:00.679034Z","iopub.status.idle":"2023-12-25T04:55:00.691043Z","shell.execute_reply.started":"2023-12-25T04:55:00.678994Z","shell.execute_reply":"2023-12-25T04:55:00.689757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_txt = x_train_txt/255","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:00.692656Z","iopub.execute_input":"2023-12-25T04:55:00.693045Z","iopub.status.idle":"2023-12-25T04:55:00.831543Z","shell.execute_reply.started":"2023-12-25T04:55:00.693001Z","shell.execute_reply":"2023-12-25T04:55:00.830541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.Sequential([\n    keras.layers.Conv2D(filters=12, kernel_size=(5,5),activation='relu',input_shape=[28,28,1]),\n    keras.layers.AveragePooling2D(strides=2),\n    keras.layers.Conv2D(filters=12, kernel_size=(5,5),activation='relu'),\n    keras.layers.AveragePooling2D(strides=2),\n    keras.layers.Flatten(),\n    keras.layers.Dense(120,activation='relu'),\n    keras.layers.Dropout(0.3),\n    keras.layers.Dense(84,activation='relu'),\n    keras.layers.Dropout(0.3),\n    keras.layers.Dense(10,activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:00.832859Z","iopub.execute_input":"2023-12-25T04:55:00.833248Z","iopub.status.idle":"2023-12-25T04:55:01.201527Z","shell.execute_reply.started":"2023-12-25T04:55:00.833218Z","shell.execute_reply":"2023-12-25T04:55:01.200326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics='accuracy')","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:01.202876Z","iopub.execute_input":"2023-12-25T04:55:01.203244Z","iopub.status.idle":"2023-12-25T04:55:01.226440Z","shell.execute_reply.started":"2023-12-25T04:55:01.203212Z","shell.execute_reply":"2023-12-25T04:55:01.224534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train_txt, y_train_txt, epochs = 15, validation_split = 0.2, callbacks=keras.callbacks.EarlyStopping(patience=5))","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:55:01.227928Z","iopub.execute_input":"2023-12-25T04:55:01.228439Z","iopub.status.idle":"2023-12-25T04:58:00.679887Z","shell.execute_reply.started":"2023-12-25T04:55:01.228400Z","shell.execute_reply":"2023-12-25T04:58:00.678750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_train_txt, y_train_txt)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:58:00.686303Z","iopub.execute_input":"2023-12-25T04:58:00.687171Z","iopub.status.idle":"2023-12-25T04:58:07.148729Z","shell.execute_reply.started":"2023-12-25T04:58:00.687132Z","shell.execute_reply":"2023-12-25T04:58:07.147464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test_txt.to_numpy()\nx_test = x_test.reshape(28000,28,28)/255\ny_pred = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:58:07.150673Z","iopub.execute_input":"2023-12-25T04:58:07.151457Z","iopub.status.idle":"2023-12-25T04:58:12.763706Z","shell.execute_reply.started":"2023-12-25T04:58:07.151408Z","shell.execute_reply":"2023-12-25T04:58:12.762445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_out = pd.DataFrame({'ImageId': [i for i in range(1, 28001)], 'Label': np.argmax(y_pred, axis = 1)})","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:58:12.765569Z","iopub.execute_input":"2023-12-25T04:58:12.766084Z","iopub.status.idle":"2023-12-25T04:58:12.790849Z","shell.execute_reply.started":"2023-12-25T04:58:12.766038Z","shell.execute_reply":"2023-12-25T04:58:12.789922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_out.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T04:58:12.792513Z","iopub.execute_input":"2023-12-25T04:58:12.793199Z","iopub.status.idle":"2023-12-25T04:58:12.866395Z","shell.execute_reply.started":"2023-12-25T04:58:12.793158Z","shell.execute_reply":"2023-12-25T04:58:12.865280Z"},"trusted":true},"execution_count":null,"outputs":[]}]}