{"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)\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 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Flatten,Dropout\nfrom keras.layers.convolutional import Conv2D, MaxPooling2D\nfrom keras.optimizers import RMSprop,Adam, SGD\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"CV = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')\ntrain = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\nx_cv = CV.loc[:,'pixel0':].values\ny_cv = CV.loc[:,:'label'].values\nx_train = train.loc[:,'pixel0':].values\ny_train = train.loc[:,:'label'].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('x_train shape: {} y_train shape: {}'.format(x_train.shape, y_train.shape))\nprint('x_cv shape: {} y_cv shape: {}'.format(x_cv.shape, y_cv.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_cv = to_categorical(y_cv)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_classes = y_train.shape[1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_rows = 28\nimg_cols = 28\n\nx_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\nx_cv = x_cv.reshape(x_cv.shape[0], img_rows, img_cols, 1)\n\ninput_shape = (img_rows, img_cols, 1)\n\nx_train = x_train.astype('float32')\nx_cv = x_cv.astype('float32')\n\nx_train = x_train/255\nx_cv = x_cv/255\n\nprint('x_train shape: {} y_train shape: {}'.format(x_train.shape, y_train.shape))\nprint('x_cv shape: {} y_cv shape: {}'.format(x_cv.shape, y_cv.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, kernel_size=(5,5), activation= 'relu', padding = 'Same',input_shape=input_shape))\nmodel.add(Conv2D(64, (3,3), activation= 'relu'))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(32, (3,3), activation= 'relu', padding = 'Same'))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation= 'relu'))\nmodel.add(Dropout(0.50))\nmodel.add(Dense(num_classes, activation= 'softmax'))\n\nmodel.compile(optimizer=SGD(0.01), loss='categorical_crossentropy', metrics=['accuracy'])\nprint(model.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 32\nepochs = 20\n\nhistory = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1, validation_data=(x_cv, y_cv))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nhistory_dict = history.history\n\ntr_loss = history_dict['loss']\ncv_loss = history_dict['val_loss']\nepochs = range(1, len(tr_loss)+1)\nplt.plot(epochs,tr_loss, label='Train Loss')\nplt.plot(epochs,cv_loss, label='CV Loss')\nplt.title('Loss Evaluation')\nplt.grid()\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history_dict = history.history\n\ntr_acc = history_dict['accuracy']\ncv_acc = history_dict['val_accuracy']\nepochs = range(1, len(tr_acc)+1)\nplt.plot(epochs,tr_acc, label='Train Acc')\nplt.plot(epochs,cv_acc, label='CV Acc')\nplt.title('Accuracy Evaluation')\nplt.grid()\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ntest_id = test.loc[:,:'id'].values\nx_test = test.loc[:,'pixel0':].values\nx_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\nx_test = x_test.astype('float32')\nx_test = x_test/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = model.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label = np.argmax(pred, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_csv = pd.DataFrame(test['id'].values,columns=['id'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_csv['label'] = label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_csv.to_csv('submission.csv', index=False)","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}