{"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":"#importing necessary libraries\nimport pandas as pd\nimport numpy as np\nfrom tensorflow import keras\nfrom keras.layers import Dense, MaxPool2D, Conv2D, Dropout,Flatten\nfrom keras.models import Sequential\nfrom keras.utils import np_utils\nfrom keras.datasets import mnist\nfrom sklearn import metrics\nimport numpy\nfrom matplotlib import pyplot as plt\n\nimport tensorflow as  tf\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-12T06:05:24.709379Z","iopub.execute_input":"2022-03-12T06:05:24.709995Z","iopub.status.idle":"2022-03-12T06:05:32.070915Z","shell.execute_reply.started":"2022-03-12T06:05:24.709900Z","shell.execute_reply":"2022-03-12T06:05:32.070122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = pd.read_csv('../input/digit-recognizer/train.csv').values\ntest_dataset_X = pd.read_csv('../input/digit-recognizer/test.csv').values\ntest_dataset_y = pd.read_csv('../input/digit-recognizer/sample_submission.csv').values","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:32.072569Z","iopub.execute_input":"2022-03-12T06:05:32.072847Z","iopub.status.idle":"2022-03-12T06:05:38.295099Z","shell.execute_reply.started":"2022-03-12T06:05:32.072815Z","shell.execute_reply":"2022-03-12T06:05:38.293978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.296680Z","iopub.execute_input":"2022-03-12T06:05:38.297004Z","iopub.status.idle":"2022-03-12T06:05:38.308708Z","shell.execute_reply.started":"2022-03-12T06:05:38.296972Z","shell.execute_reply":"2022-03-12T06:05:38.307645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset_X","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.311178Z","iopub.execute_input":"2022-03-12T06:05:38.311457Z","iopub.status.idle":"2022-03-12T06:05:38.323348Z","shell.execute_reply.started":"2022-03-12T06:05:38.311425Z","shell.execute_reply":"2022-03-12T06:05:38.322312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset_y","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.325050Z","iopub.execute_input":"2022-03-12T06:05:38.325280Z","iopub.status.idle":"2022-03-12T06:05:38.340990Z","shell.execute_reply.started":"2022-03-12T06:05:38.325253Z","shell.execute_reply":"2022-03-12T06:05:38.339756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_dataset.shape)\nprint(test_dataset_X.shape)\nprint(test_dataset_y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.342546Z","iopub.execute_input":"2022-03-12T06:05:38.342854Z","iopub.status.idle":"2022-03-12T06:05:38.353504Z","shell.execute_reply.started":"2022-03-12T06:05:38.342819Z","shell.execute_reply":"2022-03-12T06:05:38.352473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = test_dataset_y[:,1]\ncol.shape = (len(col),1)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.355199Z","iopub.execute_input":"2022-03-12T06:05:38.355518Z","iopub.status.idle":"2022-03-12T06:05:38.365349Z","shell.execute_reply.started":"2022-03-12T06:05:38.355475Z","shell.execute_reply":"2022-03-12T06:05:38.364303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = np.hstack((test_dataset_X[:,:1], col, test_dataset_X[:,1:]))\n","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.366595Z","iopub.execute_input":"2022-03-12T06:05:38.366925Z","iopub.status.idle":"2022-03-12T06:05:38.521293Z","shell.execute_reply.started":"2022-03-12T06:05:38.366875Z","shell.execute_reply":"2022-03-12T06:05:38.520257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # temp\n# X_train = train_dataset[:, 1:]\n# X_test = test_dataset[:, 1:]\n# print(X_train.shape)\n# print(X_test.shape)\n# X_train = X_train.reshape(X_train.shape[0],X_train.shape[1], X_train.shape[2], 1).astype('float32')\n# X_test = X_test.reshape(X_test.shape[0],X_test.shape[1],X_test.shape[2],1).astype('float32')\n","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.522992Z","iopub.execute_input":"2022-03-12T06:05:38.523828Z","iopub.status.idle":"2022-03-12T06:05:38.527821Z","shell.execute_reply.started":"2022-03-12T06:05:38.523784Z","shell.execute_reply":"2022-03-12T06:05:38.526951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_dataset[:, 1:].reshape(train_dataset.shape[0],28,28, 1).astype( 'float32' )\nX_train = X_train / 255.0\n\ny_train = train_dataset[:,0]\n\nX_test = test_dataset[:, 1:].reshape(test_dataset.shape[0],28,28, 1).astype( 'float32' )\nX_test = X_test / 255.0\n\ny_test = test_dataset[:,0]","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.530187Z","iopub.execute_input":"2022-03-12T06:05:38.530591Z","iopub.status.idle":"2022-03-12T06:05:38.858283Z","shell.execute_reply.started":"2022-03-12T06:05:38.530555Z","shell.execute_reply":"2022-03-12T06:05:38.857339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(X_train.shape)\n# print(X_test.shape)\n# X_train = X_train.reshape(X_train.shape[0],X_train.shape[2], X_train.shape[3], 1).astype('float32')\n# X_test = X_test.reshape(X_test.shape[0],X_test.shape[2], X_test.shape[3], 1).astype('float32')\n# print(X_train.shape)\n# print(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.859777Z","iopub.execute_input":"2022-03-12T06:05:38.860215Z","iopub.status.idle":"2022-03-12T06:05:38.865826Z","shell.execute_reply.started":"2022-03-12T06:05:38.860171Z","shell.execute_reply":"2022-03-12T06:05:38.864931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_train = X_train.reshape(X_train.shape[0],X_train.shape[1], X_train.shape[2], 1).astype('float32')\n# X_test = X_test.reshape(X_test.shape[0],X_test.shape[1],X_test.shape[2],1).astype('float32')\n","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.867038Z","iopub.execute_input":"2022-03-12T06:05:38.867344Z","iopub.status.idle":"2022-03-12T06:05:38.878291Z","shell.execute_reply.started":"2022-03-12T06:05:38.867302Z","shell.execute_reply":"2022-03-12T06:05:38.877563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.879471Z","iopub.execute_input":"2022-03-12T06:05:38.879831Z","iopub.status.idle":"2022-03-12T06:05:38.899915Z","shell.execute_reply.started":"2022-03-12T06:05:38.879784Z","shell.execute_reply":"2022-03-12T06:05:38.898999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = np_utils.to_categorical(y_train)\ny_test_copy = y_test                        #for future use\ny_test = np_utils.to_categorical(y_test, num_classes = 10)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.901352Z","iopub.execute_input":"2022-03-12T06:05:38.901582Z","iopub.status.idle":"2022-03-12T06:05:38.915411Z","shell.execute_reply.started":"2022-03-12T06:05:38.901553Z","shell.execute_reply":"2022-03-12T06:05:38.913582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = Sequential()\n# model.add(Conv2D(32,(5,5), input_shape = (28,28,1), activation = 'relu'))\n# model.add(MaxPool2D(pool_size = (2,2)))\n# model.add(Dropout(0.3))\n# model.add(Flatten())\n# model.add(Dense(128, activation = 'relu'))\n# model.add(Dense(y_test.shape[1], activation = 'softmax'))\n\n# model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])\n\n# model.fit(X_train, y_train, validation_data = (X_test,y_test), epochs = 20, batch_size = 100, verbose = 1)\n\ncnn = tf.keras.models.Sequential()\ncnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=(28,28,1)))\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=(2,2),strides=2))\ncnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'))    # `input_shape` parameter is removed as it is only required in first layer\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=(2,2),strides=2))\ncnn.add(tf.keras.layers.Flatten())\ncnn.add(tf.keras.layers.Dense(units=128, activation='relu'))\ncnn.add(tf.keras.layers.Dense(units=64, activation='relu'))\ncnn.add(tf.keras.layers.Dense(units=32, activation='relu'))\ncnn.add(tf.keras.layers.Dense(units=10,activation='softmax'))\ncnn.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\ncnn.fit(X_train, y_train, validation_data = (X_test,y_test), epochs=100)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:05:38.917091Z","iopub.execute_input":"2022-03-12T06:05:38.917330Z","iopub.status.idle":"2022-03-12T06:06:21.023845Z","shell.execute_reply.started":"2022-03-12T06:05:38.917301Z","shell.execute_reply":"2022-03-12T06:06:21.022457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = cnn.predict(X_test)\nprediction = numpy.argmax(prediction, axis = 1)\nprediction","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:06:21.025082Z","iopub.status.idle":"2022-03-12T06:06:21.026155Z","shell.execute_reply.started":"2022-03-12T06:06:21.025846Z","shell.execute_reply":"2022-03-12T06:06:21.025879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"accuracy score for the MNIST classification of the dataset is :\",metrics.accuracy_score(y_test_copy, prediction, normalize = True))\nprint(metrics.classification_report(y_test_copy, prediction))\nprint(metrics.confusion_matrix(y_test_copy, prediction))","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:06:21.027894Z","iopub.status.idle":"2022-03-12T06:06:21.028412Z","shell.execute_reply.started":"2022-03-12T06:06:21.028140Z","shell.execute_reply":"2022-03-12T06:06:21.028168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({\"ImageId\": [i for i in range(1,len(prediction)+1)], \"Label\": prediction})\nprint(output)\noutput.to_csv('19BCE245_IRS_Prac1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T06:06:21.029633Z","iopub.status.idle":"2022-03-12T06:06:21.030030Z","shell.execute_reply.started":"2022-03-12T06:06:21.029850Z","shell.execute_reply":"2022-03-12T06:06:21.029870Z"},"trusted":true},"execution_count":null,"outputs":[]}]}