{"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# 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-06-19T23:32:00.11022Z","iopub.execute_input":"2022-06-19T23:32:00.11078Z","iopub.status.idle":"2022-06-19T23:32:00.124189Z","shell.execute_reply.started":"2022-06-19T23:32:00.11073Z","shell.execute_reply":"2022-06-19T23:32:00.122485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:02.193138Z","iopub.execute_input":"2022-06-19T23:32:02.193931Z","iopub.status.idle":"2022-06-19T23:32:07.258015Z","shell.execute_reply.started":"2022-06-19T23:32:02.193887Z","shell.execute_reply":"2022-06-19T23:32:07.257032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:07.259989Z","iopub.execute_input":"2022-06-19T23:32:07.26043Z","iopub.status.idle":"2022-06-19T23:32:07.267243Z","shell.execute_reply.started":"2022-06-19T23:32:07.260396Z","shell.execute_reply":"2022-06-19T23:32:07.266201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:09.617241Z","iopub.execute_input":"2022-06-19T23:32:09.617693Z","iopub.status.idle":"2022-06-19T23:32:09.638445Z","shell.execute_reply.started":"2022-06-19T23:32:09.617654Z","shell.execute_reply":"2022-06-19T23:32:09.637229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:12.809792Z","iopub.execute_input":"2022-06-19T23:32:12.810287Z","iopub.status.idle":"2022-06-19T23:32:12.817554Z","shell.execute_reply.started":"2022-06-19T23:32:12.810247Z","shell.execute_reply":"2022-06-19T23:32:12.816831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_data[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:20.262417Z","iopub.execute_input":"2022-06-19T23:32:20.263214Z","iopub.status.idle":"2022-06-19T23:32:20.266741Z","shell.execute_reply.started":"2022-06-19T23:32:20.263176Z","shell.execute_reply":"2022-06-19T23:32:20.265994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:22.783864Z","iopub.execute_input":"2022-06-19T23:32:22.784271Z","iopub.status.idle":"2022-06-19T23:32:22.793248Z","shell.execute_reply.started":"2022-06-19T23:32:22.784239Z","shell.execute_reply":"2022-06-19T23:32:22.792095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.drop('label', inplace = True, axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:25.795941Z","iopub.execute_input":"2022-06-19T23:32:25.796347Z","iopub.status.idle":"2022-06-19T23:32:25.887667Z","shell.execute_reply.started":"2022-06-19T23:32:25.796316Z","shell.execute_reply":"2022-06-19T23:32:25.886818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:27.847848Z","iopub.execute_input":"2022-06-19T23:32:27.848277Z","iopub.status.idle":"2022-06-19T23:32:30.476052Z","shell.execute_reply.started":"2022-06-19T23:32:27.848234Z","shell.execute_reply":"2022-06-19T23:32:30.474869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfirst_image = train_data.iloc[0]\nfirst_image = np.array(first_image).reshape(28, 28)\nplt.imshow(first_image);","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:32.402312Z","iopub.execute_input":"2022-06-19T23:32:32.40278Z","iopub.status.idle":"2022-06-19T23:32:32.591682Z","shell.execute_reply.started":"2022-06-19T23:32:32.402742Z","shell.execute_reply":"2022-06-19T23:32:32.590579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reshape_and_normalize(images, len):\n    images = np.array(images).reshape((len, 28, 28, 1))\n    images = images/255\n  \n    return images","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:38.646803Z","iopub.execute_input":"2022-06-19T23:32:38.647224Z","iopub.status.idle":"2022-06-19T23:32:38.652111Z","shell.execute_reply.started":"2022-06-19T23:32:38.647191Z","shell.execute_reply":"2022-06-19T23:32:38.651304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = reshape_and_normalize(train_data, 42000)","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:43.388283Z","iopub.execute_input":"2022-06-19T23:32:43.389273Z","iopub.status.idle":"2022-06-19T23:32:43.584482Z","shell.execute_reply.started":"2022-06-19T23:32:43.389224Z","shell.execute_reply":"2022-06-19T23:32:43.583368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:46.974255Z","iopub.execute_input":"2022-06-19T23:32:46.974737Z","iopub.status.idle":"2022-06-19T23:32:46.97923Z","shell.execute_reply.started":"2022-06-19T23:32:46.974699Z","shell.execute_reply":"2022-06-19T23:32:46.978466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(64, (4, 4), activation='relu', input_shape = (28, 28, 1)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(64, (5, 5), activation='relu'),\n    tf.keras.layers.MaxPooling2D(3, 3),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(150, activation = 'relu'),\n    tf.keras.layers.Dense(10, activation = 'softmax')\n\n\n])","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:51.61641Z","iopub.execute_input":"2022-06-19T23:32:51.617447Z","iopub.status.idle":"2022-06-19T23:32:51.677355Z","shell.execute_reply.started":"2022-06-19T23:32:51.617401Z","shell.execute_reply":"2022-06-19T23:32:51.676338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:32:54.395242Z","iopub.execute_input":"2022-06-19T23:32:54.395705Z","iopub.status.idle":"2022-06-19T23:32:54.40187Z","shell.execute_reply.started":"2022-06-19T23:32:54.395669Z","shell.execute_reply":"2022-06-19T23:32:54.401057Z"},"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":"2022-06-19T23:32:58.977783Z","iopub.execute_input":"2022-06-19T23:32:58.97818Z","iopub.status.idle":"2022-06-19T23:32:58.990398Z","shell.execute_reply.started":"2022-06-19T23:32:58.978148Z","shell.execute_reply":"2022-06-19T23:32:58.989513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_data, y_train, epochs = 15)","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:33:06.693555Z","iopub.execute_input":"2022-06-19T23:33:06.694017Z","iopub.status.idle":"2022-06-19T23:40:48.478292Z","shell.execute_reply.started":"2022-06-19T23:33:06.693978Z","shell.execute_reply":"2022-06-19T23:40:48.477002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.5, 1])\nplt.legend(loc='lower right')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:40:48.704597Z","iopub.execute_input":"2022-06-19T23:40:48.705126Z","iopub.status.idle":"2022-06-19T23:40:48.916431Z","shell.execute_reply.started":"2022-06-19T23:40:48.705074Z","shell.execute_reply":"2022-06-19T23:40:48.915303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = reshape_and_normalize(test_data, 28000)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:42:33.181787Z","iopub.execute_input":"2022-06-19T23:42:33.183042Z","iopub.status.idle":"2022-06-19T23:42:33.317412Z","shell.execute_reply.started":"2022-06-19T23:42:33.182988Z","shell.execute_reply":"2022-06-19T23:42:33.316242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ntest_image = test_data[27999]\ntest_image = np.array(test_image).reshape(28, 28)\nplt.imshow(test_image);","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:43:45.100747Z","iopub.execute_input":"2022-06-19T23:43:45.101164Z","iopub.status.idle":"2022-06-19T23:43:45.240905Z","shell.execute_reply.started":"2022-06-19T23:43:45.10113Z","shell.execute_reply":"2022-06-19T23:43:45.239899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_prediction = model.predict(test_data)\nprint('prediction: \\n', X_prediction[0])","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:43:04.290492Z","iopub.execute_input":"2022-06-19T23:43:04.290935Z","iopub.status.idle":"2022-06-19T23:43:10.805948Z","shell.execute_reply.started":"2022-06-19T23:43:04.290901Z","shell.execute_reply":"2022-06-19T23:43:10.804667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.argmax(X_prediction, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:43:12.595045Z","iopub.execute_input":"2022-06-19T23:43:12.595461Z","iopub.status.idle":"2022-06-19T23:43:12.600136Z","shell.execute_reply.started":"2022-06-19T23:43:12.595428Z","shell.execute_reply":"2022-06-19T23:43:12.599257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pd.Series(pred, name='Label')\npred.head()\n\nindex_list = []\n\nfor i, item in enumerate(pred):\n    index_list.append(i+1)\n    \nimage_id = pd.Series(index_list, name='ImageId')","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:43:19.840199Z","iopub.execute_input":"2022-06-19T23:43:19.84105Z","iopub.status.idle":"2022-06-19T23:43:19.876695Z","shell.execute_reply.started":"2022-06-19T23:43:19.841003Z","shell.execute_reply":"2022-06-19T23:43:19.875444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.concat([image_id, pred], axis=1)\nsubmit.to_csv('submission.csv', index=False)\nsubmit.tail()\nsub = pd.read_csv('./submission.csv')\nsub","metadata":{"execution":{"iopub.status.busy":"2022-06-19T23:43:24.345883Z","iopub.execute_input":"2022-06-19T23:43:24.346706Z","iopub.status.idle":"2022-06-19T23:43:24.415251Z","shell.execute_reply.started":"2022-06-19T23:43:24.346664Z","shell.execute_reply":"2022-06-19T23:43:24.414249Z"},"trusted":true},"execution_count":null,"outputs":[]}]}