{"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-10-21T11:52:07.203639Z","iopub.execute_input":"2022-10-21T11:52:07.204143Z","iopub.status.idle":"2022-10-21T11:52:07.217091Z","shell.execute_reply.started":"2022-10-21T11:52:07.204106Z","shell.execute_reply":"2022-10-21T11:52:07.215488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nimport tensorflow as tf\n\ntrain_data = pd.read_csv('../input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('../input/digit-recognizer/test.csv')\n\nX = train_data.drop(['label'],axis=1)\ny = train_data['label']\ntotal_clf = X.shape[1]\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:52:07.222139Z","iopub.execute_input":"2022-10-21T11:52:07.223409Z","iopub.status.idle":"2022-10-21T11:52:11.404032Z","shell.execute_reply.started":"2022-10-21T11:52:07.223366Z","shell.execute_reply":"2022-10-21T11:52:11.402734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train = (train_data.iloc[:, 1:].values).astype('float32')\ny_train = (train_data.iloc[:, 0].values).astype('int32')","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:52:11.406967Z","iopub.execute_input":"2022-10-21T11:52:11.407523Z","iopub.status.idle":"2022-10-21T11:52:11.485547Z","shell.execute_reply.started":"2022-10-21T11:52:11.407483Z","shell.execute_reply":"2022-10-21T11:52:11.484177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nX_reshaped = X_train.reshape(X_train.shape[0], 28, 28)\n\nX_train = X_train/255\n\nplt.figure()\nplt.imshow(X_reshaped[100], cmap=plt.get_cmap('gray'))\nplt.title(y_train[100]);\nplt.colorbar()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:52:11.487152Z","iopub.execute_input":"2022-10-21T11:52:11.487496Z","iopub.status.idle":"2022-10-21T11:52:11.835242Z","shell.execute_reply.started":"2022-10-21T11:52:11.487464Z","shell.execute_reply":"2022-10-21T11:52:11.833882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = (test_data.iloc[:,0:].values).astype('float32')\n\nX_test = X_test/255\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:52:11.837601Z","iopub.execute_input":"2022-10-21T11:52:11.838515Z","iopub.status.idle":"2022-10-21T11:52:11.917292Z","shell.execute_reply.started":"2022-10-21T11:52:11.838479Z","shell.execute_reply":"2022-10-21T11:52:11.915867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers\n\ninput_shape = X.shape[1]\n\nmodel = keras.Sequential([\n    \n    layers.Flatten(input_shape=[input_shape]),\n    \n    layers.Dense(256,activation = 'relu'),\n    layers.Dropout(0.3),\n    layers.BatchNormalization(),\n    \n    layers.Dense(total_clf,activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:52:11.919092Z","iopub.execute_input":"2022-10-21T11:52:11.919442Z","iopub.status.idle":"2022-10-21T11:52:11.979988Z","shell.execute_reply.started":"2022-10-21T11:52:11.919412Z","shell.execute_reply":"2022-10-21T11:52:11.978344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:52:11.981840Z","iopub.execute_input":"2022-10-21T11:52:11.982188Z","iopub.status.idle":"2022-10-21T11:52:11.988622Z","shell.execute_reply.started":"2022-10-21T11:52:11.982159Z","shell.execute_reply":"2022-10-21T11:52:11.987399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\nearly_stopping = EarlyStopping(patience=10,\n                                    min_delta=0.001,\n                                    restore_best_weights=True)\n\nmodel.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])\n\n\nhistory = model.fit(X_train,y_train,epochs=250,callbacks=early_stopping)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T11:52:29.693172Z","iopub.execute_input":"2022-10-21T11:52:29.694512Z","iopub.status.idle":"2022-10-21T12:18:51.640130Z","shell.execute_reply.started":"2022-10-21T11:52:29.694458Z","shell.execute_reply":"2022-10-21T12:18:51.638860Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['loss'])\nplt.title('Accuracy per epochs')\nplt.xlabel('epochs')\nplt.ylabel('accuracy / loss')\nplt.legend(['acc','loss'],)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T12:20:38.685556Z","iopub.execute_input":"2022-10-21T12:20:38.686667Z","iopub.status.idle":"2022-10-21T12:20:38.890272Z","shell.execute_reply.started":"2022-10-21T12:20:38.686624Z","shell.execute_reply":"2022-10-21T12:20:38.888816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('model_digrec_1Node_.h5')","metadata":{"execution":{"iopub.status.busy":"2022-10-21T12:20:56.424327Z","iopub.execute_input":"2022-10-21T12:20:56.425786Z","iopub.status.idle":"2022-10-21T12:20:56.466103Z","shell.execute_reply.started":"2022-10-21T12:20:56.425738Z","shell.execute_reply":"2022-10-21T12:20:56.464740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(X_test)\nresults = np.argmax(results,axis = 1)\nresults = pd.Series(results,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T12:20:59.944261Z","iopub.execute_input":"2022-10-21T12:20:59.945306Z","iopub.status.idle":"2022-10-21T12:21:01.730621Z","shell.execute_reply.started":"2022-10-21T12:20:59.945263Z","shell.execute_reply":"2022-10-21T12:21:01.729541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('./submission.csv').head()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T12:21:04.917586Z","iopub.execute_input":"2022-10-21T12:21:04.918548Z","iopub.status.idle":"2022-10-21T12:21:04.937776Z","shell.execute_reply.started":"2022-10-21T12:21:04.918493Z","shell.execute_reply":"2022-10-21T12:21:04.936333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}