{"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":"2021-05-25T11:47:13.438309Z","iopub.execute_input":"2021-05-25T11:47:13.438974Z","iopub.status.idle":"2021-05-25T11:47:13.449076Z","shell.execute_reply.started":"2021-05-25T11:47:13.438934Z","shell.execute_reply":"2021-05-25T11:47:13.447711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# importing data using pandas\n","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\n\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:47:13.450900Z","iopub.execute_input":"2021-05-25T11:47:13.451212Z","iopub.status.idle":"2021-05-25T11:47:19.477420Z","shell.execute_reply.started":"2021-05-25T11:47:13.451182Z","shell.execute_reply":"2021-05-25T11:47:19.476205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:47:19.479296Z","iopub.execute_input":"2021-05-25T11:47:19.479592Z","iopub.status.idle":"2021-05-25T11:47:19.514965Z","shell.execute_reply.started":"2021-05-25T11:47:19.479563Z","shell.execute_reply":"2021-05-25T11:47:19.513939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Separate data as Training and Test","metadata":{}},{"cell_type":"code","source":"x_train = train.drop(\"label\",axis=1)\ny_train = train[\"label\"]\n\nx_test = test","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:47:19.516608Z","iopub.execute_input":"2021-05-25T11:47:19.516982Z","iopub.status.idle":"2021-05-25T11:47:19.632149Z","shell.execute_reply.started":"2021-05-25T11:47:19.516935Z","shell.execute_reply":"2021-05-25T11:47:19.631233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalising Data:","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Conv2D,Flatten,MaxPooling2D,Dropout,BatchNormalization\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:47:19.633250Z","iopub.execute_input":"2021-05-25T11:47:19.633682Z","iopub.status.idle":"2021-05-25T11:47:26.070856Z","shell.execute_reply.started":"2021-05-25T11:47:19.633649Z","shell.execute_reply":"2021-05-25T11:47:26.069880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(x_train)\nx_test = np.array(x_test)\n\nX_train = x_train.reshape(len(x_train), 28,28,1)\nX_test = x_test.reshape(len(x_test), 28,28,1)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:47:26.072163Z","iopub.execute_input":"2021-05-25T11:47:26.072604Z","iopub.status.idle":"2021-05-25T11:47:26.395852Z","shell.execute_reply.started":"2021-05-25T11:47:26.072571Z","shell.execute_reply":"2021-05-25T11:47:26.394988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = tf.keras.utils.to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:48:05.523317Z","iopub.execute_input":"2021-05-25T11:48:05.523677Z","iopub.status.idle":"2021-05-25T11:48:05.529552Z","shell.execute_reply.started":"2021-05-25T11:48:05.523646Z","shell.execute_reply":"2021-05-25T11:48:05.528372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = 1/255.0","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:48:08.258407Z","iopub.execute_input":"2021-05-25T11:48:08.258776Z","iopub.status.idle":"2021-05-25T11:48:08.263462Z","shell.execute_reply.started":"2021-05-25T11:48:08.258742Z","shell.execute_reply":"2021-05-25T11:48:08.262261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=16,kernel_size=(3,3),activation='relu',input_shape=(28,28,1)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(2,2))\n\n\nmodel.add(Conv2D(filters=32,kernel_size=(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(2,2))\n\nmodel.add(Conv2D(filters=128,kernel_size=(3,3),activation='relu'))\nmodel.add(MaxPooling2D(2,2))\n\nmodel.add(Flatten())\nmodel.add(Dense(64, activation=\"relu\"))\nmodel.add(Dense(10, activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:48:10.603399Z","iopub.execute_input":"2021-05-25T11:48:10.603786Z","iopub.status.idle":"2021-05-25T11:48:10.780713Z","shell.execute_reply.started":"2021-05-25T11:48:10.603752Z","shell.execute_reply":"2021-05-25T11:48:10.779839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=\"adam\",metrics=[\"accuracy\"],loss=\"categorical_crossentropy\")\n\nhistory = model.fit( X_train, y_train, batch_size = 300  , epochs = 50)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T11:48:13.743443Z","iopub.execute_input":"2021-05-25T11:48:13.744166Z","iopub.status.idle":"2021-05-25T11:59:34.182108Z","shell.execute_reply.started":"2021-05-25T11:48:13.744116Z","shell.execute_reply":"2021-05-25T11:59:34.181349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:01:04.784065Z","iopub.execute_input":"2021-05-25T12:01:04.784418Z","iopub.status.idle":"2021-05-25T12:01:09.251599Z","shell.execute_reply.started":"2021-05-25T12:01:04.784388Z","shell.execute_reply":"2021-05-25T12:01:09.250640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = np.argmax(pred, axis= 1)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:01:34.698274Z","iopub.execute_input":"2021-05-25T12:01:34.698642Z","iopub.status.idle":"2021-05-25T12:01:34.704550Z","shell.execute_reply.started":"2021-05-25T12:01:34.698610Z","shell.execute_reply":"2021-05-25T12:01:34.703221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_data = pd.Series(res, name=\"Label\")\nfinal_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:02:13.099733Z","iopub.execute_input":"2021-05-25T12:02:13.100089Z","iopub.status.idle":"2021-05-25T12:02:13.110114Z","shell.execute_reply.started":"2021-05-25T12:02:13.100060Z","shell.execute_reply":"2021-05-25T12:02:13.108839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_file = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),final_data],axis = 1)\nsub_file.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:03:05.899531Z","iopub.execute_input":"2021-05-25T12:03:05.899994Z","iopub.status.idle":"2021-05-25T12:03:05.961986Z","shell.execute_reply.started":"2021-05-25T12:03:05.899954Z","shell.execute_reply":"2021-05-25T12:03:05.960945Z"},"trusted":true},"execution_count":null,"outputs":[]}]}