{"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":"2023-08-05T17:07:19.810749Z","iopub.execute_input":"2023-08-05T17:07:19.811124Z","iopub.status.idle":"2023-08-05T17:07:19.848285Z","shell.execute_reply.started":"2023-08-05T17:07:19.811092Z","shell.execute_reply":"2023-08-05T17:07:19.846984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-08-05T17:14:18.912608Z","iopub.execute_input":"2023-08-05T17:14:18.913036Z","iopub.status.idle":"2023-08-05T17:14:24.282557Z","shell.execute_reply.started":"2023-08-05T17:14:18.913005Z","shell.execute_reply":"2023-08-05T17:14:24.281442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(train.drop('label', axis=1))\ny_train = np.array(train.label)","metadata":{"execution":{"iopub.status.busy":"2023-08-05T17:14:43.925237Z","iopub.execute_input":"2023-08-05T17:14:43.925636Z","iopub.status.idle":"2023-08-05T17:14:44.198544Z","shell.execute_reply.started":"2023-08-05T17:14:43.925605Z","shell.execute_reply":"2023-08-05T17:14:44.197443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout\n\nmodel = Sequential()\n\n# Layer input\nmodel.add(Dense(512, input_shape=(784,), activation='relu'))\nmodel.add(Dropout(0.2))\n\n# Hidden layer\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.2))\n\n# Hidden layer\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dropout(0.2))\n\n# Output layer\nmodel.add(Dense(10, activation='softmax'))\n\n# Compile model\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-08-05T17:15:20.491770Z","iopub.execute_input":"2023-08-05T17:15:20.492263Z","iopub.status.idle":"2023-08-05T17:15:28.914418Z","shell.execute_reply.started":"2023-08-05T17:15:20.492229Z","shell.execute_reply":"2023-08-05T17:15:28.913484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=\"adam\", loss=\"sparse_categorical_crossentropy\", metrics=[\"accuracy\"])\n\nhistory = model.fit(x_train, y_train, epochs=20, batch_size=128, validation_split=0.3)","metadata":{"execution":{"iopub.status.busy":"2023-08-05T17:15:53.446017Z","iopub.execute_input":"2023-08-05T17:15:53.446408Z","iopub.status.idle":"2023-08-05T17:17:17.068273Z","shell.execute_reply.started":"2023-08-05T17:15:53.446370Z","shell.execute_reply":"2023-08-05T17:17:17.066838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.array(test)\n\ny_pred = model.predict(x_test)\ny_pred = np.argmax(y_pred, axis=1)\n\nsubmission_df = pd.DataFrame({\"ImageId\": range(1, len(y_pred) + 1), \"Label\": y_pred})\n\n\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-05T17:17:17.070718Z","iopub.execute_input":"2023-08-05T17:17:17.071060Z","iopub.status.idle":"2023-08-05T17:17:19.757759Z","shell.execute_reply.started":"2023-08-05T17:17:17.071032Z","shell.execute_reply":"2023-08-05T17:17:19.756902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-05T17:18:02.618548Z","iopub.execute_input":"2023-08-05T17:18:02.618996Z","iopub.status.idle":"2023-08-05T17:18:02.927245Z","shell.execute_reply.started":"2023-08-05T17:18:02.618954Z","shell.execute_reply":"2023-08-05T17:18:02.925976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-05T17:18:08.107116Z","iopub.execute_input":"2023-08-05T17:18:08.108242Z","iopub.status.idle":"2023-08-05T17:18:08.378079Z","shell.execute_reply.started":"2023-08-05T17:18:08.108188Z","shell.execute_reply":"2023-08-05T17:18:08.376945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}