{"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":"2023-03-18T12:10:36.774772Z","iopub.execute_input":"2023-03-18T12:10:36.775136Z","iopub.status.idle":"2023-03-18T12:10:36.799450Z","shell.execute_reply.started":"2023-03-18T12:10:36.775059Z","shell.execute_reply":"2023-03-18T12:10:36.798541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf_test=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:19:19.284591Z","iopub.execute_input":"2023-03-18T12:19:19.284961Z","iopub.status.idle":"2023-03-18T12:19:22.864771Z","shell.execute_reply.started":"2023-03-18T12:19:19.284924Z","shell.execute_reply":"2023-03-18T12:19:22.863832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixel_data=df_train.iloc[:,1:785] #from col 1 to 785\nlabel=df_train[\"label\"]\n\nx_train=np.array(pixel_data)\nx_train=x_train.reshape(len(pixel_data),28,28,1)\n\nx_train=x_train/255.0\nx_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:10:57.802167Z","iopub.execute_input":"2023-03-18T12:10:57.802517Z","iopub.status.idle":"2023-03-18T12:10:58.011491Z","shell.execute_reply.started":"2023-03-18T12:10:57.802485Z","shell.execute_reply":"2023-03-18T12:10:58.010554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = df_train.iloc[:,0]\ny_true=np.array(y_true)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:11:07.961024Z","iopub.execute_input":"2023-03-18T12:11:07.961377Z","iopub.status.idle":"2023-03-18T12:11:07.966642Z","shell.execute_reply.started":"2023-03-18T12:11:07.961347Z","shell.execute_reply":"2023-03-18T12:11:07.965446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixel_data=df_test.iloc[:,:] #from col 1 to 785\nnumber1=np.array(pixel_data)\nnumber1=number1.reshape(len(pixel_data),28,28,1)\ndftest=number1\ndftest.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:19:42.957294Z","iopub.execute_input":"2023-03-18T12:19:42.957667Z","iopub.status.idle":"2023-03-18T12:19:43.029520Z","shell.execute_reply.started":"2023-03-18T12:19:42.957635Z","shell.execute_reply":"2023-03-18T12:19:43.027926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import np_utils\ny_train=np_utils.to_categorical(df_train['label'])\n\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:11:14.732381Z","iopub.execute_input":"2023-03-18T12:11:14.732744Z","iopub.status.idle":"2023-03-18T12:11:20.423850Z","shell.execute_reply.started":"2023-03-18T12:11:14.732714Z","shell.execute_reply":"2023-03-18T12:11:20.422101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPool2D\nfrom keras.layers import Dense, BatchNormalization,Dropout,Flatten,Activation,AveragePooling2D,Conv2D\nfrom keras import Model\nimport keras","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:11:36.762710Z","iopub.execute_input":"2023-03-18T12:11:36.764016Z","iopub.status.idle":"2023-03-18T12:11:36.769945Z","shell.execute_reply.started":"2023-03-18T12:11:36.763966Z","shell.execute_reply":"2023-03-18T12:11:36.768936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=6, kernel_size=(3,3),input_shape=(28, 28, 1)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(AveragePooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters=16, kernel_size=(3,3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(AveragePooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters=120, kernel_size=(3,3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Flatten())\nmodel.add(Dense(84))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(10))\nmodel.add(BatchNormalization())\nmodel.add(Activation('softmax'))\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nhistory=model.fit(x_train, y_train, epochs=50)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:11:41.185315Z","iopub.execute_input":"2023-03-18T12:11:41.185657Z","iopub.status.idle":"2023-03-18T12:18:06.687777Z","shell.execute_reply.started":"2023-03-18T12:11:41.185625Z","shell.execute_reply":"2023-03-18T12:18:06.686836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nresult = model.predict(dftest)\n\npredicted_val=[]\nfor i in range(len(result)):\n    predicted_val.append(np.argmax(result[i]))","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:19:51.277462Z","iopub.execute_input":"2023-03-18T12:19:51.277827Z","iopub.status.idle":"2023-03-18T12:19:53.843318Z","shell.execute_reply.started":"2023-03-18T12:19:51.277777Z","shell.execute_reply":"2023-03-18T12:19:53.842149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:20:31.453523Z","iopub.execute_input":"2023-03-18T12:20:31.453899Z","iopub.status.idle":"2023-03-18T12:20:31.460615Z","shell.execute_reply.started":"2023-03-18T12:20:31.453866Z","shell.execute_reply":"2023-03-18T12:20:31.459674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame()\n\nimgid=[]\nfor i in range(1, len(predicted_val)+1):\n    imgid.append(i)\n    \nsubmission[\"Imageid\"]=imgid\nsubmission[\"Label\"]=predicted_val\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:20:42.156274Z","iopub.execute_input":"2023-03-18T12:20:42.157034Z","iopub.status.idle":"2023-03-18T12:20:42.210752Z","shell.execute_reply.started":"2023-03-18T12:20:42.156997Z","shell.execute_reply":"2023-03-18T12:20:42.209907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)\nprint(\"Submission Done\")","metadata":{"execution":{"iopub.status.busy":"2023-03-18T12:20:45.589266Z","iopub.execute_input":"2023-03-18T12:20:45.589618Z","iopub.status.idle":"2023-03-18T12:20:45.621733Z","shell.execute_reply.started":"2023-03-18T12:20:45.589588Z","shell.execute_reply":"2023-03-18T12:20:45.620832Z"},"trusted":true},"execution_count":null,"outputs":[]}]}