{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30043,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2024-01-30T02:21:34.199037Z","iopub.execute_input":"2024-01-30T02:21:34.199391Z","iopub.status.idle":"2024-01-30T02:21:34.209825Z","shell.execute_reply.started":"2024-01-30T02:21:34.199361Z","shell.execute_reply":"2024-01-30T02:21:34.208540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom keras.models import Sequential\nfrom keras.optimizers import RMSprop\nfrom keras.utils.np_utils import to_categorical\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\n%matplotlib inline","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-01-30T02:21:34.219289Z","iopub.execute_input":"2024-01-30T02:21:34.219636Z","iopub.status.idle":"2024-01-30T02:21:40.517451Z","shell.execute_reply.started":"2024-01-30T02:21:34.219597Z","shell.execute_reply":"2024-01-30T02:21:40.516514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/digit-recognizer/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:40.519629Z","iopub.execute_input":"2024-01-30T02:21:40.519940Z","iopub.status.idle":"2024-01-30T02:21:44.427319Z","shell.execute_reply.started":"2024-01-30T02:21:40.519910Z","shell.execute_reply":"2024-01-30T02:21:44.426473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:44.428744Z","iopub.execute_input":"2024-01-30T02:21:44.429075Z","iopub.status.idle":"2024-01-30T02:21:44.434300Z","shell.execute_reply.started":"2024-01-30T02:21:44.429044Z","shell.execute_reply":"2024-01-30T02:21:44.433384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:44.436224Z","iopub.execute_input":"2024-01-30T02:21:44.436682Z","iopub.status.idle":"2024-01-30T02:21:44.451712Z","shell.execute_reply.started":"2024-01-30T02:21:44.436636Z","shell.execute_reply":"2024-01-30T02:21:44.450771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,4))\nsns.countplot(train_df['label'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:44.455094Z","iopub.execute_input":"2024-01-30T02:21:44.455427Z","iopub.status.idle":"2024-01-30T02:21:44.666367Z","shell.execute_reply.started":"2024-01-30T02:21:44.455396Z","shell.execute_reply":"2024-01-30T02:21:44.665452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_df.drop(labels = ['label'], axis = 1)\nx_train.head(1)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:44.669809Z","iopub.execute_input":"2024-01-30T02:21:44.670282Z","iopub.status.idle":"2024-01-30T02:21:44.772285Z","shell.execute_reply.started":"2024-01-30T02:21:44.670231Z","shell.execute_reply":"2024-01-30T02:21:44.771168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_df['label']\ny_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:44.774529Z","iopub.execute_input":"2024-01-30T02:21:44.775134Z","iopub.status.idle":"2024-01-30T02:21:44.785313Z","shell.execute_reply.started":"2024-01-30T02:21:44.774970Z","shell.execute_reply":"2024-01-30T02:21:44.784234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Preprocessing**","metadata":{}},{"cell_type":"code","source":"x_train = x_train.values.reshape(-1, 28, 28, 1)\nx_train = x_train / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:44.786711Z","iopub.execute_input":"2024-01-30T02:21:44.787045Z","iopub.status.idle":"2024-01-30T02:21:44.899263Z","shell.execute_reply.started":"2024-01-30T02:21:44.787013Z","shell.execute_reply":"2024-01-30T02:21:44.898198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:44.900656Z","iopub.execute_input":"2024-01-30T02:21:44.901003Z","iopub.status.idle":"2024-01-30T02:21:44.906505Z","shell.execute_reply.started":"2024-01-30T02:21:44.900968Z","shell.execute_reply":"2024-01-30T02:21:44.905512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_train[5][:,:,0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:44.908630Z","iopub.execute_input":"2024-01-30T02:21:44.909072Z","iopub.status.idle":"2024-01-30T02:21:45.077304Z","shell.execute_reply.started":"2024-01-30T02:21:44.909027Z","shell.execute_reply":"2024-01-30T02:21:45.076448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[5]","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:45.078414Z","iopub.execute_input":"2024-01-30T02:21:45.078765Z","iopub.status.idle":"2024-01-30T02:21:45.086421Z","shell.execute_reply.started":"2024-01-30T02:21:45.078733Z","shell.execute_reply":"2024-01-30T02:21:45.085305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model Building**","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.5))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))\n\noptimizer = RMSprop(lr=0.01)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:45.088008Z","iopub.execute_input":"2024-01-30T02:21:45.088462Z","iopub.status.idle":"2024-01-30T02:21:47.447191Z","shell.execute_reply.started":"2024-01-30T02:21:45.088418Z","shell.execute_reply":"2024-01-30T02:21:47.446313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Compiling the Model**","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer = 'adam' , loss = \"categorical_crossentropy\", metrics = [\"accuracy\"])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:47.448612Z","iopub.execute_input":"2024-01-30T02:21:47.448961Z","iopub.status.idle":"2024-01-30T02:21:47.471377Z","shell.execute_reply.started":"2024-01-30T02:21:47.448928Z","shell.execute_reply":"2024-01-30T02:21:47.470255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=20\nbatch_size=64\nmodel.fit(x_train,y_train,epochs=epochs,batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:21:47.472725Z","iopub.execute_input":"2024-01-30T02:21:47.473022Z","iopub.status.idle":"2024-01-30T02:22:50.914805Z","shell.execute_reply.started":"2024-01-30T02:21:47.472993Z","shell.execute_reply":"2024-01-30T02:22:50.913411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df= pd.read_csv('../input/digit-recognizer/test.csv')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:50.916905Z","iopub.execute_input":"2024-01-30T02:22:50.917361Z","iopub.status.idle":"2024-01-30T02:22:53.378103Z","shell.execute_reply.started":"2024-01-30T02:22:50.917314Z","shell.execute_reply":"2024-01-30T02:22:53.377078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test_df.values.reshape(-1, 28, 28, 1)\nx_test = x_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:53.379522Z","iopub.execute_input":"2024-01-30T02:22:53.379983Z","iopub.status.idle":"2024-01-30T02:22:53.454584Z","shell.execute_reply.started":"2024-01-30T02:22:53.379939Z","shell.execute_reply":"2024-01-30T02:22:53.453721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:53.455929Z","iopub.execute_input":"2024-01-30T02:22:53.456242Z","iopub.status.idle":"2024-01-30T02:22:54.583497Z","shell.execute_reply.started":"2024-01-30T02:22:53.456211Z","shell.execute_reply":"2024-01-30T02:22:54.582675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_test[0][:,:,0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:54.584884Z","iopub.execute_input":"2024-01-30T02:22:54.585222Z","iopub.status.idle":"2024-01-30T02:22:54.745678Z","shell.execute_reply.started":"2024-01-30T02:22:54.585191Z","shell.execute_reply":"2024-01-30T02:22:54.744797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[0]","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:54.747191Z","iopub.execute_input":"2024-01-30T02:22:54.747607Z","iopub.status.idle":"2024-01-30T02:22:54.754594Z","shell.execute_reply.started":"2024-01-30T02:22:54.747564Z","shell.execute_reply":"2024-01-30T02:22:54.753402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=[np.argmax(y_test1) for y_test1 in y_test]","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:54.756152Z","iopub.execute_input":"2024-01-30T02:22:54.756501Z","iopub.status.idle":"2024-01-30T02:22:54.860148Z","shell.execute_reply.started":"2024-01-30T02:22:54.756470Z","shell.execute_reply":"2024-01-30T02:22:54.859403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[0]","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:54.861329Z","iopub.execute_input":"2024-01-30T02:22:54.861604Z","iopub.status.idle":"2024-01-30T02:22:54.867812Z","shell.execute_reply.started":"2024-01-30T02:22:54.861578Z","shell.execute_reply":"2024-01-30T02:22:54.866851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId' : [i+1 for i in range(len(y_test))] , 'Label' : y_test})","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:54.868982Z","iopub.execute_input":"2024-01-30T02:22:54.869292Z","iopub.status.idle":"2024-01-30T02:22:54.914866Z","shell.execute_reply.started":"2024-01-30T02:22:54.869264Z","shell.execute_reply":"2024-01-30T02:22:54.913777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:54.916502Z","iopub.execute_input":"2024-01-30T02:22:54.917027Z","iopub.status.idle":"2024-01-30T02:22:54.933310Z","shell.execute_reply.started":"2024-01-30T02:22:54.916949Z","shell.execute_reply":"2024-01-30T02:22:54.932199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('./submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T02:22:54.934871Z","iopub.execute_input":"2024-01-30T02:22:54.935308Z","iopub.status.idle":"2024-01-30T02:22:55.352942Z","shell.execute_reply.started":"2024-01-30T02:22:54.935274Z","shell.execute_reply":"2024-01-30T02:22:55.351821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}