{"cells":[{"metadata":{},"cell_type":"markdown","source":"We will use Convolutional Neural Networks to classify the Kannada digits. "},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# importing the essential libraries\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# importing the deep learning libraries\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# getting the training data from the location\ntrain_data = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Checking the distribution of the labels in the dataset\ntrain_data['label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The labels are equally distributed. "},{"metadata":{"trusted":true},"cell_type":"code","source":"# Splitting the training and validating data\nX = train_data.drop(['label'], axis = 1).values\ny = train_data[['label']].values\n\n# scaling the data\nX = X / 255.0\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2, random_state = 101)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(X_train.shape)\nprint(y_train.shape)\nprint(X_val.shape)\nprint(y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Reshaping the data\nX_train = X_train.reshape(48000, 28, 28, 1)\nX_val = X_val.reshape(12000, 28, 28, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(X_train.shape)\nprint(y_train.shape)\nprint(X_val.shape)\nprint(y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(X_train[0][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# the total number of outputs\noutput_classes = len(set(train_data['label']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# preparing the artificial neural network with CNN Layers\ncnn_model = Sequential()\ncnn_model.add(Conv2D(filters = 128, kernel_size = (3, 3), activation = 'relu', input_shape = (28, 28, 1)))\ncnn_model.add(MaxPooling2D(pool_size = (2, 2)))\n# cnn_model.add(Dropout(rate = 0.2))\ncnn_model.add(Conv2D(filters = 64, kernel_size = (3, 3), activation = 'relu'))\ncnn_model.add(MaxPooling2D(pool_size = (2, 2)))\ncnn_model.add(Conv2D(filters = 32, kernel_size = (3, 3), activation = 'relu'))\ncnn_model.add(Flatten())\ncnn_model.add(Dense(units = 128, activation = 'relu'))\n# cnn_model.add(Dropout(rate = 0.2))\ncnn_model.add(Dense(units = 64, activation = 'relu'))\n# cnn_model.add(Dropout(rate = 0.1))\ncnn_model.add(Dense(units = output_classes, activation = 'softmax'))\n\ncnn_model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fitted_model = cnn_model.fit(X_train, y_train, epochs = 10, validation_data = (X_val, y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"accuracy = fitted_model.history['acc']\nx_axis = range(len(accuracy))\nplt.plot(x_axis, accuracy, 'o', label = 'accuracy plot')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we can include the test data and test it."},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ntest_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = test_data.drop(['id'], axis = 1)\nX = X / 255.0\nprint(X.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = X.values.reshape(5000, 28, 28, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = cnn_model.predict_classes(X)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Preparing a file for submitting"},{"metadata":{"trusted":true},"cell_type":"code","source":"# submission = pd.DataFrame({'id' : test_data['id'], 'label' : predictions})\n# submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')\nsubmission['label'] = predictions\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Exporting the dataframe"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Thank you for viewing my kernel and your kind encouragement."}],"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":1}