{"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":"2021-07-23T11:18:34.886629Z","iopub.execute_input":"2021-07-23T11:18:34.886990Z","iopub.status.idle":"2021-07-23T11:18:34.896001Z","shell.execute_reply.started":"2021-07-23T11:18:34.886959Z","shell.execute_reply":"2021-07-23T11:18:34.894861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sb\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:19:36.164189Z","iopub.execute_input":"2021-07-23T11:19:36.164507Z","iopub.status.idle":"2021-07-23T11:19:36.169715Z","shell.execute_reply.started":"2021-07-23T11:19:36.164481Z","shell.execute_reply":"2021-07-23T11:19:36.168348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train =pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:21:27.303449Z","iopub.execute_input":"2021-07-23T11:21:27.303842Z","iopub.status.idle":"2021-07-23T11:21:31.002494Z","shell.execute_reply.started":"2021-07-23T11:21:27.303809Z","shell.execute_reply":"2021-07-23T11:21:31.001629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:22:03.980933Z","iopub.execute_input":"2021-07-23T11:22:03.981281Z","iopub.status.idle":"2021-07-23T11:22:06.233213Z","shell.execute_reply.started":"2021-07-23T11:22:03.981250Z","shell.execute_reply":"2021-07-23T11:22:06.232233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = df_train.drop('label', axis=1)\ny_train = df_train.label","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:22:34.483859Z","iopub.execute_input":"2021-07-23T11:22:34.484206Z","iopub.status.idle":"2021-07-23T11:22:34.597554Z","shell.execute_reply.started":"2021-07-23T11:22:34.484178Z","shell.execute_reply":"2021-07-23T11:22:34.596824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    digit = X_train[X_train.index==i].to_numpy()\n    digit = digit.reshape((28,28))\n    plt.matshow(digit)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:22:56.332261Z","iopub.execute_input":"2021-07-23T11:22:56.332770Z","iopub.status.idle":"2021-07-23T11:22:57.859069Z","shell.execute_reply.started":"2021-07-23T11:22:56.332721Z","shell.execute_reply":"2021-07-23T11:22:57.858047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### creating model\nmodel = keras.Sequential([\n    keras.layers.Dense(200, input_shape=(784,), activation='sigmoid'),\n    keras.layers.Dense(10, input_shape=(200,), activation='sigmoid')\n])\n\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:23:30.395482Z","iopub.execute_input":"2021-07-23T11:23:30.396018Z","iopub.status.idle":"2021-07-23T11:23:30.426041Z","shell.execute_reply.started":"2021-07-23T11:23:30.395985Z","shell.execute_reply":"2021-07-23T11:23:30.425112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### training model\nmodel.fit(X_train, y_train, epochs=5)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:24:17.521447Z","iopub.execute_input":"2021-07-23T11:24:17.521808Z","iopub.status.idle":"2021-07-23T11:24:29.071936Z","shell.execute_reply.started":"2021-07-23T11:24:17.521780Z","shell.execute_reply":"2021-07-23T11:24:29.070980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### evaluating model\ny_train_predicted = model.predict(X_train)\ny_train_predicted_digit = [np.argmax(i) for i in y_train_predicted]\ncm = tf.math.confusion_matrix(labels=y_train, predictions=y_train_predicted_digit)\nplt.figure(figsize=(10,8))\nsb.heatmap(cm,annot=True,fmt='d')\nplt.xlabel('Predicted')\nplt.ylabel('Truth')","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:24:56.974301Z","iopub.execute_input":"2021-07-23T11:24:56.974639Z","iopub.status.idle":"2021-07-23T11:24:59.312540Z","shell.execute_reply.started":"2021-07-23T11:24:56.974611Z","shell.execute_reply":"2021-07-23T11:24:59.311745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### making submission\ny_test = model.predict(X_test)\ny_test = [np.argmax(i) for i in y_test]\nImageId = pd.DataFrame([i for i in range(1,28001)], columns=['ImageId'])\ny_test = pd.DataFrame(y_test, columns=['Label'])\nsubmission = pd.concat([ImageId, y_test], axis=1)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:26:08.400996Z","iopub.execute_input":"2021-07-23T11:26:08.401342Z","iopub.status.idle":"2021-07-23T11:26:09.757754Z","shell.execute_reply.started":"2021-07-23T11:26:08.401313Z","shell.execute_reply":"2021-07-23T11:26:09.756852Z"},"trusted":true},"execution_count":null,"outputs":[]}]}