{"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-05-22T11:59:53.994568Z","iopub.execute_input":"2021-05-22T11:59:53.994992Z","iopub.status.idle":"2021-05-22T11:59:54.006670Z","shell.execute_reply.started":"2021-05-22T11:59:53.994954Z","shell.execute_reply":"2021-05-22T11:59:54.005175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:54.010665Z","iopub.execute_input":"2021-05-22T11:59:54.011411Z","iopub.status.idle":"2021-05-22T11:59:54.020948Z","shell.execute_reply.started":"2021-05-22T11:59:54.011131Z","shell.execute_reply":"2021-05-22T11:59:54.019639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:54.023010Z","iopub.execute_input":"2021-05-22T11:59:54.023569Z","iopub.status.idle":"2021-05-22T11:59:54.032982Z","shell.execute_reply.started":"2021-05-22T11:59:54.023522Z","shell.execute_reply":"2021-05-22T11:59:54.031743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, AveragePooling2D, MaxPool2D, BatchNormalization, Dropout, Activation","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:54.036681Z","iopub.execute_input":"2021-05-22T11:59:54.037079Z","iopub.status.idle":"2021-05-22T11:59:54.046333Z","shell.execute_reply.started":"2021-05-22T11:59:54.037034Z","shell.execute_reply":"2021-05-22T11:59:54.045127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:54.048513Z","iopub.execute_input":"2021-05-22T11:59:54.049080Z","iopub.status.idle":"2021-05-22T11:59:59.417446Z","shell.execute_reply.started":"2021-05-22T11:59:54.048958Z","shell.execute_reply":"2021-05-22T11:59:59.416241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:59.419623Z","iopub.execute_input":"2021-05-22T11:59:59.420044Z","iopub.status.idle":"2021-05-22T11:59:59.446386Z","shell.execute_reply.started":"2021-05-22T11:59:59.420012Z","shell.execute_reply":"2021-05-22T11:59:59.445034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:59.448615Z","iopub.execute_input":"2021-05-22T11:59:59.449130Z","iopub.status.idle":"2021-05-22T11:59:59.470186Z","shell.execute_reply.started":"2021-05-22T11:59:59.449066Z","shell.execute_reply":"2021-05-22T11:59:59.468847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train.drop('label', axis =1)\ny = train['label']","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:59.471893Z","iopub.execute_input":"2021-05-22T11:59:59.472591Z","iopub.status.idle":"2021-05-22T11:59:59.582474Z","shell.execute_reply.started":"2021-05-22T11:59:59.472547Z","shell.execute_reply":"2021-05-22T11:59:59.581339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(x)\ny_train = np.array(y)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:59.584109Z","iopub.execute_input":"2021-05-22T11:59:59.584628Z","iopub.status.idle":"2021-05-22T11:59:59.691549Z","shell.execute_reply.started":"2021-05-22T11:59:59.584577Z","shell.execute_reply":"2021-05-22T11:59:59.690250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.array(test)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:59.696513Z","iopub.execute_input":"2021-05-22T11:59:59.697113Z","iopub.status.idle":"2021-05-22T11:59:59.771369Z","shell.execute_reply.started":"2021-05-22T11:59:59.697019Z","shell.execute_reply":"2021-05-22T11:59:59.770227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('x_train.shape = ', x_train.shape, '\\ny_train.shape = ', y_train.shape, '\\nx_test.shape = ', x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:59.774043Z","iopub.execute_input":"2021-05-22T11:59:59.774713Z","iopub.status.idle":"2021-05-22T11:59:59.782448Z","shell.execute_reply.started":"2021-05-22T11:59:59.774664Z","shell.execute_reply":"2021-05-22T11:59:59.781110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:59.784177Z","iopub.execute_input":"2021-05-22T11:59:59.785085Z","iopub.status.idle":"2021-05-22T11:59:59.802084Z","shell.execute_reply.started":"2021-05-22T11:59:59.785040Z","shell.execute_reply":"2021-05-22T11:59:59.800895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,10))\nplt.xticks(size = 12)\nsns.countplot(y_train, linewidth = 2)\nplt.title('Distribution of labels in train dataset')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-22T11:59:59.804006Z","iopub.execute_input":"2021-05-22T11:59:59.804453Z","iopub.status.idle":"2021-05-22T12:00:00.040516Z","shell.execute_reply.started":"2021-05-22T11:59:59.804408Z","shell.execute_reply":"2021-05-22T12:00:00.039236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(len(x_train), 28,28,1)\nx_test = x_test.reshape(len(x_test), 28, 28, 1)\ny_train = tf.keras.utils.to_categorical(y_train)\nprint('x_train.shape = ', x_train.shape, '\\nx_test.shape = ', x_test.shape ,'\\ny_train  = ', y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:00:00.042124Z","iopub.execute_input":"2021-05-22T12:00:00.042856Z","iopub.status.idle":"2021-05-22T12:00:00.053714Z","shell.execute_reply.started":"2021-05-22T12:00:00.042808Z","shell.execute_reply":"2021-05-22T12:00:00.052176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.astype('float32')/255\ntest = x_test.astype('float32')/255","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:00:00.055984Z","iopub.execute_input":"2021-05-22T12:00:00.056723Z","iopub.status.idle":"2021-05-22T12:00:00.209931Z","shell.execute_reply.started":"2021-05-22T12:00:00.056675Z","shell.execute_reply":"2021-05-22T12:00:00.208737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length , width = (6,6)\nfig ,axes = plt.subplots(length, width, figsize = (20,15))\naxes = axes.ravel()\nfor i in range(0, 36):\n    axes[i].imshow(x_train[i], cmap = 'gray')\n    axes[i].set_title('Digit = '+str(i))\n    axes[i].axis('off')\nplt.subplots_adjust(wspace= 0.1)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:00:00.211553Z","iopub.execute_input":"2021-05-22T12:00:00.212047Z","iopub.status.idle":"2021-05-22T12:00:02.479723Z","shell.execute_reply.started":"2021-05-22T12:00:00.211988Z","shell.execute_reply":"2021-05-22T12:00:02.478485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test = train_test_split(x_train,y_train,test_size=0.15,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:00:02.483456Z","iopub.execute_input":"2021-05-22T12:00:02.483798Z","iopub.status.idle":"2021-05-22T12:00:02.841806Z","shell.execute_reply.started":"2021-05-22T12:00:02.483752Z","shell.execute_reply":"2021-05-22T12:00:02.840585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = Sequential()\n#model.add(Conv2D(filters = 32,kernel_size = (5,5), padding=\"same\", activation = 'tanh',input_shape = (28,28,1)))\n#model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))\n\n#model.add(Conv2D(filters = 32, kernel_size=(5, 5), padding='valid',  strides=(1, 1), activation='tanh'))\n#model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))\n#Lenet","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:00:02.843481Z","iopub.execute_input":"2021-05-22T12:00:02.843974Z","iopub.status.idle":"2021-05-22T12:00:02.849339Z","shell.execute_reply.started":"2021-05-22T12:00:02.843899Z","shell.execute_reply":"2021-05-22T12:00:02.847897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model.add(Flatten())\nmodel.add(Dense(120, activation='tanh'))\nmodel.add(Dense(84, activation='tanh'))\nmodel.add(Dense(10,activation='softmax')) \nmodel.summary()\"\"\"","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:00:02.851089Z","iopub.execute_input":"2021-05-22T12:00:02.851554Z","iopub.status.idle":"2021-05-22T12:00:02.865749Z","shell.execute_reply.started":"2021-05-22T12:00:02.851510Z","shell.execute_reply":"2021-05-22T12:00:02.864497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating CNN model\n\"\"\"\n  [[Conv2D->relu]*2 -> BatchNormalization -> MaxPool2D -> Dropout]*2 -> \n  [Conv2D->relu]*2 -> BatchNormalization -> Dropout -> \n  Flatten -> Dense -> BatchNormalization -> Dropout -> Out\n\"\"\"\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', activation ='relu', input_shape = (28,28,1)))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization())\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3), padding = 'Same',  activation ='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(10, activation = \"softmax\"))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:00:02.867714Z","iopub.execute_input":"2021-05-22T12:00:02.868284Z","iopub.status.idle":"2021-05-22T12:00:03.076360Z","shell.execute_reply.started":"2021-05-22T12:00:02.868237Z","shell.execute_reply":"2021-05-22T12:00:03.075213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',metrics = ['accuracy'],loss = 'categorical_crossentropy')","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:00:03.079734Z","iopub.execute_input":"2021-05-22T12:00:03.080077Z","iopub.status.idle":"2021-05-22T12:00:03.097522Z","shell.execute_reply.started":"2021-05-22T12:00:03.080045Z","shell.execute_reply":"2021-05-22T12:00:03.096178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HWDR= model.fit(x_train, y_train, batch_size = 128, epochs = 45)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:00:03.103085Z","iopub.execute_input":"2021-05-22T12:00:03.103354Z","iopub.status.idle":"2021-05-22T12:10:56.641861Z","shell.execute_reply.started":"2021-05-22T12:00:03.103327Z","shell.execute_reply":"2021-05-22T12:10:56.640769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:10:56.643928Z","iopub.execute_input":"2021-05-22T12:10:56.644389Z","iopub.status.idle":"2021-05-22T12:10:59.949674Z","shell.execute_reply.started":"2021-05-22T12:10:56.644341Z","shell.execute_reply":"2021-05-22T12:10:59.948527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = model.predict(test)\nresults = np.argmax(test_predictions, axis =1)\nresults[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:11:34.844152Z","iopub.execute_input":"2021-05-22T12:11:34.844533Z","iopub.status.idle":"2021-05-22T12:11:44.569214Z","shell.execute_reply.started":"2021-05-22T12:11:34.844489Z","shell.execute_reply":"2021-05-22T12:11:44.568123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results[2]","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:13:05.480124Z","iopub.execute_input":"2021-05-22T12:13:05.480495Z","iopub.status.idle":"2021-05-22T12:13:05.491312Z","shell.execute_reply.started":"2021-05-22T12:13:05.480462Z","shell.execute_reply":"2021-05-22T12:13:05.490110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.Series(results, name = 'Label')","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:13:07.723890Z","iopub.execute_input":"2021-05-22T12:13:07.724263Z","iopub.status.idle":"2021-05-22T12:13:07.730501Z","shell.execute_reply.started":"2021-05-22T12:13:07.724230Z","shell.execute_reply":"2021-05-22T12:13:07.729179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:13:08.987043Z","iopub.execute_input":"2021-05-22T12:13:08.987552Z","iopub.status.idle":"2021-05-22T12:13:09.003955Z","shell.execute_reply.started":"2021-05-22T12:13:08.987510Z","shell.execute_reply":"2021-05-22T12:13:09.001946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\nsubmission.to_csv(\"submission2.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-22T12:13:24.036880Z","iopub.execute_input":"2021-05-22T12:13:24.037229Z","iopub.status.idle":"2021-05-22T12:13:24.099810Z","shell.execute_reply.started":"2021-05-22T12:13:24.037200Z","shell.execute_reply":"2021-05-22T12:13:24.098815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}