{"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/digit-recognizer'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB 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-10-31T09:01:00.542531Z","iopub.execute_input":"2021-10-31T09:01:00.543131Z","iopub.status.idle":"2021-10-31T09:01:00.550226Z","shell.execute_reply.started":"2021-10-31T09:01:00.543092Z","shell.execute_reply":"2021-10-31T09:01:00.549571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint(train.shape)\nprint(test.shape)","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-10-31T09:01:00.551391Z","iopub.execute_input":"2021-10-31T09:01:00.551835Z","iopub.status.idle":"2021-10-31T09:01:05.507926Z","shell.execute_reply.started":"2021-10-31T09:01:00.551806Z","shell.execute_reply":"2021-10-31T09:01:05.50677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Our dataset currently has 42000 rows and 785 column.","metadata":{}},{"cell_type":"code","source":"y_train = train['label']\nX_train = train.drop(labels = [\"label\"],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:05.510117Z","iopub.execute_input":"2021-10-31T09:01:05.510494Z","iopub.status.idle":"2021-10-31T09:01:05.626971Z","shell.execute_reply.started":"2021-10-31T09:01:05.510452Z","shell.execute_reply":"2021-10-31T09:01:05.625844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:05.63027Z","iopub.execute_input":"2021-10-31T09:01:05.630696Z","iopub.status.idle":"2021-10-31T09:01:05.643506Z","shell.execute_reply.started":"2021-10-31T09:01:05.630652Z","shell.execute_reply":"2021-10-31T09:01:05.642675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the data\n\n\"\"\" the goal of normalization is to change the values of numeric columns in the dataset \n    to a common scale, without distorting differences in the ranges of values.\"\"\"\nX_train = X_train / 255.0  \ntest = test / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:05.645037Z","iopub.execute_input":"2021-10-31T09:01:05.645444Z","iopub.status.idle":"2021-10-31T09:01:05.825955Z","shell.execute_reply.started":"2021-10-31T09:01:05.645402Z","shell.execute_reply":"2021-10-31T09:01:05.825056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting our dataset into (28,28,1) as default image.\nX_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:05.827615Z","iopub.execute_input":"2021-10-31T09:01:05.828017Z","iopub.status.idle":"2021-10-31T09:01:05.833743Z","shell.execute_reply.started":"2021-10-31T09:01:05.827973Z","shell.execute_reply":"2021-10-31T09:01:05.832826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(y_train.shape)\nprint(test.shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:05.835137Z","iopub.execute_input":"2021-10-31T09:01:05.835651Z","iopub.status.idle":"2021-10-31T09:01:05.846336Z","shell.execute_reply.started":"2021-10-31T09:01:05.835613Z","shell.execute_reply":"2021-10-31T09:01:05.845625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have 42000 training data and 28000 Test data","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\ny_train = to_categorical(y_train, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:05.847335Z","iopub.execute_input":"2021-10-31T09:01:05.847755Z","iopub.status.idle":"2021-10-31T09:01:10.387471Z","shell.execute_reply.started":"2021-10-31T09:01:05.847699Z","shell.execute_reply":"2021-10-31T09:01:10.386669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"With one-hot, we convert each categorical value into a new categorical column and assign a binary value of 1 or 0 ,<br>\nto those columns. Each integer value is represented as a binary vector.","metadata":{}},{"cell_type":"code","source":"#Using matplotlib to show a random image in our training data.\nplt.imshow(X_train[50][:,:,0])","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:10.388515Z","iopub.execute_input":"2021-10-31T09:01:10.388822Z","iopub.status.idle":"2021-10-31T09:01:10.555758Z","shell.execute_reply.started":"2021-10-31T09:01:10.388787Z","shell.execute_reply":"2021-10-31T09:01:10.554848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_val,y_train,y_val = train_test_split(X_train,y_train,test_size= 0.2,random_state = 101)\n#Splitting data into training and validation to check whether model is not overfitting nor underfitting.","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:10.557269Z","iopub.execute_input":"2021-10-31T09:01:10.557658Z","iopub.status.idle":"2021-10-31T09:01:11.637039Z","shell.execute_reply.started":"2021-10-31T09:01:10.557617Z","shell.execute_reply":"2021-10-31T09:01:11.636064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers, models\n\nmodel = models.Sequential()\n","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:11.638577Z","iopub.execute_input":"2021-10-31T09:01:11.638925Z","iopub.status.idle":"2021-10-31T09:01:11.65397Z","shell.execute_reply.started":"2021-10-31T09:01:11.638893Z","shell.execute_reply":"2021-10-31T09:01:11.653175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(layers.Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(layers.Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(layers.MaxPool2D(pool_size=(2,2)))\nmodel.add(layers.Dropout(0.25))\n\n\nmodel.add(layers.Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(layers.Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(layers.Dropout(0.25))\n\n\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation = \"relu\"))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(10, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:11.658056Z","iopub.execute_input":"2021-10-31T09:01:11.658303Z","iopub.status.idle":"2021-10-31T09:01:11.86127Z","shell.execute_reply.started":"2021-10-31T09:01:11.658279Z","shell.execute_reply":"2021-10-31T09:01:11.860409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:11.862509Z","iopub.execute_input":"2021-10-31T09:01:11.862792Z","iopub.status.idle":"2021-10-31T09:01:11.870741Z","shell.execute_reply.started":"2021-10-31T09:01:11.862763Z","shell.execute_reply":"2021-10-31T09:01:11.869088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)\n#RMSprop is a gradient-based optimization technique used in training neural networks.\n#Using RMSprop to minimize an error function(loss function).\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:11.872076Z","iopub.execute_input":"2021-10-31T09:01:11.872351Z","iopub.status.idle":"2021-10-31T09:01:11.887731Z","shell.execute_reply.started":"2021-10-31T09:01:11.872325Z","shell.execute_reply":"2021-10-31T09:01:11.886998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, batch_size = 100, epochs = 10, validation_data = (X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:01:11.888802Z","iopub.execute_input":"2021-10-31T09:01:11.88904Z","iopub.status.idle":"2021-10-31T09:19:20.342646Z","shell.execute_reply.started":"2021-10-31T09:01:11.889017Z","shell.execute_reply":"2021-10-31T09:19:20.34182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.epoch,history.history['accuracy'],label = \"accuracy\") # Accuracy curve for training set\nplt.plot(history.epoch,history.history['val_accuracy'],label = \"val_accuracy\") # Accuracy curve for validation set\n\nplt.title(\"Accuracy Curve\",fontsize=18)\nplt.xlabel(\"Epochs\",fontsize=15)\nplt.ylabel(\"Accuracy\",fontsize=15)\nplt.grid(alpha=0.3)\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:19:20.343875Z","iopub.execute_input":"2021-10-31T09:19:20.344153Z","iopub.status.idle":"2021-10-31T09:19:20.5226Z","shell.execute_reply.started":"2021-10-31T09:19:20.344126Z","shell.execute_reply":"2021-10-31T09:19:20.521826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Lets predict our model using random image in test data\nplt.imshow(test[24][:,:,0])\n","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:20:05.665798Z","iopub.execute_input":"2021-10-31T09:20:05.666116Z","iopub.status.idle":"2021-10-31T09:20:05.794672Z","shell.execute_reply.started":"2021-10-31T09:20:05.666089Z","shell.execute_reply":"2021-10-31T09:20:05.793947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Our random test image shows number 6.Lets test on our model.","metadata":{}},{"cell_type":"code","source":"prediction = model.predict(test[24].reshape(-1,28,28,1))\nprediction = np.argmax(prediction,axis = 1)\nprint(\"Our model predicts for the image is Digit:\",prediction)\n#results = pd.Series(results,name=\"Label\")\n","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:20:10.777981Z","iopub.execute_input":"2021-10-31T09:20:10.778455Z","iopub.status.idle":"2021-10-31T09:20:10.810819Z","shell.execute_reply.started":"2021-10-31T09:20:10.778421Z","shell.execute_reply":"2021-10-31T09:20:10.809901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Similarly we will predict for the test and submit it.\nresults = model.predict(test)\nresults = np.argmax(results,axis = 1)\nresults = pd.Series(results,name=\"Label\")\n","metadata":{"execution":{"iopub.status.busy":"2021-10-31T09:19:20.807971Z","iopub.execute_input":"2021-10-31T09:19:20.808238Z","iopub.status.idle":"2021-10-31T09:19:40.120174Z","shell.execute_reply.started":"2021-10-31T09:19:20.808212Z","shell.execute_reply":"2021-10-31T09:19:40.119403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}