{"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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from numpy import mean\nfrom numpy import std\nimport pandas as pd \nimport numpy as np\nfrom matplotlib import pyplot\nfrom sklearn.model_selection import KFold\nfrom keras.datasets import mnist\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom keras.optimizers import SGD\nimport pandas as pd \n\n\ntrain=  pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest=  pd.read_csv('../input/Kannada-MNIST/test.csv')\n\n\n\n\n\n# example of loading the mnist dataset\nfrom keras.datasets import mnist\nfrom matplotlib import pyplot\n\n\n\n\ndef load_dataset():\n\t# load dataset\n\t#(trainX, trainY), (testX, testY) = df.load_data()\n\t# reshape dataset to have a single channel\n\ttrainX = trainX.reshape((trainX.shape[0], 28, 28, 1))\n\ttestX = testX.reshape((testX.shape[0], 28, 28, 1))\n\t# one hot encode target values\n\ttrainY = to_categorical(trainY)\n\ttestY = to_categorical(testY)\n\treturn trainX, trainY, testX, testY\n\n# scale pixels\ndef prep_pixels(train, test):\n\t# convert from integers to floats\n\ttrain_norm = train.astype('float32')\n\ttest_norm = test.astype('float32')\n\t# normalize to range 0-1\n\ttrain_norm = train_norm / 255.0\n\ttest_norm = test_norm / 255.0\n\t# return normalized images\n\treturn train_norm, test_norm\n\n# define cnn model\ndef define_model():\n\tmodel = Sequential()\n\tmodel.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))\n\tmodel.add(MaxPooling2D((2, 2)))\n\tmodel.add(Flatten())\n\tmodel.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))\n\tmodel.add(Dense(10, activation='sigmoid'))\n\t# compile model\n\t#opt = SGD(lr=0.01, momentum=0.9) \n\tmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\treturn model\n\n# evaluate a model using k-fold cross-validation\ndef evaluate_model(dataX, dataY, n_folds=2):\n\tscores, histories = list(), list()\n\t# prepare cross validation\n\tkfold = KFold(n_folds, shuffle=True, random_state=1)\n\t# enumerate splits\n\tfor train_ix, test_ix in kfold.split(dataX):\n\t\t# define model\n\t\tmodel = define_model()\n\t\t# select rows for train and test\n\t\ttrainX, trainY, testX, testY = dataX[train_ix], dataY[train_ix], dataX[test_ix], dataY[test_ix]\n\t\t# fit model\n\t\thistory = model.fit(trainX, trainY, epochs=10, batch_size=32, validation_data=(testX, testY), verbose=0)\n\t\t# evaluate model\n\t\t_, acc = model.evaluate(testX, testY, verbose=0)\n\t\tprint('> %.3f' % (acc * 100.0))\n\t\t# stores scores\n\t\tscores.append(acc)\n\t\thistories.append(history)\n\treturn scores, histories\n\n# plot diagnostic learning curves\ndef summarize_diagnostics(histories):\n\tfor i in range(len(histories)):\n\t\t# plot loss\n\t\tpyplot.subplot(2, 1, 1)\n\t\tpyplot.title('Cross Entropy Loss')\n\t\tpyplot.plot(histories[i].history['loss'], color='blue', label='train')\n\t\tpyplot.plot(histories[i].history['val_loss'], color='orange', label='test')\n\t\t# plot accuracy\n\t\tpyplot.subplot(2, 1, 2)\n\t\tpyplot.title('Classification Accuracy')\n\t\tpyplot.plot(histories[i].history['accuracy'], color='blue', label='train')\n\t\tpyplot.plot(histories[i].history['val_accuracy'], color='orange', label='test')\n\tpyplot.show()\n\n# summarize model performance\ndef summarize_performance(scores):\n\t# print summary\n\tprint('Accuracy: mean=%.3f std=%.3f, n=%d' % (mean(scores)*100, std(scores)*100, len(scores)))\n\t# box and whisker plots of results\n\tpyplot.boxplot(scores)\n\tpyplot.show()\n\n# run the test harness for evaluating a model\ndef run_test_harness():\n  trainX=train.iloc[:,1:].values \n  trainY=train.iloc[:,0].values \n  trainX = trainX.reshape(trainX.shape[0], 28, 28,1) \n  trainY = to_categorical(trainY, 10) \n  testX=test.drop('id', axis=1).iloc[:,:].values\n  testX = testX.reshape(testX.shape[0], 28, 28,1)\n  #trainY = train.loc[:,'label'].values\n  #trainX = train.loc[:,'pixel0':].values\n  #testY= test.loc[:,'id'].values\n  #testX=test.loc[:,'pixel0':].values\n\t# load dataset\n\t#trainX, trainY, testX, testY = load_dataset()\n\t# prepare pixel data\n  trainX, testX = prep_pixels(trainX, testX)\n\t# evaluate model\n  scores, histories = evaluate_model(trainX, trainY)\n\t# learning curves\n  summarize_diagnostics(histories)\n\t# summarize estimated performance\n  summarize_performance(scores)\n\n# entry point, run the test harness\nrun_test_harness()","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}