{"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-11T06:04:38.980248Z","iopub.execute_input":"2022-03-11T06:04:38.981087Z","iopub.status.idle":"2022-03-11T06:04:38.994919Z","shell.execute_reply.started":"2022-03-11T06:04:38.981043Z","shell.execute_reply":"2022-03-11T06:04:38.993799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf \nfrom tensorflow import keras\nfrom keras.models import Sequential, Model\nfrom keras.layers import Dense, Activation, Flatten\nfrom keras.utils.np_utils import to_categorical\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:04:38.996605Z","iopub.execute_input":"2022-03-11T06:04:38.997062Z","iopub.status.idle":"2022-03-11T06:04:39.003382Z","shell.execute_reply.started":"2022-03-11T06:04:38.997029Z","shell.execute_reply":"2022-03-11T06:04:39.002474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \nData_train=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nData_test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\ny_train=Data_train[\"label\"].values\nX_train=Data_train.iloc[:,1:].values\n\nX_test=Data_test.values","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:04:39.004773Z","iopub.execute_input":"2022-03-11T06:04:39.005056Z","iopub.status.idle":"2022-03-11T06:04:43.466211Z","shell.execute_reply.started":"2022-03-11T06:04:39.005027Z","shell.execute_reply":"2022-03-11T06:04:43.465348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from  sklearn.preprocessing  import OneHotEncoder\nOE = OneHotEncoder()\n# here OE return sparse array therefore converting it to array\ny_train = OE.fit_transform(pd.DataFrame(y_train)).toarray()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:04:43.467832Z","iopub.execute_input":"2022-03-11T06:04:43.468119Z","iopub.status.idle":"2022-03-11T06:04:43.479832Z","shell.execute_reply.started":"2022-03-11T06:04:43.468090Z","shell.execute_reply":"2022-03-11T06:04:43.478985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalization\nX_train = (X_train.astype(np.float32) - 127.5)/127.5\nX_test = (X_test.astype(np.float32) - 127.5)/127.5","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:04:43.480989Z","iopub.execute_input":"2022-03-11T06:04:43.481282Z","iopub.status.idle":"2022-03-11T06:04:43.634804Z","shell.execute_reply.started":"2022-03-11T06:04:43.481251Z","shell.execute_reply":"2022-03-11T06:04:43.633849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLS = Sequential()\nCLS.add(Dense(units=512, input_shape=(784,), activation='relu'))\nCLS.add(Dense(units=256, activation='relu'))\nCLS.add(Dense(units=128, activation='relu'))\nCLS.add(Dense(units=32, activation='sigmoid'))\nCLS.add(Dense(units=10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:04:43.636432Z","iopub.execute_input":"2022-03-11T06:04:43.637009Z","iopub.status.idle":"2022-03-11T06:04:43.695866Z","shell.execute_reply.started":"2022-03-11T06:04:43.636969Z","shell.execute_reply":"2022-03-11T06:04:43.694774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLS.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nCLS.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:04:43.697099Z","iopub.execute_input":"2022-03-11T06:04:43.697362Z","iopub.status.idle":"2022-03-11T06:04:43.711728Z","shell.execute_reply.started":"2022-03-11T06:04:43.697331Z","shell.execute_reply":"2022-03-11T06:04:43.710761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLS.fit(x=X_train, y=y_train, batch_size=120, epochs=30)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:04:43.713201Z","iopub.execute_input":"2022-03-11T06:04:43.713558Z","iopub.status.idle":"2022-03-11T06:06:07.829328Z","shell.execute_reply.started":"2022-03-11T06:04:43.713526Z","shell.execute_reply":"2022-03-11T06:06:07.828008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = CLS.evaluate(X_train, y_train)\nprint(f\"Loss: {metrics[0]}\")\nprint(f\"Accuracy: {metrics[1]}\")","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:06:07.831275Z","iopub.execute_input":"2022-03-11T06:06:07.831613Z","iopub.status.idle":"2022-03-11T06:06:12.427842Z","shell.execute_reply.started":"2022-03-11T06:06:07.831569Z","shell.execute_reply":"2022-03-11T06:06:12.426632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = CLS.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:06:12.430062Z","iopub.execute_input":"2022-03-11T06:06:12.430276Z","iopub.status.idle":"2022-03-11T06:06:14.602664Z","shell.execute_reply.started":"2022-03-11T06:06:12.430250Z","shell.execute_reply":"2022-03-11T06:06:14.601844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nprd = np.argmax(predictions,axis=1) \nprint(prd)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:06:14.604976Z","iopub.execute_input":"2022-03-11T06:06:14.605326Z","iopub.status.idle":"2022-03-11T06:06:14.623589Z","shell.execute_reply.started":"2022-03-11T06:06:14.605269Z","shell.execute_reply":"2022-03-11T06:06:14.622963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsub[\"Label\"]=prd\nsub.to_csv('sub.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:06:14.624860Z","iopub.execute_input":"2022-03-11T06:06:14.625206Z","iopub.status.idle":"2022-03-11T06:06:14.685402Z","shell.execute_reply.started":"2022-03-11T06:06:14.625177Z","shell.execute_reply":"2022-03-11T06:06:14.684388Z"},"trusted":true},"execution_count":null,"outputs":[]}]}