{"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-07-06T09:05:58.446070Z","iopub.execute_input":"2022-07-06T09:05:58.447213Z","iopub.status.idle":"2022-07-06T09:05:58.484273Z","shell.execute_reply.started":"2022-07-06T09:05:58.447088Z","shell.execute_reply":"2022-07-06T09:05:58.482747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:05:58.489150Z","iopub.execute_input":"2022-07-06T09:05:58.490371Z","iopub.status.idle":"2022-07-06T09:06:04.957335Z","shell.execute_reply.started":"2022-07-06T09:05:58.490318Z","shell.execute_reply":"2022-07-06T09:06:04.955800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Conv2D, MaxPool2D,MaxPooling2D,Dropout,Flatten\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.utils import to_categorical\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:04.959634Z","iopub.execute_input":"2022-07-06T09:06:04.960388Z","iopub.status.idle":"2022-07-06T09:06:14.966387Z","shell.execute_reply.started":"2022-07-06T09:06:04.960336Z","shell.execute_reply":"2022-07-06T09:06:14.965327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:14.967544Z","iopub.execute_input":"2022-07-06T09:06:14.968091Z","iopub.status.idle":"2022-07-06T09:06:14.974901Z","shell.execute_reply.started":"2022-07-06T09:06:14.968061Z","shell.execute_reply":"2022-07-06T09:06:14.973491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:14.978050Z","iopub.execute_input":"2022-07-06T09:06:14.978498Z","iopub.status.idle":"2022-07-06T09:06:15.042986Z","shell.execute_reply.started":"2022-07-06T09:06:14.978455Z","shell.execute_reply":"2022-07-06T09:06:15.041722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:15.044286Z","iopub.execute_input":"2022-07-06T09:06:15.044618Z","iopub.status.idle":"2022-07-06T09:06:15.064562Z","shell.execute_reply.started":"2022-07-06T09:06:15.044588Z","shell.execute_reply":"2022-07-06T09:06:15.063454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.iloc[:,1:]\ny_train = train['label']\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:15.066467Z","iopub.execute_input":"2022-07-06T09:06:15.066777Z","iopub.status.idle":"2022-07-06T09:06:15.076723Z","shell.execute_reply.started":"2022-07-06T09:06:15.066748Z","shell.execute_reply":"2022-07-06T09:06:15.075564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:24.524992Z","iopub.execute_input":"2022-07-06T09:06:24.525388Z","iopub.status.idle":"2022-07-06T09:06:24.551591Z","shell.execute_reply.started":"2022-07-06T09:06:24.525353Z","shell.execute_reply":"2022-07-06T09:06:24.550475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape,y_train.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:25.083263Z","iopub.execute_input":"2022-07-06T09:06:25.083656Z","iopub.status.idle":"2022-07-06T09:06:25.090875Z","shell.execute_reply.started":"2022-07-06T09:06:25.083622Z","shell.execute_reply":"2022-07-06T09:06:25.089769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:26.430228Z","iopub.execute_input":"2022-07-06T09:06:26.431047Z","iopub.status.idle":"2022-07-06T09:06:26.510194Z","shell.execute_reply.started":"2022-07-06T09:06:26.430998Z","shell.execute_reply":"2022-07-06T09:06:26.509258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test/255\ntest = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:31.888864Z","iopub.execute_input":"2022-07-06T09:06:31.889257Z","iopub.status.idle":"2022-07-06T09:06:31.949767Z","shell.execute_reply.started":"2022-07-06T09:06:31.889222Z","shell.execute_reply":"2022-07-06T09:06:31.948794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_hot = to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:32.573758Z","iopub.execute_input":"2022-07-06T09:06:32.574166Z","iopub.status.idle":"2022-07-06T09:06:32.579433Z","shell.execute_reply.started":"2022-07-06T09:06:32.574135Z","shell.execute_reply":"2022-07-06T09:06:32.578421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape,y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:37.527593Z","iopub.execute_input":"2022-07-06T09:06:37.528314Z","iopub.status.idle":"2022-07-06T09:06:37.535653Z","shell.execute_reply.started":"2022-07-06T09:06:37.528277Z","shell.execute_reply":"2022-07-06T09:06:37.534667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel = Sequential()\nmodel.add(Conv2D(32,(5,5),activation='relu',input_shape = (28,28,1)))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Conv2D(32,(5,5),activation='relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n# Adding a flatting layer\nmodel.add(Flatten())\n# Adding layers\nmodel.add(Dense(64,activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(64,activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation = 'softmax'))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:42.063560Z","iopub.execute_input":"2022-07-06T09:06:42.063985Z","iopub.status.idle":"2022-07-06T09:06:42.406068Z","shell.execute_reply.started":"2022-07-06T09:06:42.063951Z","shell.execute_reply":"2022-07-06T09:06:42.404881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'categorical_crossentropy',metrics = ['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:44.050871Z","iopub.execute_input":"2022-07-06T09:06:44.051777Z","iopub.status.idle":"2022-07-06T09:06:44.067866Z","shell.execute_reply.started":"2022-07-06T09:06:44.051727Z","shell.execute_reply":"2022-07-06T09:06:44.066467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(X_train,y_train_hot,epochs = 50,validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:06:56.891193Z","iopub.execute_input":"2022-07-06T09:06:56.891622Z","iopub.status.idle":"2022-07-06T09:16:29.885481Z","shell.execute_reply.started":"2022-07-06T09:06:56.891585Z","shell.execute_reply":"2022-07-06T09:16:29.884216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:16:58.931013Z","iopub.execute_input":"2022-07-06T09:16:58.931529Z","iopub.status.idle":"2022-07-06T09:16:58.937983Z","shell.execute_reply.started":"2022-07-06T09:16:58.931484Z","shell.execute_reply":"2022-07-06T09:16:58.936818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(hist.history['accuracy'])\nplt.plot(hist.history['val_accuracy'])\nplt.title(\"Model Accuracy\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel('Epochs')\nplt.legend(['Train','Val'],loc = 'upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:17:02.769784Z","iopub.execute_input":"2022-07-06T09:17:02.770496Z","iopub.status.idle":"2022-07-06T09:17:03.003990Z","shell.execute_reply.started":"2022-07-06T09:17:02.770443Z","shell.execute_reply":"2022-07-06T09:17:03.002908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_train,y_train_hot)[1]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:17:39.355690Z","iopub.execute_input":"2022-07-06T09:17:39.356056Z","iopub.status.idle":"2022-07-06T09:17:44.268026Z","shell.execute_reply.started":"2022-07-06T09:17:39.356025Z","shell.execute_reply":"2022-07-06T09:17:44.266690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\nans = tf.keras.backend.argmax(model.predict(test))\nsubmission['Label'] = ans\nsubmission.to_csv('AX.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T09:17:53.714378Z","iopub.execute_input":"2022-07-06T09:17:53.715329Z","iopub.status.idle":"2022-07-06T09:17:56.790637Z","shell.execute_reply.started":"2022-07-06T09:17:53.715273Z","shell.execute_reply":"2022-07-06T09:17:56.789520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}