{"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":"2021-06-13T06:11:10.517392Z","iopub.execute_input":"2021-06-13T06:11:10.517870Z","iopub.status.idle":"2021-06-13T06:11:10.527493Z","shell.execute_reply.started":"2021-06-13T06:11:10.517756Z","shell.execute_reply":"2021-06-13T06:11:10.526103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head /kaggle/input/digit-recognizer/sample_submission.csv","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:11:41.288697Z","iopub.execute_input":"2021-06-13T06:11:41.289095Z","iopub.status.idle":"2021-06-13T06:11:42.028626Z","shell.execute_reply.started":"2021-06-13T06:11:41.289063Z","shell.execute_reply":"2021-06-13T06:11:42.027239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head /kaggle/input/digit-recognizer/test.csv","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:12:37.827736Z","iopub.execute_input":"2021-06-13T06:12:37.828219Z","iopub.status.idle":"2021-06-13T06:12:38.588358Z","shell.execute_reply.started":"2021-06-13T06:12:37.828177Z","shell.execute_reply":"2021-06-13T06:12:38.587290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! wc -l ../input/digit-recognizer/test.csv","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:13:37.776039Z","iopub.execute_input":"2021-06-13T06:13:37.776448Z","iopub.status.idle":"2021-06-13T06:13:39.699787Z","shell.execute_reply.started":"2021-06-13T06:13:37.776412Z","shell.execute_reply":"2021-06-13T06:13:39.698334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! wc -l ../input/digit-recognizer/train.csv","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:13:54.413958Z","iopub.execute_input":"2021-06-13T06:13:54.414345Z","iopub.status.idle":"2021-06-13T06:13:57.145990Z","shell.execute_reply.started":"2021-06-13T06:13:54.414313Z","shell.execute_reply":"2021-06-13T06:13:57.144498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wc -l ../input/digit-recognizer/sample_submission.csv","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:17:47.250469Z","iopub.execute_input":"2021-06-13T06:17:47.250907Z","iopub.status.idle":"2021-06-13T06:17:47.990380Z","shell.execute_reply.started":"2021-06-13T06:17:47.250872Z","shell.execute_reply":"2021-06-13T06:17:47.988802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading our datasets","metadata":{}},{"cell_type":"code","source":"# read train \ntrain = pd.read_csv(\"../input/digit-recognizer/train.csv\")\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:31:07.166083Z","iopub.execute_input":"2021-06-13T06:31:07.166480Z","iopub.status.idle":"2021-06-13T06:31:10.536487Z","shell.execute_reply.started":"2021-06-13T06:31:07.166444Z","shell.execute_reply":"2021-06-13T06:31:10.535473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read test \ntest= pd.read_csv(\"../input/digit-recognizer/test.csv\")\nprint(test.shape)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:31:24.427180Z","iopub.execute_input":"2021-06-13T06:31:24.427611Z","iopub.status.idle":"2021-06-13T06:31:26.325033Z","shell.execute_reply.started":"2021-06-13T06:31:24.427567Z","shell.execute_reply":"2021-06-13T06:31:26.324077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# put labels into y_train variable\nY_train = train[\"label\"]\n# Drop 'label' column\nX_train = train.drop(labels = [\"label\"],axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:33:20.277390Z","iopub.execute_input":"2021-06-13T06:33:20.278061Z","iopub.status.idle":"2021-06-13T06:33:20.444771Z","shell.execute_reply.started":"2021-06-13T06:33:20.278023Z","shell.execute_reply":"2021-06-13T06:33:20.443602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"label\"].value_counts(normalize=True)*100","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:34:19.865265Z","iopub.execute_input":"2021-06-13T06:34:19.865632Z","iopub.status.idle":"2021-06-13T06:34:19.874242Z","shell.execute_reply.started":"2021-06-13T06:34:19.865602Z","shell.execute_reply":"2021-06-13T06:34:19.873248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:38:22.178948Z","iopub.execute_input":"2021-06-13T06:38:22.179282Z","iopub.status.idle":"2021-06-13T06:38:22.185098Z","shell.execute_reply.started":"2021-06-13T06:38:22.179254Z","shell.execute_reply":"2021-06-13T06:38:22.183870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualize number of digits classes\nplt.figure(figsize=(15,7))\ng = sns.countplot(Y_train, palette=\"icefire\")\nplt.title(\"Number of digit classes\")\nY_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:38:22.503074Z","iopub.execute_input":"2021-06-13T06:38:22.503422Z","iopub.status.idle":"2021-06-13T06:38:22.730418Z","shell.execute_reply.started":"2021-06-13T06:38:22.503394Z","shell.execute_reply":"2021-06-13T06:38:22.728934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot some samples\nimg = X_train.iloc[0].to_numpy()\nimg=img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[0,0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:42:31.459831Z","iopub.execute_input":"2021-06-13T06:42:31.460274Z","iopub.status.idle":"2021-06-13T06:42:31.606328Z","shell.execute_reply.started":"2021-06-13T06:42:31.460241Z","shell.execute_reply":"2021-06-13T06:42:31.605075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot some samples\nimg = X_train.iloc[10].to_numpy()\nimg=img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[10,0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:42:48.233624Z","iopub.execute_input":"2021-06-13T06:42:48.234030Z","iopub.status.idle":"2021-06-13T06:42:48.372381Z","shell.execute_reply.started":"2021-06-13T06:42:48.233997Z","shell.execute_reply":"2021-06-13T06:42:48.371323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot some samples\nimg = X_train.iloc[100].to_numpy()\nimg=img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[100,0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:42:57.906863Z","iopub.execute_input":"2021-06-13T06:42:57.907266Z","iopub.status.idle":"2021-06-13T06:42:58.044579Z","shell.execute_reply.started":"2021-06-13T06:42:57.907233Z","shell.execute_reply":"2021-06-13T06:42:58.043727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalization\n#Images 28*28","metadata":{"execution":{"iopub.status.busy":"2021-06-13T06:44:27.921930Z","iopub.execute_input":"2021-06-13T06:44:27.922370Z","iopub.status.idle":"2021-06-13T06:44:27.926926Z","shell.execute_reply.started":"2021-06-13T06:44:27.922333Z","shell.execute_reply":"2021-06-13T06:44:27.925793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the data\nX_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":"2021-06-13T06:44:42.073877Z","iopub.execute_input":"2021-06-13T06:44:42.074303Z","iopub.status.idle":"2021-06-13T06:44:42.219501Z","shell.execute_reply.started":"2021-06-13T06:44:42.074268Z","shell.execute_reply":"2021-06-13T06:44:42.218216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape\nX_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":"2021-06-13T06:44:55.196546Z","iopub.execute_input":"2021-06-13T06:44:55.196898Z","iopub.status.idle":"2021-06-13T06:44:55.204408Z","shell.execute_reply.started":"2021-06-13T06:44:55.196868Z","shell.execute_reply":"2021-06-13T06:44:55.203523Z"},"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_train = to_categorical(Y_train, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:03:14.693022Z","iopub.execute_input":"2021-06-13T07:03:14.693638Z","iopub.status.idle":"2021-06-13T07:03:14.699879Z","shell.execute_reply.started":"2021-06-13T07:03:14.693586Z","shell.execute_reply":"2021-06-13T07:03:14.698607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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_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_val shape\",X_val.shape)\nprint(\"y_train shape\",Y_train.shape)\nprint(\"y_val shape\",Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:03:33.714381Z","iopub.execute_input":"2021-06-13T07:03:33.714988Z","iopub.status.idle":"2021-06-13T07:03:33.810963Z","shell.execute_reply.started":"2021-06-13T07:03:33.714953Z","shell.execute_reply":"2021-06-13T07:03:33.810203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# \nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\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\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\nmodel = Sequential()\n#Layer-1\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\n#Layer-2\nmodel.add(Conv2D(filters = 16, 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\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":"2021-06-13T07:03:47.871066Z","iopub.execute_input":"2021-06-13T07:03:47.871612Z","iopub.status.idle":"2021-06-13T07:03:47.942803Z","shell.execute_reply.started":"2021-06-13T07:03:47.871579Z","shell.execute_reply":"2021-06-13T07:03:47.941604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:03:48.597043Z","iopub.execute_input":"2021-06-13T07:03:48.597602Z","iopub.status.idle":"2021-06-13T07:03:48.610581Z","shell.execute_reply.started":"2021-06-13T07:03:48.597567Z","shell.execute_reply":"2021-06-13T07:03:48.609259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:03:49.188907Z","iopub.execute_input":"2021-06-13T07:03:49.189293Z","iopub.status.idle":"2021-06-13T07:03:49.199660Z","shell.execute_reply.started":"2021-06-13T07:03:49.189261Z","shell.execute_reply":"2021-06-13T07:03:49.196735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10  # for better result increase the epochs\nbatch_size = 250","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:03:53.756924Z","iopub.execute_input":"2021-06-13T07:03:53.757294Z","iopub.status.idle":"2021-06-13T07:03:53.762020Z","shell.execute_reply.started":"2021-06-13T07:03:53.757261Z","shell.execute_reply":"2021-06-13T07:03:53.760916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory=model.fit(\n    X_train, \n    Y_train, \n    epochs=10, \n    batch_size=250, validation_data = (X_val,Y_val))","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:03:55.234775Z","iopub.execute_input":"2021-06-13T07:03:55.235164Z","iopub.status.idle":"2021-06-13T07:05:19.932445Z","shell.execute_reply.started":"2021-06-13T07:03:55.235133Z","shell.execute_reply":"2021-06-13T07:05:19.930945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:17:16.432733Z","iopub.execute_input":"2021-06-13T07:17:16.433125Z","iopub.status.idle":"2021-06-13T07:17:21.176041Z","shell.execute_reply.started":"2021-06-13T07:17:16.433092Z","shell.execute_reply":"2021-06-13T07:17:21.174779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:17:26.847547Z","iopub.execute_input":"2021-06-13T07:17:26.847927Z","iopub.status.idle":"2021-06-13T07:17:26.855550Z","shell.execute_reply.started":"2021-06-13T07:17:26.847896Z","shell.execute_reply":"2021-06-13T07:17:26.854031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred[0]","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:17:41.755732Z","iopub.execute_input":"2021-06-13T07:17:41.756188Z","iopub.status.idle":"2021-06-13T07:17:41.764001Z","shell.execute_reply.started":"2021-06-13T07:17:41.756148Z","shell.execute_reply":"2021-06-13T07:17:41.763056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred_classes = np.argmax(y_pred,axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:18:02.042804Z","iopub.execute_input":"2021-06-13T07:18:02.043505Z","iopub.status.idle":"2021-06-13T07:18:02.049102Z","shell.execute_reply.started":"2021-06-13T07:18:02.043457Z","shell.execute_reply":"2021-06-13T07:18:02.047372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:18:58.982458Z","iopub.execute_input":"2021-06-13T07:18:58.983220Z","iopub.status.idle":"2021-06-13T07:18:59.008136Z","shell.execute_reply.started":"2021-06-13T07:18:58.983172Z","shell.execute_reply":"2021-06-13T07:18:59.007040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.columns","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:19:05.702185Z","iopub.execute_input":"2021-06-13T07:19:05.702946Z","iopub.status.idle":"2021-06-13T07:19:05.709389Z","shell.execute_reply.started":"2021-06-13T07:19:05.702900Z","shell.execute_reply":"2021-06-13T07:19:05.708437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_final_v1=pd.DataFrame(Y_pred_classes,columns=['Label'])","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:24:43.659795Z","iopub.execute_input":"2021-06-13T07:24:43.660212Z","iopub.status.idle":"2021-06-13T07:24:43.665828Z","shell.execute_reply.started":"2021-06-13T07:24:43.660179Z","shell.execute_reply":"2021-06-13T07:24:43.664569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_final_v1['ImageId']=np.arange(1,28001)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:26:42.746547Z","iopub.execute_input":"2021-06-13T07:26:42.746955Z","iopub.status.idle":"2021-06-13T07:26:42.754716Z","shell.execute_reply.started":"2021-06-13T07:26:42.746923Z","shell.execute_reply":"2021-06-13T07:26:42.752789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_final_v1.to_csv(\"./submission_cnn_model.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:27:25.770401Z","iopub.execute_input":"2021-06-13T07:27:25.771100Z","iopub.status.idle":"2021-06-13T07:27:25.828515Z","shell.execute_reply.started":"2021-06-13T07:27:25.771051Z","shell.execute_reply":"2021-06-13T07:27:25.827440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_final_v1","metadata":{"execution":{"iopub.status.busy":"2021-06-13T07:27:26.773295Z","iopub.execute_input":"2021-06-13T07:27:26.773693Z","iopub.status.idle":"2021-06-13T07:27:26.789667Z","shell.execute_reply.started":"2021-06-13T07:27:26.773662Z","shell.execute_reply":"2021-06-13T07:27:26.788340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Schedule a LR\n# Using other optimisers\n# Use data augmentation\n# Using pretrained models(finetuning)","metadata":{},"execution_count":null,"outputs":[]}]}