{"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-11-01T02:32:09.302771Z","iopub.execute_input":"2022-11-01T02:32:09.303974Z","iopub.status.idle":"2022-11-01T02:32:09.330928Z","shell.execute_reply.started":"2022-11-01T02:32:09.303839Z","shell.execute_reply":"2022-11-01T02:32:09.328659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:33:37.024494Z","iopub.execute_input":"2022-11-01T02:33:37.024916Z","iopub.status.idle":"2022-11-01T02:33:40.968561Z","shell.execute_reply.started":"2022-11-01T02:33:37.024864Z","shell.execute_reply":"2022-11-01T02:33:40.967306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:33:41.842464Z","iopub.execute_input":"2022-11-01T02:33:41.842863Z","iopub.status.idle":"2022-11-01T02:33:41.861626Z","shell.execute_reply.started":"2022-11-01T02:33:41.842831Z","shell.execute_reply":"2022-11-01T02:33:41.860157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df_train.drop(['label'], axis=1)\ny = df_train.label\n\ntest = df_test.copy()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:33:42.586579Z","iopub.execute_input":"2022-11-01T02:33:42.587012Z","iopub.status.idle":"2022-11-01T02:33:42.806219Z","shell.execute_reply.started":"2022-11-01T02:33:42.586978Z","shell.execute_reply":"2022-11-01T02:33:42.804422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = x / 255.0\ntest = test / 255.0","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:33:55.796222Z","iopub.execute_input":"2022-11-01T02:33:55.796624Z","iopub.status.idle":"2022-11-01T02:33:55.914849Z","shell.execute_reply.started":"2022-11-01T02:33:55.796591Z","shell.execute_reply":"2022-11-01T02:33:55.914065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(x).reshape(-1, 28, 28, 1)\ntest = np.array(test).reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:36:01.787685Z","iopub.execute_input":"2022-11-01T02:36:01.788085Z","iopub.status.idle":"2022-11-01T02:36:01.966164Z","shell.execute_reply.started":"2022-11-01T02:36:01.788054Z","shell.execute_reply":"2022-11-01T02:36:01.965230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:36:07.407953Z","iopub.execute_input":"2022-11-01T02:36:07.408497Z","iopub.status.idle":"2022-11-01T02:36:07.416201Z","shell.execute_reply.started":"2022-11-01T02:36:07.408464Z","shell.execute_reply":"2022-11-01T02:36:07.414529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:36:14.050694Z","iopub.execute_input":"2022-11-01T02:36:14.051105Z","iopub.status.idle":"2022-11-01T02:36:14.058564Z","shell.execute_reply.started":"2022-11-01T02:36:14.051070Z","shell.execute_reply":"2022-11-01T02:36:14.057317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:36:42.097580Z","iopub.execute_input":"2022-11-01T02:36:42.098013Z","iopub.status.idle":"2022-11-01T02:36:42.607561Z","shell.execute_reply.started":"2022-11-01T02:36:42.097976Z","shell.execute_reply":"2022-11-01T02:36:42.606473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='label', data=df_train)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:36:56.668445Z","iopub.execute_input":"2022-11-01T02:36:56.668835Z","iopub.status.idle":"2022-11-01T02:36:56.946038Z","shell.execute_reply.started":"2022-11-01T02:36:56.668804Z","shell.execute_reply":"2022-11-01T02:36:56.944690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:37:09.359796Z","iopub.execute_input":"2022-11-01T02:37:09.360303Z","iopub.status.idle":"2022-11-01T02:37:14.585479Z","shell.execute_reply.started":"2022-11-01T02:37:09.360267Z","shell.execute_reply":"2022-11-01T02:37:14.584348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPool2D((2, 2)),\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n    tf.keras.layers.MaxPool2D((2, 2)),\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n    \n    tf.keras.layers.Flatten(input_shape=(28, 28)),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(10)\n])","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:41:34.675957Z","iopub.execute_input":"2022-11-01T02:41:34.676377Z","iopub.status.idle":"2022-11-01T02:41:34.780782Z","shell.execute_reply.started":"2022-11-01T02:41:34.676339Z","shell.execute_reply":"2022-11-01T02:41:34.779957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:44:37.108401Z","iopub.execute_input":"2022-11-01T02:44:37.108829Z","iopub.status.idle":"2022-11-01T02:44:37.126981Z","shell.execute_reply.started":"2022-11-01T02:44:37.108785Z","shell.execute_reply":"2022-11-01T02:44:37.125617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x, y, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:44:57.224128Z","iopub.execute_input":"2022-11-01T02:44:57.224563Z","iopub.status.idle":"2022-11-01T02:47:53.456763Z","shell.execute_reply.started":"2022-11-01T02:44:57.224526Z","shell.execute_reply":"2022-11-01T02:47:53.455522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_model = model.predict(x)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:48:19.263857Z","iopub.execute_input":"2022-11-01T02:48:19.264297Z","iopub.status.idle":"2022-11-01T02:48:24.723449Z","shell.execute_reply.started":"2022-11-01T02:48:19.264261Z","shell.execute_reply":"2022-11-01T02:48:24.721216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_model = tf.keras.Sequential([model,\n                                        tf.keras.layers.Softmax()])","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:48:31.491242Z","iopub.execute_input":"2022-11-01T02:48:31.491666Z","iopub.status.idle":"2022-11-01T02:48:31.531543Z","shell.execute_reply.started":"2022-11-01T02:48:31.491632Z","shell.execute_reply":"2022-11-01T02:48:31.530515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = probability_model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:48:31.898988Z","iopub.execute_input":"2022-11-01T02:48:31.899383Z","iopub.status.idle":"2022-11-01T02:48:35.796438Z","shell.execute_reply.started":"2022-11-01T02:48:31.899351Z","shell.execute_reply":"2022-11-01T02:48:35.795423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = np.argmax(prediction, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:50:15.338779Z","iopub.execute_input":"2022-11-01T02:50:15.339816Z","iopub.status.idle":"2022-11-01T02:50:15.345536Z","shell.execute_reply.started":"2022-11-01T02:50:15.339775Z","shell.execute_reply":"2022-11-01T02:50:15.344313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(prediction)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:51:28.831108Z","iopub.execute_input":"2022-11-01T02:51:28.831548Z","iopub.status.idle":"2022-11-01T02:51:28.838505Z","shell.execute_reply.started":"2022-11-01T02:51:28.831512Z","shell.execute_reply":"2022-11-01T02:51:28.837546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_col = pd.Series(prediction, name='label')\nid_col = pd.Series(range(1, 28001), name='ImageId')\n\nsubmission = pd.concat([id_col, prediction_col], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:52:34.683651Z","iopub.execute_input":"2022-11-01T02:52:34.684058Z","iopub.status.idle":"2022-11-01T02:52:34.692932Z","shell.execute_reply.started":"2022-11-01T02:52:34.684021Z","shell.execute_reply":"2022-11-01T02:52:34.691669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T02:52:57.527290Z","iopub.execute_input":"2022-11-01T02:52:57.528128Z","iopub.status.idle":"2022-11-01T02:52:57.575094Z","shell.execute_reply.started":"2022-11-01T02:52:57.528084Z","shell.execute_reply":"2022-11-01T02:52:57.573863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}