{"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"root_path = '/kaggle/input/Kannada-MNIST/'\noutput_path = '/kaggle/working/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(root_path + 'train.csv')\ndf.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"examples = df.values[:10, 1:]\nplt.figure(1, figsize=(15, 6))\nfor i in range(10):\n  plt.subplot(2, 5, i + 1)\n  plt.imshow(examples[i].reshape((28, 28)), cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_data = df.values[:, 1:]\ny_data = df.label.values\n\nX_data.shape, y_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df2 = pd.read_csv(root_path + 'Dig-MNIST.csv')\ndf2.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"examples = df2.values[:10, 1:]\nplt.figure(1, figsize=(15, 6))\nfor i in range(10):\n  plt.subplot(2, 5, i + 1)\n  plt.imshow(examples[i].reshape((28, 28)), cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_X = df2.values[:, 1:]\ntest_y = df2.label.values\n\ntest_X.shape, test_y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"size = X_data.shape[0]\nperm = np.random.permutation(size)\n\nX_data = X_data[perm]\ny_data = y_data[perm]\n\ntrain_size = int(size / 10 * 7.5)\ntrain_X, valid_X = X_data[:train_size], X_data[train_size:]\ntrain_y, valid_y = y_data[:train_size], y_data[train_size:]\n\ntrain_X.shape, train_y.shape, test_X.shape, test_y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_mean = np.mean(train_X)\ntrain_X = train_X - train_mean\n\ntrain_max = np.max(np.abs(train_X))\ntrain_X = train_X / train_max\n\ntrain_mean, train_max","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_X = (valid_X - train_mean) / train_max\ntest_X = (test_X - train_mean) / train_max","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers import *\nfrom keras.models import Model\nfrom keras.callbacks import CSVLogger, ModelCheckpoint","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = Input(shape=(784,))\ny = Dense(20, activation=None)(x)\ny = Activation('elu')(y)\ny = Dropout(rate=0.3)(y)\ny = Dense(20, activation=None)(y)\ny = Activation('elu')(y)\nprediction = Dense(10, activation='softmax')(y)\n\nmodel = Model(inputs=[x], output=[prediction])\n\nmodel.compile(optimizer ='sgd',\n              loss = 'sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(train_X, train_y,\n          batch_size=16,\n          epochs=20,\n          verbose=1,\n          validation_data=(valid_X, valid_y),\n          callbacks=[\n              CSVLogger(output_path + 'log.csv'),\n              ModelCheckpoint(output_path + 'model.h5', save_best_only=True),\n          ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nmodel = keras.models.load_model(output_path + 'model.h5')\npred_probas = model.predict(test_X, batch_size=16)\nprediction = pred_probas.argmax(axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(test_y, prediction)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df3 = pd.read_csv(root_path + 'test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"examples = df3.values[:10, 1:]\nplt.figure(1, figsize=(15, 6))\nfor i in range(10):\n  plt.subplot(2, 5, i + 1)\n  plt.imshow(examples[i].reshape((28, 28)), cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df3.shape\ndf3.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = df3.id.values\nres_X = df3.values[:, 1:]\n\nres_X.shape, ids.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res_X = (res_X - train_mean) / train_max","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_probas = model.predict(res_X, batch_size=16)\nprediction = pred_probas.argmax(axis=1)\n\nprediction[:10], prediction.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv(root_path + 'sample_submission.csv')\nsubmission['label'] = prediction\n\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":1}