{"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-03-09T04:15:18.626432Z","iopub.execute_input":"2022-03-09T04:15:18.626854Z","iopub.status.idle":"2022-03-09T04:15:18.661805Z","shell.execute_reply.started":"2022-03-09T04:15:18.626740Z","shell.execute_reply":"2022-03-09T04:15:18.660552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, BatchNormalization, Activation, Input, Dropout\nfrom keras.utils.np_utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2022-03-09T04:15:18.663907Z","iopub.execute_input":"2022-03-09T04:15:18.664410Z","iopub.status.idle":"2022-03-09T04:15:25.812584Z","shell.execute_reply.started":"2022-03-09T04:15:18.664369Z","shell.execute_reply":"2022-03-09T04:15:25.811249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-03-09T04:15:25.816797Z","iopub.execute_input":"2022-03-09T04:15:25.817158Z","iopub.status.idle":"2022-03-09T04:15:32.573398Z","shell.execute_reply.started":"2022-03-09T04:15:25.817115Z","shell.execute_reply":"2022-03-09T04:15:32.572351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2022-03-09T04:15:32.575746Z","iopub.execute_input":"2022-03-09T04:15:32.576116Z","iopub.status.idle":"2022-03-09T04:15:32.604676Z","shell.execute_reply.started":"2022-03-09T04:15:32.576080Z","shell.execute_reply":"2022-03-09T04:15:32.603569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dividing the data into train and test\ny_train=train_df[\"label\"].values\nX_train=train_df.iloc[:,1:].values\nX_test=test_df.values","metadata":{"execution":{"iopub.status.busy":"2022-03-09T04:15:32.605865Z","iopub.execute_input":"2022-03-09T04:15:32.606109Z","iopub.status.idle":"2022-03-09T04:15:32.616535Z","shell.execute_reply.started":"2022-03-09T04:15:32.606079Z","shell.execute_reply":"2022-03-09T04:15:32.615258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# normalize the values in range of -1 to 1\nX_train=(X_train-127.5)/127.5\nX_test=(X_test-127.5)/127.5\n\n#one-hot encoding of the actual labels\ny_train=to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T04:15:32.617839Z","iopub.execute_input":"2022-03-09T04:15:32.618159Z","iopub.status.idle":"2022-03-09T04:15:33.080066Z","shell.execute_reply.started":"2022-03-09T04:15:32.618127Z","shell.execute_reply":"2022-03-09T04:15:33.078809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#making a feed forward neural network\nmodel = Sequential()\n\nmodel.add(Dense(units=256, input_shape=(784,), activation='relu'))\nmodel.add(Dense(units=256, activation='relu'))\nmodel.add(Dense(units=256, activation='relu'))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(units=10, activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T04:15:33.081918Z","iopub.execute_input":"2022-03-09T04:15:33.082436Z","iopub.status.idle":"2022-03-09T04:15:33.255011Z","shell.execute_reply.started":"2022-03-09T04:15:33.082380Z","shell.execute_reply":"2022-03-09T04:15:33.254064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x=X_train, y=y_train, batch_size=120, epochs=50)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T04:15:33.256583Z","iopub.execute_input":"2022-03-09T04:15:33.257752Z","iopub.status.idle":"2022-03-09T04:17:56.796533Z","shell.execute_reply.started":"2022-03-09T04:15:33.257693Z","shell.execute_reply":"2022-03-09T04:17:56.795490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=model.predict(X_test)\npredictions=np.argmax(predictions,axis=1)\nsub=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv', header='infer')\nsub[\"Label\"]=predictions\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T04:17:56.799201Z","iopub.execute_input":"2022-03-09T04:17:56.799833Z","iopub.status.idle":"2022-03-09T04:17:59.886231Z","shell.execute_reply.started":"2022-03-09T04:17:56.799781Z","shell.execute_reply":"2022-03-09T04:17:59.884924Z"},"trusted":true},"execution_count":null,"outputs":[]}]}