{"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-05T11:37:28.994394Z","iopub.execute_input":"2021-06-05T11:37:28.994863Z","iopub.status.idle":"2021-06-05T11:37:29.008273Z","shell.execute_reply.started":"2021-06-05T11:37:28.994744Z","shell.execute_reply":"2021-06-05T11:37:29.003096Z"},"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\nimport seaborn as sns\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, BatchNormalization\nfrom keras.optimizers import RMSprop","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:37:29.039179Z","iopub.execute_input":"2021-06-05T11:37:29.039740Z","iopub.status.idle":"2021-06-05T11:37:35.872325Z","shell.execute_reply.started":"2021-06-05T11:37:29.039678Z","shell.execute_reply":"2021-06-05T11:37:35.871009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading the train dataset\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:37:35.878888Z","iopub.execute_input":"2021-06-05T11:37:35.881754Z","iopub.status.idle":"2021-06-05T11:37:43.281629Z","shell.execute_reply.started":"2021-06-05T11:37:35.881706Z","shell.execute_reply":"2021-06-05T11:37:43.280302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Data Label Distribution\nprint(data['label'].value_counts())\nsns.countplot(data['label'])","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:37:43.283417Z","iopub.execute_input":"2021-06-05T11:37:43.283880Z","iopub.status.idle":"2021-06-05T11:37:43.540072Z","shell.execute_reply.started":"2021-06-05T11:37:43.283815Z","shell.execute_reply":"2021-06-05T11:37:43.538896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Data.shape:',data.shape)\nprint('Number of classes: ',len(data['label'].unique()))","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:37:43.542421Z","iopub.execute_input":"2021-06-05T11:37:43.542900Z","iopub.status.idle":"2021-06-05T11:37:43.552197Z","shell.execute_reply.started":"2021-06-05T11:37:43.542851Z","shell.execute_reply":"2021-06-05T11:37:43.550513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filtering the dataset between X and Y\nx = data.drop(labels = [\"label\"], axis=1)\ny = data['label']\nprint(\"Target Variable: \",y)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:37:43.557247Z","iopub.execute_input":"2021-06-05T11:37:43.558095Z","iopub.status.idle":"2021-06-05T11:37:43.667408Z","shell.execute_reply.started":"2021-06-05T11:37:43.558045Z","shell.execute_reply":"2021-06-05T11:37:43.665860Z"},"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-06-05T11:37:43.670368Z","iopub.execute_input":"2021-06-05T11:37:43.670933Z","iopub.status.idle":"2021-06-05T11:37:43.956736Z","shell.execute_reply.started":"2021-06-05T11:37:43.670885Z","shell.execute_reply":"2021-06-05T11:37:43.955595Z"},"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-06-05T11:37:43.958533Z","iopub.execute_input":"2021-06-05T11:37:43.958978Z","iopub.status.idle":"2021-06-05T11:37:43.965308Z","shell.execute_reply.started":"2021-06-05T11:37:43.958934Z","shell.execute_reply":"2021-06-05T11:37:43.963792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalization Pixels\nx_train = x_train.astype('float32') / 255.0\nx_test = x_test.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:37:43.967499Z","iopub.execute_input":"2021-06-05T11:37:43.967965Z","iopub.status.idle":"2021-06-05T11:37:44.073398Z","shell.execute_reply.started":"2021-06-05T11:37:43.967921Z","shell.execute_reply":"2021-06-05T11:37:44.072128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu', input_shape = x_train.shape[1:]))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:37:44.075097Z","iopub.execute_input":"2021-06-05T11:37:44.075538Z","iopub.status.idle":"2021-06-05T11:37:47.198359Z","shell.execute_reply.started":"2021-06-05T11:37:44.075486Z","shell.execute_reply":"2021-06-05T11:37:47.197049Z"},"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# define loss and optimizer\nmodel.compile(optimizer= 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])\n# fit the model\nmodel.fit(x_train, y_train, validation_split=0.2, epochs=10, batch_size=128, verbose= 1)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:38:16.925393Z","iopub.execute_input":"2021-06-05T11:38:16.925781Z","iopub.status.idle":"2021-06-05T11:38:34.854961Z","shell.execute_reply.started":"2021-06-05T11:38:16.925748Z","shell.execute_reply":"2021-06-05T11:38:34.853765Z"},"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-06-05T11:38:40.119082Z","iopub.execute_input":"2021-06-05T11:38:40.119453Z","iopub.status.idle":"2021-06-05T11:38:41.000532Z","shell.execute_reply.started":"2021-06-05T11:38:40.119421Z","shell.execute_reply":"2021-06-05T11:38:40.999152Z"},"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-06-05T11:39:01.909099Z","iopub.execute_input":"2021-06-05T11:39:01.909479Z","iopub.status.idle":"2021-06-05T11:39:01.964795Z","shell.execute_reply.started":"2021-06-05T11:39:01.909446Z","shell.execute_reply":"2021-06-05T11:39:01.963558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_test[2].reshape([28,28]))","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:39:04.599378Z","iopub.execute_input":"2021-06-05T11:39:04.599758Z","iopub.status.idle":"2021-06-05T11:39:04.768454Z","shell.execute_reply.started":"2021-06-05T11:39:04.599719Z","shell.execute_reply":"2021-06-05T11:39:04.767274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalize the test data\ntest = test_data/255.0\n#Reshaping the test data\ntest_final_Data = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:39:30.039135Z","iopub.execute_input":"2021-06-05T11:39:30.039511Z","iopub.status.idle":"2021-06-05T11:39:30.138072Z","shell.execute_reply.started":"2021-06-05T11:39:30.039479Z","shell.execute_reply":"2021-06-05T11:39:30.136656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pridicting the model\nlabel = model.predict(test_final_Data)\n#shape of the labels\nlabel = np.argmax(label, axis=1)\nprint(label)\nprint(label.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:41:11.858987Z","iopub.execute_input":"2021-06-05T11:41:11.859349Z","iopub.status.idle":"2021-06-05T11:41:13.373241Z","shell.execute_reply.started":"2021-06-05T11:41:11.859317Z","shell.execute_reply":"2021-06-05T11:41:13.372074Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:41:32.978925Z","iopub.execute_input":"2021-06-05T11:41:32.979307Z","iopub.status.idle":"2021-06-05T11:41:33.010690Z","shell.execute_reply.started":"2021-06-05T11:41:32.979274Z","shell.execute_reply":"2021-06-05T11:41:33.009169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"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()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:42:08.539588Z","iopub.execute_input":"2021-06-05T11:42:08.539968Z","iopub.status.idle":"2021-06-05T11:42:08.553318Z","shell.execute_reply.started":"2021-06-05T11:42:08.539935Z","shell.execute_reply":"2021-06-05T11:42:08.551909Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:42:26.214422Z","iopub.execute_input":"2021-06-05T11:42:26.214801Z","iopub.status.idle":"2021-06-05T11:42:26.477792Z","shell.execute_reply.started":"2021-06-05T11:42:26.214768Z","shell.execute_reply":"2021-06-05T11:42:26.476815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_file","metadata":{"execution":{"iopub.status.busy":"2021-06-05T11:42:46.194487Z","iopub.execute_input":"2021-06-05T11:42:46.194891Z","iopub.status.idle":"2021-06-05T11:42:46.213612Z","shell.execute_reply.started":"2021-06-05T11:42:46.194858Z","shell.execute_reply":"2021-06-05T11:42:46.212082Z"},"trusted":true},"execution_count":null,"outputs":[]}]}