{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom matplotlib import pyplot\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom numpy import asarray, unique, argmax\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout\nfrom keras.optimizers import RMSprop","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:05:20.222461Z","iopub.execute_input":"2021-05-23T08:05:20.222842Z","iopub.status.idle":"2021-05-23T08:05:26.661665Z","shell.execute_reply.started":"2021-05-23T08:05:20.222757Z","shell.execute_reply":"2021-05-23T08:05:26.660881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading the train dataset\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:06:19.833667Z","iopub.execute_input":"2021-05-23T08:06:19.834000Z","iopub.status.idle":"2021-05-23T08:06:23.312877Z","shell.execute_reply.started":"2021-05-23T08:06:19.833968Z","shell.execute_reply":"2021-05-23T08:06:23.312169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking the shape of provided train dataset\ndata.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:06:30.736139Z","iopub.execute_input":"2021-05-23T08:06:30.736494Z","iopub.status.idle":"2021-05-23T08:06:30.741295Z","shell.execute_reply.started":"2021-05-23T08:06:30.736463Z","shell.execute_reply":"2021-05-23T08:06:30.740272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking unique numbers in train label column\nunique = data['label'].unique()\nprint(\"Unique Numbers :\",unique)\n\n#countine the unique number of digits for classification\nn_classes = len(unique)\nprint(\"Number of classes :\",n_classes)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:06:45.552704Z","iopub.execute_input":"2021-05-23T08:06:45.553043Z","iopub.status.idle":"2021-05-23T08:06:45.563541Z","shell.execute_reply.started":"2021-05-23T08:06:45.553000Z","shell.execute_reply":"2021-05-23T08:06:45.562618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filtering the dataset between X and Y\nx = data.drop(labels = [\"label\"], axis=1)\nprint(\"X= \",x)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:06:59.688841Z","iopub.execute_input":"2021-05-23T08:06:59.689213Z","iopub.status.idle":"2021-05-23T08:06:59.785173Z","shell.execute_reply.started":"2021-05-23T08:06:59.689181Z","shell.execute_reply":"2021-05-23T08:06:59.784446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filtering the target variable from the train data\ny = data['label']\nprint(\"Target Variable: \",y)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:07:14.671338Z","iopub.execute_input":"2021-05-23T08:07:14.671655Z","iopub.status.idle":"2021-05-23T08:07:14.681055Z","shell.execute_reply.started":"2021-05-23T08:07:14.671625Z","shell.execute_reply":"2021-05-23T08:07:14.680072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Splitting the dataset in train and test\nx_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.20, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:07:27.786449Z","iopub.execute_input":"2021-05-23T08:07:27.786794Z","iopub.status.idle":"2021-05-23T08:07:28.024352Z","shell.execute_reply.started":"2021-05-23T08:07:27.786760Z","shell.execute_reply":"2021-05-23T08:07:28.023550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Priting the Splitted dataset\nprint(\"X_train: \", x_train)\nprint(\"X_test: \", y_test)\nprint(\"Y_train: \", y_train)\nprint(\"Y_test: \", y_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:07:48.291847Z","iopub.execute_input":"2021-05-23T08:07:48.292202Z","iopub.status.idle":"2021-05-23T08:07:48.312254Z","shell.execute_reply.started":"2021-05-23T08:07:48.292170Z","shell.execute_reply":"2021-05-23T08:07:48.310954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reshaping the Dataset\nx_train = x_train.values.reshape(-1,28,28,1)\nx_test = x_test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:08:09.108203Z","iopub.execute_input":"2021-05-23T08:08:09.108562Z","iopub.status.idle":"2021-05-23T08:08:09.113799Z","shell.execute_reply.started":"2021-05-23T08:08:09.108519Z","shell.execute_reply":"2021-05-23T08:08:09.112841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#determine the shape of input images\nin_shape = x_train.shape[1:]\nprint(in_shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:08:20.713781Z","iopub.execute_input":"2021-05-23T08:08:20.714113Z","iopub.status.idle":"2021-05-23T08:08:20.719236Z","shell.execute_reply.started":"2021-05-23T08:08:20.714078Z","shell.execute_reply":"2021-05-23T08:08:20.717981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_train[0].reshape([28,28]))","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:08:36.489824Z","iopub.execute_input":"2021-05-23T08:08:36.490177Z","iopub.status.idle":"2021-05-23T08:08:36.636405Z","shell.execute_reply.started":"2021-05-23T08:08:36.490138Z","shell.execute_reply":"2021-05-23T08:08:36.635513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# normalize pixel values\nx_train = x_train.astype('float32') / 255.0\nx_test = x_test.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:09:07.436679Z","iopub.execute_input":"2021-05-23T08:09:07.437009Z","iopub.status.idle":"2021-05-23T08:09:07.511573Z","shell.execute_reply.started":"2021-05-23T08:09:07.436976Z","shell.execute_reply":"2021-05-23T08:09:07.510643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = in_shape))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:09:32.243695Z","iopub.execute_input":"2021-05-23T08:09:32.244010Z","iopub.status.idle":"2021-05-23T08:09:35.111428Z","shell.execute_reply.started":"2021-05-23T08:09:32.243980Z","shell.execute_reply":"2021-05-23T08:09:35.110562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:09:46.924128Z","iopub.execute_input":"2021-05-23T08:09:46.924458Z","iopub.status.idle":"2021-05-23T08:09:46.930700Z","shell.execute_reply.started":"2021-05-23T08:09:46.924430Z","shell.execute_reply":"2021-05-23T08:09:46.929568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define loss and optimizer\nmodel.compile(optimizer= 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:10:02.131915Z","iopub.execute_input":"2021-05-23T08:10:02.132300Z","iopub.status.idle":"2021-05-23T08:10:02.149927Z","shell.execute_reply.started":"2021-05-23T08:10:02.132258Z","shell.execute_reply":"2021-05-23T08:10:02.149043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit the model\nmodel.fit(x_train, y_train, validation_split=0.2, epochs=100, batch_size=128, verbose= 1)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:10:12.764820Z","iopub.execute_input":"2021-05-23T08:10:12.765200Z","iopub.status.idle":"2021-05-23T08:12:36.830404Z","shell.execute_reply.started":"2021-05-23T08:10:12.765166Z","shell.execute_reply":"2021-05-23T08:12:36.829450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Evaluate the model\nloss, accuracy = model.evaluate(x_test, y_test, verbose=1)\nprint('Accuracy: %.3f' %accuracy)\nprint('Loss: ',loss)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:13:01.428711Z","iopub.execute_input":"2021-05-23T08:13:01.429080Z","iopub.status.idle":"2021-05-23T08:13:02.206765Z","shell.execute_reply.started":"2021-05-23T08:13:01.429040Z","shell.execute_reply":"2021-05-23T08:13:02.206101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#make a pridiction\nimage = x_test[2]\nypred = model.predict(asarray([image]))\nprint('Prediction: Class =%d' %argmax(ypred))","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:13:39.022240Z","iopub.execute_input":"2021-05-23T08:13:39.022583Z","iopub.status.idle":"2021-05-23T08:13:39.164831Z","shell.execute_reply.started":"2021-05-23T08:13:39.022551Z","shell.execute_reply":"2021-05-23T08:13:39.163775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_test[2].reshape([28,28]))\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:13:50.281842Z","iopub.execute_input":"2021-05-23T08:13:50.282196Z","iopub.status.idle":"2021-05-23T08:13:50.406219Z","shell.execute_reply.started":"2021-05-23T08:13:50.282163Z","shell.execute_reply":"2021-05-23T08:13:50.405076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading the test Data\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:14:12.366897Z","iopub.execute_input":"2021-05-23T08:14:12.367287Z","iopub.status.idle":"2021-05-23T08:14:14.836634Z","shell.execute_reply.started":"2021-05-23T08:14:12.367252Z","shell.execute_reply":"2021-05-23T08:14:14.835911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalize the test data\ntest = test_data/255.0","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:14:44.507917Z","iopub.execute_input":"2021-05-23T08:14:44.508296Z","iopub.status.idle":"2021-05-23T08:14:44.594662Z","shell.execute_reply.started":"2021-05-23T08:14:44.508264Z","shell.execute_reply":"2021-05-23T08:14:44.593884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reshaping the test data\ntest_final_Data = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:14:55.682957Z","iopub.execute_input":"2021-05-23T08:14:55.683298Z","iopub.status.idle":"2021-05-23T08:14:55.687432Z","shell.execute_reply.started":"2021-05-23T08:14:55.683267Z","shell.execute_reply":"2021-05-23T08:14:55.686521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pridicting the model\nlabel = model.predict(test_final_Data)\nprint(label)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:15:34.879579Z","iopub.execute_input":"2021-05-23T08:15:34.879908Z","iopub.status.idle":"2021-05-23T08:15:36.714991Z","shell.execute_reply.started":"2021-05-23T08:15:34.879877Z","shell.execute_reply":"2021-05-23T08:15:36.713846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#shape of the labels\nlabel.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:15:55.006736Z","iopub.execute_input":"2021-05-23T08:15:55.007067Z","iopub.status.idle":"2021-05-23T08:15:55.014276Z","shell.execute_reply.started":"2021-05-23T08:15:55.007034Z","shell.execute_reply":"2021-05-23T08:15:55.013093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = np.argmax(label, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:16:03.936913Z","iopub.execute_input":"2021-05-23T08:16:03.937326Z","iopub.status.idle":"2021-05-23T08:16:03.941936Z","shell.execute_reply.started":"2021-05-23T08:16:03.937278Z","shell.execute_reply":"2021-05-23T08:16:03.940788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(label)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:16:14.459096Z","iopub.execute_input":"2021-05-23T08:16:14.459481Z","iopub.status.idle":"2021-05-23T08:16:14.465079Z","shell.execute_reply.started":"2021-05-23T08:16:14.459448Z","shell.execute_reply":"2021-05-23T08:16:14.463990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(label.shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:16:24.899201Z","iopub.execute_input":"2021-05-23T08:16:24.899536Z","iopub.status.idle":"2021-05-23T08:16:24.907681Z","shell.execute_reply.started":"2021-05-23T08:16:24.899504Z","shell.execute_reply":"2021-05-23T08:16:24.906667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading the Given sample file for just checking the submission file format\nsample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsample_submission.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:16:44.945887Z","iopub.execute_input":"2021-05-23T08:16:44.946248Z","iopub.status.idle":"2021-05-23T08:16:44.990256Z","shell.execute_reply.started":"2021-05-23T08:16:44.946214Z","shell.execute_reply":"2021-05-23T08:16:44.989537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating the file ready with rows and Columns feild\nindex = test_data.index.values + 1\ndata = {'ImageId' : index, \"Label\" : label}\ndf = pd.DataFrame(data=data)\ndf.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:17:03.640640Z","iopub.execute_input":"2021-05-23T08:17:03.641052Z","iopub.status.idle":"2021-05-23T08:17:03.660428Z","shell.execute_reply.started":"2021-05-23T08:17:03.641007Z","shell.execute_reply":"2021-05-23T08:17:03.659502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_file = pd.DataFrame({'ImageId' : index, \"Label\" : label.astype(int).ravel()})\nsubmit_file.to_csv(\"submission.csv\",index = False)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:17:21.072040Z","iopub.execute_input":"2021-05-23T08:17:21.072364Z","iopub.status.idle":"2021-05-23T08:17:21.311858Z","shell.execute_reply.started":"2021-05-23T08:17:21.072332Z","shell.execute_reply":"2021-05-23T08:17:21.311133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_file","metadata":{"execution":{"iopub.status.busy":"2021-05-23T08:17:35.504377Z","iopub.execute_input":"2021-05-23T08:17:35.504756Z","iopub.status.idle":"2021-05-23T08:17:35.518936Z","shell.execute_reply.started":"2021-05-23T08:17:35.504720Z","shell.execute_reply":"2021-05-23T08:17:35.517961Z"},"trusted":true},"execution_count":null,"outputs":[]}]}