{"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":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:11:23.170037Z","iopub.execute_input":"2021-06-29T16:11:23.170385Z","iopub.status.idle":"2021-06-29T16:11:23.180628Z","shell.execute_reply.started":"2021-06-29T16:11:23.170353Z","shell.execute_reply":"2021-06-29T16:11:23.179605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import all required libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport tensorflow as tf\nfrom tensorflow import keras\n\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:11:25.929296Z","iopub.execute_input":"2021-06-29T16:11:25.929719Z","iopub.status.idle":"2021-06-29T16:11:33.201677Z","shell.execute_reply.started":"2021-06-29T16:11:25.92968Z","shell.execute_reply":"2021-06-29T16:11:33.200762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploring the dataset                                           \n### Data consists of two csv files i.e train and test","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nprint(train_data.shape)\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:11:35.160733Z","iopub.execute_input":"2021-06-29T16:11:35.161133Z","iopub.status.idle":"2021-06-29T16:11:38.823722Z","shell.execute_reply.started":"2021-06-29T16:11:35.161096Z","shell.execute_reply":"2021-06-29T16:11:38.822854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Training Data has 42000 examples of images of digits**","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nprint(test.shape)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:11:40.757428Z","iopub.execute_input":"2021-06-29T16:11:40.757778Z","iopub.status.idle":"2021-06-29T16:11:42.91785Z","shell.execute_reply.started":"2021-06-29T16:11:40.757744Z","shell.execute_reply":"2021-06-29T16:11:42.916522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:11:47.2836Z","iopub.execute_input":"2021-06-29T16:11:47.284063Z","iopub.status.idle":"2021-06-29T16:11:49.443012Z","shell.execute_reply.started":"2021-06-29T16:11:47.284026Z","shell.execute_reply":"2021-06-29T16:11:49.441981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:11:52.318758Z","iopub.execute_input":"2021-06-29T16:11:52.319287Z","iopub.status.idle":"2021-06-29T16:11:54.212827Z","shell.execute_reply.started":"2021-06-29T16:11:52.319254Z","shell.execute_reply":"2021-06-29T16:11:54.211597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-06-29T16:11:57.678824Z","iopub.execute_input":"2021-06-29T16:11:57.679176Z","iopub.status.idle":"2021-06-29T16:11:57.730969Z","shell.execute_reply.started":"2021-06-29T16:11:57.679146Z","shell.execute_reply":"2021-06-29T16:11:57.730208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:00.631581Z","iopub.execute_input":"2021-06-29T16:12:00.632097Z","iopub.status.idle":"2021-06-29T16:12:00.6778Z","shell.execute_reply.started":"2021-06-29T16:12:00.632064Z","shell.execute_reply":"2021-06-29T16:12:00.677041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Test Data has 28000 images of size 784(28*28)**","metadata":{}},{"cell_type":"code","source":"train_data.isnull().sum().sort_values(ascending = False) ","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-06-29T16:12:06.800433Z","iopub.execute_input":"2021-06-29T16:12:06.80094Z","iopub.status.idle":"2021-06-29T16:12:06.853174Z","shell.execute_reply.started":"2021-06-29T16:12:06.800907Z","shell.execute_reply":"2021-06-29T16:12:06.852237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum().sort_values(ascending = False) ","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-06-29T16:12:18.039566Z","iopub.execute_input":"2021-06-29T16:12:18.040159Z","iopub.status.idle":"2021-06-29T16:12:18.081006Z","shell.execute_reply.started":"2021-06-29T16:12:18.04009Z","shell.execute_reply":"2021-06-29T16:12:18.079993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**No Null Values here**","metadata":{}},{"cell_type":"markdown","source":"#### Data is alredy clean and  ready to use","metadata":{}},{"cell_type":"markdown","source":"## Data collection and preparation","metadata":{}},{"cell_type":"code","source":"data_labels = train_data['label']\nY = data_labels\ndata_new = train_data.drop(['label'], axis=1)\ndata_arr = np.array(data_new).reshape(-1,28,28)\ndata_clean = data_arr / 255.0\n\ndata = data_clean","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:23.643582Z","iopub.execute_input":"2021-06-29T16:12:23.644148Z","iopub.status.idle":"2021-06-29T16:12:24.009267Z","shell.execute_reply.started":"2021-06-29T16:12:23.644098Z","shell.execute_reply":"2021-06-29T16:12:24.008435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data\ntest_new = test\ntest_arr = np.array(test_new).reshape(-1,28,28)\ntest_clean = test_arr / 255.0\ntest_data = test_clean","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:27.374348Z","iopub.execute_input":"2021-06-29T16:12:27.374945Z","iopub.status.idle":"2021-06-29T16:12:27.550307Z","shell.execute_reply.started":"2021-06-29T16:12:27.374899Z","shell.execute_reply":"2021-06-29T16:12:27.549335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(data[i], cmap=plt.cm.binary)\n    plt.xlabel(data_labels[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:32.554739Z","iopub.execute_input":"2021-06-29T16:12:32.555112Z","iopub.status.idle":"2021-06-29T16:12:33.655591Z","shell.execute_reply.started":"2021-06-29T16:12:32.555081Z","shell.execute_reply":"2021-06-29T16:12:33.654596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data split into tarining and test splits","metadata":{}},{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(X, Y , random_state=1, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:37.475211Z","iopub.execute_input":"2021-06-29T16:12:37.475572Z","iopub.status.idle":"2021-06-29T16:12:37.917748Z","shell.execute_reply.started":"2021-06-29T16:12:37.475542Z","shell.execute_reply":"2021-06-29T16:12:37.916676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:39.856804Z","iopub.execute_input":"2021-06-29T16:12:39.857273Z","iopub.status.idle":"2021-06-29T16:12:39.866014Z","shell.execute_reply.started":"2021-06-29T16:12:39.857227Z","shell.execute_reply":"2021-06-29T16:12:39.864713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:41.534477Z","iopub.execute_input":"2021-06-29T16:12:41.534814Z","iopub.status.idle":"2021-06-29T16:12:41.543585Z","shell.execute_reply.started":"2021-06-29T16:12:41.534786Z","shell.execute_reply":"2021-06-29T16:12:41.54262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:45.414455Z","iopub.execute_input":"2021-06-29T16:12:45.414809Z","iopub.status.idle":"2021-06-29T16:12:45.419873Z","shell.execute_reply.started":"2021-06-29T16:12:45.414781Z","shell.execute_reply":"2021-06-29T16:12:45.418807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:47.172804Z","iopub.execute_input":"2021-06-29T16:12:47.173202Z","iopub.status.idle":"2021-06-29T16:12:47.177794Z","shell.execute_reply.started":"2021-06-29T16:12:47.173167Z","shell.execute_reply":"2021-06-29T16:12:47.177156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building the Model","metadata":{}},{"cell_type":"markdown","source":"### Setting up layers","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Flatten(input_shape=(28, 28)),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(10)\n])","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:51.24081Z","iopub.execute_input":"2021-06-29T16:12:51.241498Z","iopub.status.idle":"2021-06-29T16:12:51.339283Z","shell.execute_reply.started":"2021-06-29T16:12:51.241458Z","shell.execute_reply":"2021-06-29T16:12:51.338215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compile the Model","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:12:57.646488Z","iopub.execute_input":"2021-06-29T16:12:57.646828Z","iopub.status.idle":"2021-06-29T16:12:57.848568Z","shell.execute_reply.started":"2021-06-29T16:12:57.6468Z","shell.execute_reply":"2021-06-29T16:12:57.847771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train the Model","metadata":{}},{"cell_type":"code","source":"model.fit(X_train, Y_train, epochs=20)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:13:03.693787Z","iopub.execute_input":"2021-06-29T16:13:03.694424Z","iopub.status.idle":"2021-06-29T16:13:37.591903Z","shell.execute_reply.started":"2021-06-29T16:13:03.694368Z","shell.execute_reply":"2021-06-29T16:13:37.59108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Evaluate the model","metadata":{}},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(X_test,  Y_test, verbose=2)\n\nprint('\\nTest accuracy:', test_accuracy*100,\"%\")","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:13:43.243294Z","iopub.execute_input":"2021-06-29T16:13:43.243646Z","iopub.status.idle":"2021-06-29T16:13:43.654606Z","shell.execute_reply.started":"2021-06-29T16:13:43.243617Z","shell.execute_reply":"2021-06-29T16:13:43.653515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loss, train_accuracy = model.evaluate(X_train,  Y_train, verbose=2)\n\nprint('\\nTrain accuracy:', train_accuracy*100,\"%\")","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:13:48.27273Z","iopub.execute_input":"2021-06-29T16:13:48.273132Z","iopub.status.idle":"2021-06-29T16:13:49.864967Z","shell.execute_reply.started":"2021-06-29T16:13:48.273097Z","shell.execute_reply":"2021-06-29T16:13:49.864009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###  Make predictions","metadata":{}},{"cell_type":"code","source":"pred = model.predict(test_data)\n\noutput = pd.DataFrame()\n\noutput['ImageId'] = list(range(1,28001))\n\npredictions = []\nfor p in pred:\n    predictions.append(np.argmax(p))\n \noutput['Label'] = predictions\n\noutput.to_csv(\"my_submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-29T16:13:54.036539Z","iopub.execute_input":"2021-06-29T16:13:54.036892Z","iopub.status.idle":"2021-06-29T16:13:55.131398Z","shell.execute_reply.started":"2021-06-29T16:13:54.03685Z","shell.execute_reply":"2021-06-29T16:13:55.130344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-06-29T16:13:58.87353Z","iopub.execute_input":"2021-06-29T16:13:58.873865Z","iopub.status.idle":"2021-06-29T16:13:58.882102Z","shell.execute_reply.started":"2021-06-29T16:13:58.873836Z","shell.execute_reply":"2021-06-29T16:13:58.88083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}