{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as  np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n\nfrom keras.utils.np_utils import to_categorical\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.callbacks import ReduceLROnPlateau","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\n\nimg_rows, img_cols = 28, 28\nnum_classes = 10\n\ndef prep_data(raw):\n    y = raw[:, 0]\n    out_y = keras.utils.to_categorical(y, num_classes)\n    \n    x = raw[:,1:]\n    num_images = raw.shape[0]\n    out_x = x.reshape(num_images, img_rows, img_cols, 1)\n    out_x = out_x / 255\n    return out_x, out_y\n\nfashion_file = \"../input/Kannada-MNIST/train.csv\"\nfashion_data = np.loadtxt(fashion_file, skiprows=1, delimiter=',')\nx, y = prep_data(fashion_data)\n\nprint(\"Setup Complete\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Start the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D\n\n\n# Your Code Here\nmodel=Sequential()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Add the first layer"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.add(Conv2D(12, kernel_size=(3,3),\n                 activation='relu',\n                 input_shape=(img_rows, img_cols,1)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Add the remaining layers"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.add(Conv2D(20, activation='relu', kernel_size=3))\nmodel.add(Conv2D(20, activation='relu', kernel_size=3))\nmodel.add(Flatten())\nmodel.add(Dense(100, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Compile Your Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss = \"categorical_crossentropy\",\n             optimizer = 'adam',\n             metrics = ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Fit The Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(x,y,\n                 batch_size = 100,\n                 epochs = 4,\n                 validation_split = 0.2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Create A New Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"#define model name\nsecond_model=Sequential()\n\n#fist lay\nsecond_model.add(Conv2D(17, kernel_size=(3,3),\n                 activation='relu',\n                 input_shape=(img_rows, img_cols,1)))\n\n#the remaining layers\nsecond_model.add(Conv2D(50, activation='relu', kernel_size=3))\nsecond_model.add(Conv2D(50, activation='relu', kernel_size=3))\nsecond_model.add(Flatten())\nsecond_model.add(Dense(100, activation='relu'))\nsecond_model.add(Dense(10, activation='softmax'))\n\n#compile\nsecond_model.compile(loss = \"categorical_crossentropy\",\n             optimizer = 'adam',\n             metrics = ['accuracy'])\n\n#fit/train model\nsecond_model.fit(x,y,\n                 batch_size = 100,\n                 epochs = 7,\n                 validation_split = 0.2)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Loading data"},{"metadata":{"trusted":true},"cell_type":"code","source":"train=pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest=pd.read_csv('../input/Kannada-MNIST/test.csv')\nsample_sub=pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Understanding the data <a id=\"1\" ></a>"},{"metadata":{},"cell_type":"markdown","source":"Before jumping to all complex stuff about Convolutions and all,we will simply understand our data.We will learn and gain basic understanding about this data."},{"metadata":{"trusted":true},"cell_type":"code","source":"#print('The Train  dataset has {} rows and {} columns'.format(train.shape[0],train.shape[1]))\n#print('The Test  dataset has {} rows and {} columns'.format(test.shape[0],test.shape[1]))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#train.head(3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now you can see that there are 785 columns in the training dataset given.I will describe each one of them here...\n- **Label :** This contains the label which are going to predict.That is our target value.Here it is numbers from 0 to 9.We will plot a bar graph and see the distribution of this target value later.\n- **Pixel0 to Pixel783: **These are the pixel values of the image metrics.That is each row contains 28 * 28 = 784 (0-783 here) values here.Each one of these values indicates the pixel value at i x 28 + j th pixel position in the image metric.Simple !\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"#test.head(3)\n#test=test.drop('id',axis=1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test=pd.read_csv('../input/Kannada-MNIST/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntest=test.drop('id',axis=1)\ntest=test/255\ntest=test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pre=second_model.predict(test)     ##making prediction\ny_pre=np.argmax(y_pre,axis=1) ##changing the prediction intro labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub['label']=y_pre\nsample_sub.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub.head()","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}