{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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)\nimport keras\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom keras.utils.np_utils import to_categorical\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#Read data files given in csv format.\ntrain_data = pd.read_csv('../input/Kannada-MNIST/train.csv')#train_data.shape = (60000,785)\ntest_data = pd.read_csv('../input/Kannada-MNIST/test.csv') #test_data.shape = (5000,785)\ndig_data = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv') #dig_data.shape = (10240,785)\nsample_submission_data = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv') #sample_submission_data.shape = (5000,2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Set Y values.\nY_train = train_data['label'] #Y_train.shape = (60000,)\nY_test = test_data['id'] #Y_test.shape = (5000,)\nY_dig = dig_data['label'] #Y_dig.shape = (10240,)\n\n#Set X values.\nX_train = train_data.drop(['label'], axis = 1) #X_train.shape = (60000,784)\nX_test = test_data.drop(['id'], axis = 1) #X_test.shape = (5000,784)\nX_dig = dig_data.drop(['label'], axis = 1) #X_dig.shape = (10240,785)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Show images in grayscale. For this aim we need to reshape images from 784 to 28x28.\nfig, axes = plt.subplots(2, 5, figsize=(15,5))\nfor i,ax in enumerate(axes.flat):\n    ax.imshow(X_train.to_numpy().reshape(60000, 28, 28)[i], cmap=\"gray\") #colormap = grayscale\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Convert from pandas dataframe to numpy array then reshape all data to (28x28x1) 3D matrices.\nX_train = X_train.to_numpy().reshape(60000, 28, 28, 1) \nX_test = X_test.to_numpy().reshape(5000, 28, 28, 1)\nX_dig = X_dig.to_numpy().reshape(10240, 28, 28, 1)\n\n#Make sure that the values are float so that we can get decimal points after division.\nX_train = X_train.astype('float32')\nX_test = X_test.astype('float32')\nX_dig = X_dig.astype('float32')\n\n#Normalize input values by dividing with 255. Output values are in between 0 and 1.\n#Rescale pixel values preferred for neural network models.\nX_train /= 255.0\nX_test /= 255.0\n\n#Label encoding.\nY_train = tf.keras.utils.to_categorical(Y_train, num_classes = 10, dtype='uint8')\n\n# Create validation data.\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.15, random_state=42) #Test data size is 15% of train data size.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Creating a sequential model and adding the layers.\n#CNN architecture consists of 6 layers. (Conv2D --> MaxPooling2D --> Flatten --> Dense --> Dropout --> Dense)\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(28, kernel_size=(3,3), input_shape=(28,28,1)))\nmodel.add(layers.MaxPooling2D(pool_size=(2, 2)))\nmodel.add(layers.Flatten()) # Flattening the arrays for fully connected layers.\nmodel.add(layers.Dense(128, activation=tf.nn.relu))\nmodel.add(layers.Dropout(0.2))\nmodel.add(layers.Dense(10,activation=tf.nn.softmax))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', #Optimizer that implements the Adam algorithm which is a stochastic gradient descent method that is based on adaptive estimation of first-order and second-order moments.\n              loss='categorical_crossentropy', #The categorical_crossentropy computes the cross-entropy loss between the true classes and predicted classes.\n              metrics=['accuracy']) #Metrics is a function that is used to judge the performance of your model.Accuracy metrics calculates how often predictions equal labels.\nmodel.fit(x=X_val,y=Y_val, epochs=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Check architecture of the model.\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Evaluate the model.\nmodel.evaluate(X_val, Y_val) #Achieved a test accuracy of over 99%.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Model will classify the image as a ‘4’ and here is the visual of the image.\nimage_index = 3456 #Change image_index and show other numbers.\nplt.imshow(X_val[image_index].reshape(28, 28),cmap='gray')\npred = model.predict(X_val[image_index].reshape(1, 28, 28, 1))\nprint(pred.argmax())","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}