{"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-01-20T08:22:39.388484Z","iopub.execute_input":"2023-01-20T08:22:39.388917Z","iopub.status.idle":"2023-01-20T08:22:39.397085Z","shell.execute_reply.started":"2023-01-20T08:22:39.388883Z","shell.execute_reply":"2023-01-20T08:22:39.395828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing libraries\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n# import warnings\nimport warnings\n# filter warnings\nwarnings.filterwarnings('ignore')\nimport random","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:39.426905Z","iopub.execute_input":"2023-01-20T08:22:39.427406Z","iopub.status.idle":"2023-01-20T08:22:39.433939Z","shell.execute_reply.started":"2023-01-20T08:22:39.427363Z","shell.execute_reply":"2023-01-20T08:22:39.432250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Loading the Data**","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nsubmission_df = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:39.474502Z","iopub.execute_input":"2023-01-20T08:22:39.475067Z","iopub.status.idle":"2023-01-20T08:22:43.240833Z","shell.execute_reply.started":"2023-01-20T08:22:39.475017Z","shell.execute_reply":"2023-01-20T08:22:43.239440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.243119Z","iopub.execute_input":"2023-01-20T08:22:43.243981Z","iopub.status.idle":"2023-01-20T08:22:43.254394Z","shell.execute_reply.started":"2023-01-20T08:22:43.243933Z","shell.execute_reply":"2023-01-20T08:22:43.253254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.255889Z","iopub.execute_input":"2023-01-20T08:22:43.256354Z","iopub.status.idle":"2023-01-20T08:22:43.270807Z","shell.execute_reply.started":"2023-01-20T08:22:43.256313Z","shell.execute_reply":"2023-01-20T08:22:43.269913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.shape)\nprint(test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.273026Z","iopub.execute_input":"2023-01-20T08:22:43.273938Z","iopub.status.idle":"2023-01-20T08:22:43.282666Z","shell.execute_reply.started":"2023-01-20T08:22:43.273903Z","shell.execute_reply":"2023-01-20T08:22:43.281370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.284073Z","iopub.execute_input":"2023-01-20T08:22:43.284508Z","iopub.status.idle":"2023-01-20T08:22:43.308198Z","shell.execute_reply.started":"2023-01-20T08:22:43.284475Z","shell.execute_reply":"2023-01-20T08:22:43.306973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.309481Z","iopub.execute_input":"2023-01-20T08:22:43.309819Z","iopub.status.idle":"2023-01-20T08:22:43.331491Z","shell.execute_reply.started":"2023-01-20T08:22:43.309791Z","shell.execute_reply":"2023-01-20T08:22:43.329960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Separating Independent(X) and Dependent(Y) varaiables**","metadata":{}},{"cell_type":"code","source":"X = train_df.drop(['label'],axis = 1)\nY = train_df['label']","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.332953Z","iopub.execute_input":"2023-01-20T08:22:43.333419Z","iopub.status.idle":"2023-01-20T08:22:43.449047Z","shell.execute_reply.started":"2023-01-20T08:22:43.333359Z","shell.execute_reply":"2023-01-20T08:22:43.447809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualization of Data**","metadata":{}},{"cell_type":"code","source":"# visualize number of digits classes\nplt.figure(figsize=(12,7))\ng = sns.countplot(Y, palette=\"rainbow\")\nplt.title(\"Number of digit classes\")","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.450389Z","iopub.execute_input":"2023-01-20T08:22:43.450979Z","iopub.status.idle":"2023-01-20T08:22:43.723931Z","shell.execute_reply.started":"2023-01-20T08:22:43.450947Z","shell.execute_reply":"2023-01-20T08:22:43.721948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Number of times value occurs\nY.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.725028Z","iopub.execute_input":"2023-01-20T08:22:43.725382Z","iopub.status.idle":"2023-01-20T08:22:43.736958Z","shell.execute_reply.started":"2023-01-20T08:22:43.725351Z","shell.execute_reply":"2023-01-20T08:22:43.735448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **PROCESSSING OF DATA**\nNORMALIZATION\n\nRESHAPE","metadata":{}},{"cell_type":"code","source":"print(\"shape of X : \",X.shape)\nprint(\"shape of Y : \",Y.shape)\nprint(\"shape of test_df : \",test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.741652Z","iopub.execute_input":"2023-01-20T08:22:43.742047Z","iopub.status.idle":"2023-01-20T08:22:43.748759Z","shell.execute_reply.started":"2023-01-20T08:22:43.742012Z","shell.execute_reply":"2023-01-20T08:22:43.747510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the data\nX = X / 255.0\ntest_df = test_df / 255.0\nprint(\"X shape: \",X.shape)\nprint(\"test shape: \",test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.750845Z","iopub.execute_input":"2023-01-20T08:22:43.751351Z","iopub.status.idle":"2023-01-20T08:22:43.866601Z","shell.execute_reply.started":"2023-01-20T08:22:43.751306Z","shell.execute_reply":"2023-01-20T08:22:43.865661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape\nX = X.values.reshape(-1,28,28,1)\ntest_df = test_df.values.reshape(-1,28,28,1)\nprint(\"X shape: \",X.shape)\nprint(\"test shape: \",test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.867709Z","iopub.execute_input":"2023-01-20T08:22:43.868105Z","iopub.status.idle":"2023-01-20T08:22:43.874601Z","shell.execute_reply.started":"2023-01-20T08:22:43.868072Z","shell.execute_reply":"2023-01-20T08:22:43.873581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Label Encoding \nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nY = to_categorical(Y, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.875935Z","iopub.execute_input":"2023-01-20T08:22:43.876924Z","iopub.status.idle":"2023-01-20T08:22:43.889680Z","shell.execute_reply.started":"2023-01-20T08:22:43.876891Z","shell.execute_reply":"2023-01-20T08:22:43.888612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"shape of Y : \",Y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.891021Z","iopub.execute_input":"2023-01-20T08:22:43.891732Z","iopub.status.idle":"2023-01-20T08:22:43.901583Z","shell.execute_reply.started":"2023-01-20T08:22:43.891694Z","shell.execute_reply":"2023-01-20T08:22:43.900257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = random.randint(0,len(X))\nplt.imshow(X[idx,:,:,:])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:43.903311Z","iopub.execute_input":"2023-01-20T08:22:43.903696Z","iopub.status.idle":"2023-01-20T08:22:44.098822Z","shell.execute_reply.started":"2023-01-20T08:22:43.903662Z","shell.execute_reply":"2023-01-20T08:22:44.097677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Split Train Test Data**","metadata":{}},{"cell_type":"code","source":"# Split the train and the validation set for the fitting\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.1, random_state=4)\nprint(\"x_train shape\",X_train.shape)\nprint(\"x_test shape\",X_test.shape)\nprint(\"y_train shape\",Y_train.shape)\nprint(\"y_test shape\",Y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:44.100076Z","iopub.execute_input":"2023-01-20T08:22:44.100411Z","iopub.status.idle":"2023-01-20T08:22:44.529136Z","shell.execute_reply.started":"2023-01-20T08:22:44.100381Z","shell.execute_reply":"2023-01-20T08:22:44.527796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Some examples\nplt.imshow(X_train[3][:,:,0],cmap='Blues')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:44.531021Z","iopub.execute_input":"2023-01-20T08:22:44.531492Z","iopub.status.idle":"2023-01-20T08:22:44.672568Z","shell.execute_reply.started":"2023-01-20T08:22:44.531434Z","shell.execute_reply":"2023-01-20T08:22:44.670800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[8][:,:,0],cmap='Blues')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:44.675484Z","iopub.execute_input":"2023-01-20T08:22:44.676674Z","iopub.status.idle":"2023-01-20T08:22:44.865542Z","shell.execute_reply.started":"2023-01-20T08:22:44.676610Z","shell.execute_reply":"2023-01-20T08:22:44.864712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **MODELLING (CNN KERAS)**","metadata":{}},{"cell_type":"code","source":"\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\n\n\n\nmodel = Sequential()\n#\nmodel.add(Conv2D(32,(3,3),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n#\nmodel.add(Conv2D(64,(3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\n# fully connected\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:44.866597Z","iopub.execute_input":"2023-01-20T08:22:44.867211Z","iopub.status.idle":"2023-01-20T08:22:44.939628Z","shell.execute_reply.started":"2023-01-20T08:22:44.867180Z","shell.execute_reply":"2023-01-20T08:22:44.938584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer = 'adam', loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:44.940964Z","iopub.execute_input":"2023-01-20T08:22:44.941298Z","iopub.status.idle":"2023-01-20T08:22:44.952499Z","shell.execute_reply.started":"2023-01-20T08:22:44.941269Z","shell.execute_reply":"2023-01-20T08:22:44.951418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#training the model\nmodel.fit(X_train,Y_train, batch_size= 250,epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:22:44.953988Z","iopub.execute_input":"2023-01-20T08:22:44.954350Z","iopub.status.idle":"2023-01-20T08:25:17.913687Z","shell.execute_reply.started":"2023-01-20T08:22:44.954318Z","shell.execute_reply":"2023-01-20T08:25:17.912755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **EVALUATING the model**","metadata":{}},{"cell_type":"code","source":"model.evaluate(X_test,Y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:25:17.914798Z","iopub.execute_input":"2023-01-20T08:25:17.915670Z","iopub.status.idle":"2023-01-20T08:25:18.922604Z","shell.execute_reply.started":"2023-01-20T08:25:17.915632Z","shell.execute_reply":"2023-01-20T08:25:18.921287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This model is giving the accuracy of 98%","metadata":{}},{"cell_type":"markdown","source":"**CONFUSION MATRIX**","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\nY_pred = model.predict(X_test)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(Y_test,axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n\n\n# plot the confusion matrix\nf,ax = plt.subplots(figsize=(8, 8))\nsns.heatmap(confusion_mtx, annot=True, linewidths=0.01,cmap=\"Reds\",linecolor=\"gray\", fmt= '.1f',ax=ax)\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:25:18.926326Z","iopub.execute_input":"2023-01-20T08:25:18.926734Z","iopub.status.idle":"2023-01-20T08:25:20.389067Z","shell.execute_reply.started":"2023-01-20T08:25:18.926697Z","shell.execute_reply":"2023-01-20T08:25:20.387843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" **Checking the prediction of the model**","metadata":{}},{"cell_type":"code","source":"idx2 = random.randint(0,len(Y_test))\nplt.imshow(X_test[idx2,:])\nplt.show()\ny_pred = model.predict(X_test[idx2,:].reshape(1,28,28,1))\n\nclasses = y_pred.argmax(axis=-1)\nclasses","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:26:57.908047Z","iopub.execute_input":"2023-01-20T08:26:57.908464Z","iopub.status.idle":"2023-01-20T08:26:58.164732Z","shell.execute_reply.started":"2023-01-20T08:26:57.908432Z","shell.execute_reply":"2023-01-20T08:26:58.163499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **SUBMISSION**","metadata":{}},{"cell_type":"markdown","source":"Final Predictions****","metadata":{}},{"cell_type":"code","source":"pred = model.predict(test_df)\npredictions = pred.argmax(axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:25:20.641552Z","iopub.execute_input":"2023-01-20T08:25:20.641876Z","iopub.status.idle":"2023-01-20T08:25:25.387136Z","shell.execute_reply.started":"2023-01-20T08:25:20.641849Z","shell.execute_reply":"2023-01-20T08:25:25.386159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:25:25.388373Z","iopub.execute_input":"2023-01-20T08:25:25.388731Z","iopub.status.idle":"2023-01-20T08:25:25.395555Z","shell.execute_reply.started":"2023-01-20T08:25:25.388702Z","shell.execute_reply":"2023-01-20T08:25:25.394454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(\n    {\n        'ImageId': np.arange(1, predictions.shape[0]+1),\n        'Label': predictions\n    }\n)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:25:25.396809Z","iopub.execute_input":"2023-01-20T08:25:25.397422Z","iopub.status.idle":"2023-01-20T08:25:25.414353Z","shell.execute_reply.started":"2023-01-20T08:25:25.397390Z","shell.execute_reply":"2023-01-20T08:25:25.413050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T08:25:25.420737Z","iopub.execute_input":"2023-01-20T08:25:25.421121Z","iopub.status.idle":"2023-01-20T08:25:25.457645Z","shell.execute_reply.started":"2023-01-20T08:25:25.421089Z","shell.execute_reply":"2023-01-20T08:25:25.456661Z"},"trusted":true},"execution_count":null,"outputs":[]}]}