{"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":"2022-08-25T15:39:56.968784Z","iopub.execute_input":"2022-08-25T15:39:56.969252Z","iopub.status.idle":"2022-08-25T15:39:56.989778Z","shell.execute_reply.started":"2022-08-25T15:39:56.969218Z","shell.execute_reply":"2022-08-25T15:39:56.988777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n<div style=\"border-radius:20px;\n            border : black solid;\n            background-color: ##FFFFFF;\n            font-size:200%;\n            text-align: left\">\n\n<h1 style='; border:0; border-radius: 15px; text-shadow: 1px 1px black; font-weight: bold; color:green'><center>MNIST DIGITS CLASSIFICATION</center></h1>","metadata":{}},{"cell_type":"markdown","source":"![](https://thumbs.gfycat.com/AffectionateMemorableGreyhounddog-size_restricted.gif)","metadata":{}},{"cell_type":"markdown","source":"## **Index:**\n\n- [Importing Essential libraries](#import)\n- [Importing the data](#ak)\n- [Explore Data Analysis(EDA)](#eda)\n    - [Normalization of data](#normalize)\n- [Data Visualization](#visualize)\n- [Model Creation and Evalutation](#model)\n- [Submission](#sv)","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius:10px;\n            border : black solid;\n            background-color:  #FFA07A;\n            font-size:110%;\n            text-align: left\">\n​\n<h2 style='; border:0; border-radius: 15px; text-shadow: 1px 1px black; font-weight: bold; color:black'><center> PROBLEM STATMENT </center></h2>","metadata":{}},{"cell_type":"markdown","source":"**Competition Description**\n\nMNIST (\"Modified National Institute of Standards and Technology\") is the de facto “hello world” dataset of computer vision. Since its release in 1999, this classic dataset of handwritten images has served as the basis for benchmarking classification algorithms. As new machine learning techniques emerge, MNIST remains a reliable resource for researchers and learners alike.\n\nIn this competition, your goal is to correctly identify digits from a dataset of tens of thousands of handwritten images. We’ve curated a set of tutorial-style kernels which cover everything from regression to neural networks. We encourage you to experiment with different algorithms to learn first-hand what works well and how techniques compare.","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius:10px;\n            border : black solid;\n            background-color:  #FFA07A;\n            font-size:110%;\n            text-align: left\">\n    <h2 style='; border:0; border-radius: 15px; text-shadow: 1px 1px black; font-weight: bold; color:black'><center> Importing relevant Libraries </center></h2><a id=\"import\"></a>","metadata":{}},{"cell_type":"code","source":"import plotly.express as px\n\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os\nimport tensorflow as tf\nfrom glob import glob\nimport sys\nimport sklearn.metrics as metrics\nfrom sklearn.metrics import accuracy_score,classification_report,confusion_matrix\nimport seaborn as sns\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:39:56.991682Z","iopub.execute_input":"2022-08-25T15:39:56.992203Z","iopub.status.idle":"2022-08-25T15:39:57.001259Z","shell.execute_reply.started":"2022-08-25T15:39:56.992159Z","shell.execute_reply":"2022-08-25T15:39:56.999984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px;\n            border : black solid;\n            background-color:  #FFA07A;\n            font-size:110%;\n            text-align: left\">\n    <h2 style='; border:0; border-radius: 15px; text-shadow: 1px 1px black; font-weight: bold; color:black'><center> Importing Data </center></h2><a id=\"ak\"></a>","metadata":{}},{"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":"2022-08-25T15:39:57.008309Z","iopub.execute_input":"2022-08-25T15:39:57.009153Z","iopub.status.idle":"2022-08-25T15:40:01.400164Z","shell.execute_reply.started":"2022-08-25T15:39:57.009078Z","shell.execute_reply":"2022-08-25T15:40:01.398604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px;\n            border : black solid;\n            background-color:  #FFA07A;\n            font-size:110%;\n            text-align: left\">\n    <h2 style='; border:0; border-radius: 15px; text-shadow: 1px 1px black; font-weight: bold; color:black'><center> Split Data </center></h2><a id=\"eda\"></a>","metadata":{}},{"cell_type":"markdown","source":"![](https://cdn-images-1.medium.com/max/1000/1*Owa2rsDG6Rwv1IM_RdsL3A.gif)","metadata":{}},{"cell_type":"code","source":"x_Train = train.drop(['label'], axis=1).astype('float32').values.reshape(-1,28,28,1)\ny_Train = train['label'].astype('int32')\nx_Test = test.values.reshape(-1,28,28,1)\ny_Test = test.values.reshape(-1,28,28,1)\nx_Train.shape, y_Train.shape, x_Test.shape, y_Test","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:40:01.414011Z","iopub.execute_input":"2022-08-25T15:40:01.414570Z","iopub.status.idle":"2022-08-25T15:40:01.628565Z","shell.execute_reply.started":"2022-08-25T15:40:01.414536Z","shell.execute_reply":"2022-08-25T15:40:01.627260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualizing Label counts\nfig = px.histogram(train, x = 'label', nbins=20, template = 'plotly_dark',\n                  title = 'Count for Every Label in the Dataset')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:40:01.631302Z","iopub.execute_input":"2022-08-25T15:40:01.631722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Max value in Training data:', np.amax(x_Train), 'Min value in Training data:', np.amin(x_Train))\nprint('Max value in Testing data:', np.amax(x_Test), 'Min value in Testing data:', np.amin(x_Test))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data normalization\nx_Train= x_Train/255\nx_Test = x_Test/255","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = np.random.randint(1, x_Train.shape[0]+1)\nplt.imshow(x_Train[index][:,:,0], )\nplt.title(y_Train[index]);\nprint('y_Train Label: ', y_Train[index])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px;\n            border : black solid;\n            background-color:  #FFA07A;\n            font-size:110%;\n            text-align: left\">\n    <h2 style='; border:0; border-radius: 15px; text-shadow: 1px 1px black; font-weight: bold; color:black'><center> Model Creation and Evalutation </center></h2><a id=\"model\"></a>","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\ntf.keras.layers.Conv2D(32, (2, 2), activation='relu', input_shape=x_Train.shape[1:]),\ntf.keras.layers.Activation(\"relu\"),\ntf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\ntf.keras.layers.Conv2D(32, (4, 4), activation='relu'),\ntf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\ntf.keras.layers.Flatten(),\ntf.keras.layers.Dense(128, activation='relu'),\ntf.keras.layers.Dense(10, activation='softmax')\n])\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss= 'sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(x_Train, y_Train, epochs=10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px;\n            border : black solid;\n            background-color:  #FFA07A;\n            font-size:110%;\n            text-align: left\">\n    <h2 style='; border:0; border-radius: 15px; text-shadow: 1px 1px black; font-weight: bold; color:black'><center>Result </center></h2><a id=\"sv\"></a>\n","metadata":{}},{"cell_type":"code","source":"y_Test = model.predict(x_Test)\n\ny_Test = np.argmax(y_Test, axis=1)\nplt.imshow(x_Test[5])\nf'Predicted label is: {y_Test[5]}'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://st2.depositphotos.com/1006899/7664/i/600/depositphotos_76643019-stock-photo-thank-you-words.jpg)","metadata":{}}]}