{"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)\nimport seaborn as sns\nimport matplotlib.pyplot as plt \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\nimport warnings\nwarnings.filterwarnings('ignore')\n\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-08-13T20:11:12.796387Z","iopub.execute_input":"2023-08-13T20:11:12.796842Z","iopub.status.idle":"2023-08-13T20:11:12.808774Z","shell.execute_reply.started":"2023-08-13T20:11:12.796815Z","shell.execute_reply":"2023-08-13T20:11:12.807001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Loading The Data Set","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:15.955202Z","iopub.execute_input":"2023-08-13T19:59:15.955492Z","iopub.status.idle":"2023-08-13T19:59:18.372026Z","shell.execute_reply.started":"2023-08-13T19:59:15.955468Z","shell.execute_reply":"2023-08-13T19:59:18.370770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nprint(test.shape)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:18.373246Z","iopub.execute_input":"2023-08-13T19:59:18.373569Z","iopub.status.idle":"2023-08-13T19:59:19.827701Z","shell.execute_reply.started":"2023-08-13T19:59:18.373539Z","shell.execute_reply":"2023-08-13T19:59:19.826634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train[\"label\"]\nX_train = train.drop(labels = [\"label\"],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:19.829805Z","iopub.execute_input":"2023-08-13T19:59:19.830418Z","iopub.status.idle":"2023-08-13T19:59:19.894792Z","shell.execute_reply.started":"2023-08-13T19:59:19.830388Z","shell.execute_reply":"2023-08-13T19:59:19.893819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,7))\nsns.countplot(Y_train,palette=\"icefire\")\nplt.title(\"Number of Digit Classes\")\nY_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:19.896583Z","iopub.execute_input":"2023-08-13T19:59:19.896980Z","iopub.status.idle":"2023-08-13T19:59:20.157498Z","shell.execute_reply.started":"2023-08-13T19:59:19.896949Z","shell.execute_reply":"2023-08-13T19:59:20.156621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img = X_train.iloc[0].as_matrix()\n#img = img.reshape((28,28))\n#plt.imshow(img,cmap='gray')\n#plt.title(train.iloc[0,0])\n#plt.axis(\"off\")\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:20.158694Z","iopub.execute_input":"2023-08-13T19:59:20.158937Z","iopub.status.idle":"2023-08-13T19:59:20.163137Z","shell.execute_reply.started":"2023-08-13T19:59:20.158915Z","shell.execute_reply":"2023-08-13T19:59:20.162230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img = X_train.iloc[3].as_matrix()\n#img = img.reshape((28,28))\n#plt.imshow(img,cmap='gray')\n#plt.title(train.iloc[3,0])\n#plt.axis(\"off\")\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:20.164290Z","iopub.execute_input":"2023-08-13T19:59:20.164540Z","iopub.status.idle":"2023-08-13T19:59:20.179987Z","shell.execute_reply.started":"2023-08-13T19:59:20.164518Z","shell.execute_reply":"2023-08-13T19:59:20.178556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Normalize,Reshape and Train-Test Split","metadata":{}},{"cell_type":"code","source":"X_train = X_train / 255.0 \ntest = test / 255.0 \nprint(\"X_train shape:\",X_train.shape)\nprint(\"test shape:\",test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:20.181358Z","iopub.execute_input":"2023-08-13T19:59:20.181694Z","iopub.status.idle":"2023-08-13T19:59:20.261194Z","shell.execute_reply.started":"2023-08-13T19:59:20.181665Z","shell.execute_reply":"2023-08-13T19:59:20.260228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\nprint(\"X_train shape:\",X_train.shape)\nprint(\"test shape:\",test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:20.262267Z","iopub.execute_input":"2023-08-13T19:59:20.262566Z","iopub.status.idle":"2023-08-13T19:59:20.267915Z","shell.execute_reply.started":"2023-08-13T19:59:20.262540Z","shell.execute_reply":"2023-08-13T19:59:20.267143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\nY_train = to_categorical(Y_train,num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:20.271461Z","iopub.execute_input":"2023-08-13T19:59:20.271946Z","iopub.status.idle":"2023-08-13T19:59:28.485368Z","shell.execute_reply.started":"2023-08-13T19:59:20.271916Z","shell.execute_reply":"2023-08-13T19:59:28.484533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_val,Y_train,Y_val = train_test_split(X_train,Y_train,test_size = 0.1,random_state = 2)\nprint(\"x_train shape\",X_train.shape)\nprint(\"x_test shape\",X_val.shape)\nprint(\"y_train shape\",Y_train.shape)\nprint(\"y_test shape\",Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:28.486470Z","iopub.execute_input":"2023-08-13T19:59:28.487070Z","iopub.status.idle":"2023-08-13T19:59:28.895236Z","shell.execute_reply.started":"2023-08-13T19:59:28.487043Z","shell.execute_reply":"2023-08-13T19:59:28.893950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[2][:,:,0],cmap=\"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:28.896614Z","iopub.execute_input":"2023-08-13T19:59:28.896916Z","iopub.status.idle":"2023-08-13T19:59:29.066110Z","shell.execute_reply.started":"2023-08-13T19:59:28.896893Z","shell.execute_reply":"2023-08-13T19:59:29.064416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating The Model ","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport itertools\n\n\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPool2D\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:29.067638Z","iopub.execute_input":"2023-08-13T19:59:29.067948Z","iopub.status.idle":"2023-08-13T19:59:29.085422Z","shell.execute_reply.started":"2023-08-13T19:59:29.067921Z","shell.execute_reply":"2023-08-13T19:59:29.084247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 8,kernel_size = (5,5), padding = \"Same\",activation = \"relu\",input_shape=(28,28,1)))\nmodel.add(MaxPool2D(pool_size = (2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 8,kernel_size = (3,3),padding = \"Same\",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-08-13T19:59:29.087049Z","iopub.execute_input":"2023-08-13T19:59:29.087503Z","iopub.status.idle":"2023-08-13T19:59:29.303751Z","shell.execute_reply.started":"2023-08-13T19:59:29.087468Z","shell.execute_reply":"2023-08-13T19:59:29.302159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adam(lr = 0.01,beta_1 = 0.9,beta_2 = 0.999)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:29.305084Z","iopub.execute_input":"2023-08-13T19:59:29.305430Z","iopub.status.idle":"2023-08-13T19:59:29.310949Z","shell.execute_reply.started":"2023-08-13T19:59:29.305402Z","shell.execute_reply":"2023-08-13T19:59:29.309503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = optimizer,loss = \"categorical_crossentropy\",metrics = [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:29.312420Z","iopub.execute_input":"2023-08-13T19:59:29.312750Z","iopub.status.idle":"2023-08-13T19:59:29.335453Z","shell.execute_reply.started":"2023-08-13T19:59:29.312719Z","shell.execute_reply":"2023-08-13T19:59:29.334405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nbatch_size = 250","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:29.336740Z","iopub.execute_input":"2023-08-13T19:59:29.337067Z","iopub.status.idle":"2023-08-13T19:59:29.342006Z","shell.execute_reply.started":"2023-08-13T19:59:29.337036Z","shell.execute_reply":"2023-08-13T19:59:29.341031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\nfeaturewise_center = False,\nsamplewise_center = False,\nfeaturewise_std_normalization = False,\nsamplewise_std_normalization = False,\nzca_whitening = False,\nrotation_range = 0.5,\nzoom_range = 1,\nwidth_shift_range = 0.1,\nheight_shift_range = 0.1,\nhorizontal_flip = False,\nvertical_flip = False)\n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:29.343604Z","iopub.execute_input":"2023-08-13T19:59:29.344263Z","iopub.status.idle":"2023-08-13T19:59:29.396287Z","shell.execute_reply.started":"2023-08-13T19:59:29.344208Z","shell.execute_reply":"2023-08-13T19:59:29.395217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fitting The Model","metadata":{}},{"cell_type":"code","source":"history = model.fit_generator(datagen.flow(X_train,Y_train,batch_size=batch_size),\n                             epochs = epochs,validation_data = (X_val,Y_val),steps_per_epoch = X_train.shape[0] // batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:59:29.397581Z","iopub.execute_input":"2023-08-13T19:59:29.397904Z","iopub.status.idle":"2023-08-13T20:02:29.516731Z","shell.execute_reply.started":"2023-08-13T19:59:29.397875Z","shell.execute_reply":"2023-08-13T20:02:29.515669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization of Results","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history[\"val_loss\"],color=\"b\",label = \"validation_loss\")\nplt.title(\"Test Loss\")\nplt.xlabel(\"Number of Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T20:02:29.517877Z","iopub.execute_input":"2023-08-13T20:02:29.518284Z","iopub.status.idle":"2023-08-13T20:02:29.726741Z","shell.execute_reply.started":"2023-08-13T20:02:29.518248Z","shell.execute_reply":"2023-08-13T20:02:29.725034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation of The Model ","metadata":{}},{"cell_type":"code","source":"Y_pred = model.predict(X_val)\nY_pred_classes = np.argmax(Y_pred,axis = 1)\nY_true = np.argmax(Y_val,axis = 1)\nconfusion_mtx = confusion_matrix(Y_true,Y_pred_classes)\nf,ax = plt.subplots(figsize=(8,8))\nsns.heatmap(confusion_mtx,annot=True,cmap = \"Greens\",linewidth=0.9,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-08-13T20:02:29.728280Z","iopub.execute_input":"2023-08-13T20:02:29.728905Z","iopub.status.idle":"2023-08-13T20:02:31.029843Z","shell.execute_reply.started":"2023-08-13T20:02:29.728873Z","shell.execute_reply":"2023-08-13T20:02:31.028670Z"},"trusted":true},"execution_count":null,"outputs":[]}]}