{"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-02-05T15:29:02.346694Z","iopub.execute_input":"2022-02-05T15:29:02.347438Z","iopub.status.idle":"2022-02-05T15:29:02.380899Z","shell.execute_reply.started":"2022-02-05T15:29:02.347310Z","shell.execute_reply":"2022-02-05T15:29:02.380048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import layers\nfrom keras.preprocessing.image import img_to_array, load_img, array_to_img\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Flatten, Dense\nfrom keras.callbacks import EarlyStopping\nfrom sklearn.model_selection import train_test_split\n\nimport keras\nimport tensorflow as tf\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:02.382945Z","iopub.execute_input":"2022-02-05T15:29:02.383445Z","iopub.status.idle":"2022-02-05T15:29:09.474741Z","shell.execute_reply.started":"2022-02-05T15:29:02.383401Z","shell.execute_reply":"2022-02-05T15:29:09.473879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv(\"../input/digit-recognizer/test.csv\")\ntrain=pd.read_csv(\"../input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:09.475940Z","iopub.execute_input":"2022-02-05T15:29:09.476218Z","iopub.status.idle":"2022-02-05T15:29:15.438932Z","shell.execute_reply.started":"2022-02-05T15:29:09.476180Z","shell.execute_reply":"2022-02-05T15:29:15.438028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Prepagation","metadata":{}},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(train.drop(\"label\",axis=1),train.label)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:15.440400Z","iopub.execute_input":"2022-02-05T15:29:15.441218Z","iopub.status.idle":"2022-02-05T15:29:15.843310Z","shell.execute_reply.started":"2022-02-05T15:29:15.441178Z","shell.execute_reply":"2022-02-05T15:29:15.842450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train.to_numpy().reshape((x_train.shape[0],28,28,1))\nx_test=x_test.to_numpy().reshape((x_test.shape[0],28,28,1))\n\ny_train = tf.keras.utils.to_categorical(y_train, num_classes=10)\ny_test = tf.keras.utils.to_categorical(y_test, num_classes=10)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:15.845449Z","iopub.execute_input":"2022-02-05T15:29:15.846052Z","iopub.status.idle":"2022-02-05T15:29:15.854979Z","shell.execute_reply.started":"2022-02-05T15:29:15.846005Z","shell.execute_reply":"2022-02-05T15:29:15.853867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:15.856241Z","iopub.execute_input":"2022-02-05T15:29:15.856477Z","iopub.status.idle":"2022-02-05T15:29:15.868366Z","shell.execute_reply.started":"2022-02-05T15:29:15.856446Z","shell.execute_reply":"2022-02-05T15:29:15.867310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\nx_train /= 255\nx_test /= 255","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:15.869449Z","iopub.execute_input":"2022-02-05T15:29:15.870160Z","iopub.status.idle":"2022-02-05T15:29:15.949777Z","shell.execute_reply.started":"2022-02-05T15:29:15.870119Z","shell.execute_reply":"2022-02-05T15:29:15.949091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Setting Up The Model","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32,(3,3),input_shape = (28,28,1)))\nmodel.add(Activation(\"relu\"))\nmodel.add(MaxPooling2D())\n\nmodel.add(Conv2D(64,(3,3)))\nmodel.add(Activation(\"relu\"))\nmodel.add(MaxPooling2D())\n\nmodel.add(Flatten())\n\nmodel.add(Dense(512))\nmodel.add(Activation(\"relu\"))\n\nmodel.add(Dropout(0.5)) # helps prevent overfitting\n\nmodel.add(Dense(10)) # output\nmodel.add(Activation(\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:15.950851Z","iopub.execute_input":"2022-02-05T15:29:15.951208Z","iopub.status.idle":"2022-02-05T15:29:16.125196Z","shell.execute_reply.started":"2022-02-05T15:29:15.951172Z","shell.execute_reply":"2022-02-05T15:29:16.124167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = \"categorical_crossentropy\",\n              optimizer = \"rmsprop\",\n              metrics = [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:16.126521Z","iopub.execute_input":"2022-02-05T15:29:16.126742Z","iopub.status.idle":"2022-02-05T15:29:16.142937Z","shell.execute_reply.started":"2022-02-05T15:29:16.126714Z","shell.execute_reply":"2022-02-05T15:29:16.142009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = EarlyStopping(monitor='val_loss', patience=10, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:16.146454Z","iopub.execute_input":"2022-02-05T15:29:16.146703Z","iopub.status.idle":"2022-02-05T15:29:16.151151Z","shell.execute_reply.started":"2022-02-05T15:29:16.146675Z","shell.execute_reply":"2022-02-05T15:29:16.149974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_model=model.fit(x_train,y_train,\n                         validation_data=(x_test,y_test),\n    callbacks=callback)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:16.152579Z","iopub.execute_input":"2022-02-05T15:29:16.152917Z","iopub.status.idle":"2022-02-05T15:29:35.340986Z","shell.execute_reply.started":"2022-02-05T15:29:16.152855Z","shell.execute_reply":"2022-02-05T15:29:35.340109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Taking a Look of Results and Predictions","metadata":{}},{"cell_type":"code","source":"results=learning_model.model.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:35.342466Z","iopub.execute_input":"2022-02-05T15:29:35.343726Z","iopub.status.idle":"2022-02-05T15:29:37.089080Z","shell.execute_reply.started":"2022-02-05T15:29:35.343677Z","shell.execute_reply":"2022-02-05T15:29:37.088194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_model.history[\"accuracy\"]","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:37.090375Z","iopub.execute_input":"2022-02-05T15:29:37.090946Z","iopub.status.idle":"2022-02-05T15:29:37.100388Z","shell.execute_reply.started":"2022-02-05T15:29:37.090906Z","shell.execute_reply":"2022-02-05T15:29:37.099597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results[1]","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:37.103491Z","iopub.execute_input":"2022-02-05T15:29:37.103946Z","iopub.status.idle":"2022-02-05T15:29:37.111637Z","shell.execute_reply.started":"2022-02-05T15:29:37.103899Z","shell.execute_reply":"2022-02-05T15:29:37.110702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predicts=learning_model.model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:37.113108Z","iopub.execute_input":"2022-02-05T15:29:37.113354Z","iopub.status.idle":"2022-02-05T15:29:37.121745Z","shell.execute_reply.started":"2022-02-05T15:29:37.113323Z","shell.execute_reply":"2022-02-05T15:29:37.120753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Data Prepagation for Filling Submission Data","metadata":{}},{"cell_type":"code","source":"test=test.to_numpy().reshape((test.shape[0],28,28,1))","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:37.123433Z","iopub.execute_input":"2022-02-05T15:29:37.123831Z","iopub.status.idle":"2022-02-05T15:29:37.133622Z","shell.execute_reply.started":"2022-02-05T15:29:37.123800Z","shell.execute_reply":"2022-02-05T15:29:37.132937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicts_test=learning_model.model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:37.134950Z","iopub.execute_input":"2022-02-05T15:29:37.135620Z","iopub.status.idle":"2022-02-05T15:29:41.268816Z","shell.execute_reply.started":"2022-02-05T15:29:37.135583Z","shell.execute_reply":"2022-02-05T15:29:41.267781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predicts_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:41.270039Z","iopub.execute_input":"2022-02-05T15:29:41.270306Z","iopub.status.idle":"2022-02-05T15:29:41.279363Z","shell.execute_reply.started":"2022-02-05T15:29:41.270275Z","shell.execute_reply":"2022-02-05T15:29:41.278572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,len(predicts_test)):\n    predicts_test[i]=np.argmax(predicts_test[i])","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:41.280902Z","iopub.execute_input":"2022-02-05T15:29:41.281392Z","iopub.status.idle":"2022-02-05T15:29:41.406390Z","shell.execute_reply.started":"2022-02-05T15:29:41.281357Z","shell.execute_reply":"2022-02-05T15:29:41.405469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data=pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:41.407747Z","iopub.execute_input":"2022-02-05T15:29:41.408453Z","iopub.status.idle":"2022-02-05T15:29:41.431947Z","shell.execute_reply.started":"2022-02-05T15:29:41.408393Z","shell.execute_reply":"2022-02-05T15:29:41.431209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data.Label=predicts_test","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:41.433765Z","iopub.execute_input":"2022-02-05T15:29:41.434465Z","iopub.status.idle":"2022-02-05T15:29:41.440602Z","shell.execute_reply.started":"2022-02-05T15:29:41.434416Z","shell.execute_reply":"2022-02-05T15:29:41.439953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data.Label=submission_data.Label.astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:41.442405Z","iopub.execute_input":"2022-02-05T15:29:41.442987Z","iopub.status.idle":"2022-02-05T15:29:41.453144Z","shell.execute_reply.started":"2022-02-05T15:29:41.442937Z","shell.execute_reply":"2022-02-05T15:29:41.452394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization to See the Difference Between Actual and Predicted Numbers","metadata":{}},{"cell_type":"code","source":"submission_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:41.454378Z","iopub.execute_input":"2022-02-05T15:29:41.455343Z","iopub.status.idle":"2022-02-05T15:29:41.473890Z","shell.execute_reply.started":"2022-02-05T15:29:41.455271Z","shell.execute_reply":"2022-02-05T15:29:41.473092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.subplots(2,5,figsize=(16,9))\nfor i in range(1,11):\n    plt.subplot(2,5,i)\n    plt.imshow(array_to_img(test[i-1]),cmap='binary',  interpolation='nearest')\n    plt.xlabel(f\"Predicted Number : {submission_data.Label[i-1]}\")\n    plt.title(\"Actual Number\")\n","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:41.475230Z","iopub.execute_input":"2022-02-05T15:29:41.475780Z","iopub.status.idle":"2022-02-05T15:29:42.881797Z","shell.execute_reply.started":"2022-02-05T15:29:41.475741Z","shell.execute_reply":"2022-02-05T15:29:42.880862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving the Submission Data to Submit","metadata":{}},{"cell_type":"code","source":"submission_data=submission_data.set_index(\"ImageId\")","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:29:42.883000Z","iopub.execute_input":"2022-02-05T15:29:42.883284Z","iopub.status.idle":"2022-02-05T15:29:42.889385Z","shell.execute_reply.started":"2022-02-05T15:29:42.883251Z","shell.execute_reply":"2022-02-05T15:29:42.888471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-05T15:49:48.229482Z","iopub.execute_input":"2022-02-05T15:49:48.229843Z","iopub.status.idle":"2022-02-05T15:49:48.294932Z","shell.execute_reply.started":"2022-02-05T15:49:48.229799Z","shell.execute_reply":"2022-02-05T15:49:48.293899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}