{"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":"import pandas as pd \nimport numpy as np\nimport matplotlib.pyplot as plt \nimport seaborn as sns \nimport tensorflow as tf \nimport keras ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print (\"we are usint tensorflow version :\" ,tf.__version__)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/Kannada-MNIST/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv(\"../input/Kannada-MNIST/test.csv\")\nDig = pd.read_csv(\"../input/Kannada-MNIST/Dig-MNIST.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Dig.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print (\"training shape \",train.shape,'\\n',\"testing shape \",test.shape,'\\n',\"Dig shape \",Dig.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = train.drop(columns=['label'],axis=1)\nX_val = Dig.drop(columns=['label'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = train['label']\ny_val = Dig['label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print (X.shape,X_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print (y.shape,y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = X / 255.0\nX_val = X_val / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = tf.keras.utils.to_categorical(y, num_classes=10)\ny_val = tf.keras.utils.to_categorical(y_val, num_classes=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# visualizing the first 10 images in the dataset and their labels\n%matplotlib inline\nimport matplotlib.pyplot as plt\nplt.figure(figsize=(10, 1))\nfor i in range(10):\n    plt.subplot(1, 10, i+1)\n    plt.imshow(X.iloc[i].values.reshape(28, 28), cmap=\"gray\")\n    plt.axis('off')\n    print('label for each of the below image: %s' % (np.argmax(y[0:10][i])))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(train['label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X =np.array(X).astype('float32')\ny = np.array(y).astype('float32')\nX_val =np.array(X_val).astype('float32')\ny_val = np.array(y_val).astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print ('traing set',X.shape,y.shape)\nprint ('test set',X_val.shape,y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.models.Sequential() # Instantiating keras sequential models from keras \nLambda = 0.001\n# First layer (input layer) of  28*28 = 784 after flattening the image of 28 * 28 picxels\nmodel.add(tf.keras.layers.Dense(784,input_dim=784,kernel_initializer='uniform', activation='relu'))\n# second layer \nmodel.add(tf.keras.layers.Dense(181, kernel_initializer='uniform', activation='relu'))\n# third layer\n#model.add(tf.keras.layers.Dense(181, kernel_initializer='uniform', activation='relu',kernel_regularizer=tf.keras.regularizers.l2(Lambda)))\n#fourth layer \n#model.add(tf.keras.layers.Dense(90, kernel_initializer='uniform', activation='relu',kernel_regularizer=tf.keras.regularizers.l2(Lambda)))\n# Final layer with activation function as softmax and 10 neurons \nmodel.add(tf.keras.layers.Dense(10, activation='softmax'))\n\n\n\n# Create optimizer with non-default learning rate\nsgd_optimizer =  tf.keras.optimizers.SGD(lr=0.02, decay=1e-6, momentum=0.9)\n\n# Compile the model\nmodel.compile(optimizer=sgd_optimizer, loss='binary_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(X,y,validation_data=(X_val,y_val),epochs=50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"id = test['id']\ntest = test.drop(columns=['id'])\npredict = model.predict(test)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = np.argmax(predict,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"my_submission = pd.DataFrame({'id': id, 'label': labels})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"my_submission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"my_submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.distplot(my_submission['label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"my_submission[my_submission['label']==3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"my_submission.shape","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}