{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"id":"finK_SDiWErT","colab_type":"text"},"cell_type":"markdown","source":"**1. Importing Required Library**"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"id":"98dYU2BPkfo7","colab_type":"code","colab":{}},"cell_type":"code","source":"## importing the required library\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n## keras modules\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nfrom keras import optimizers\nfrom keras.layers import LeakyReLU","execution_count":null,"outputs":[]},{"metadata":{"id":"1wBQ8xY_V7lJ","colab_type":"text"},"cell_type":"markdown","source":"**2. Loading Data**"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"id":"4Q9d9PqjkfpC","colab_type":"code","colab":{}},"cell_type":"code","source":"##Loading data\ndf_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"id":"UXPOpgscWQco","colab_type":"text"},"cell_type":"markdown","source":"**3. Data Visulatization**"},{"metadata":{"id":"n2uFsrtkXgT-","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":466},"outputId":"27979230-f192-4351-b439-5a270a32316a","trusted":true},"cell_type":"code","source":"## Label Distribution of the data\nlabels = df_train[\"label\"]\ndist = labels.value_counts()\nprint(dist)\nsns.barplot(dist.index,dist)","execution_count":null,"outputs":[]},{"metadata":{"id":"-Jf9lEzykxtx","colab_type":"text"},"cell_type":"markdown","source":"**Converting Data to numpy array**"},{"metadata":{"trusted":true,"id":"mq5QV8F6kfpN","colab_type":"code","colab":{}},"cell_type":"code","source":"### Converting the data to a numpy array\ntrain = np.array(df_train)\nY_train = train[:,0]\nX_train = train[:,1:]","execution_count":null,"outputs":[]},{"metadata":{"id":"gAPDl2uvk5EV","colab_type":"text"},"cell_type":"markdown","source":"**Splitting the Data into Train and  Validation Sets.**"},{"metadata":{"id":"OzhCAliTWlFo","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"### Split the train and the validation set for the fitting\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=np.random.seed(1))","execution_count":null,"outputs":[]},{"metadata":{"id":"Dyr-cSvReIED","colab_type":"text"},"cell_type":"markdown","source":"**Designing The Model**"},{"metadata":{"trusted":true,"id":"759D7jDykfpa","colab_type":"code","outputId":"e5b9e9ce-345f-4aea-ced1-4c3a2d9bc98c","colab":{"base_uri":"https://localhost:8080/","height":289}},"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(800, input_dim=784, activation=\"sigmoid\",kernel_initializer='random_normal'))\nmodel.add(Dense(600, activation=LeakyReLU(0.3)))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(200, activation=LeakyReLU(0.3)))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(100, activation=LeakyReLU(0.5)))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(50, activation=LeakyReLU(0.5)))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(10, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"id":"bDYmjiKalAUB","colab_type":"text"},"cell_type":"markdown","source":"**Deploying the model**"},{"metadata":{},"cell_type":"markdown","source":"The accuracy will increase if rerun the model fit again with more epochs as the training will proceed on top of pretrained model weights (transfer learning)."},{"metadata":{"id":"fHP8zh-dZGfS","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":1000},"outputId":"4e085485-d981-4ce7-9450-0f8da547d5c2","trusted":true},"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nmodel.fit(X_train, Y_train, epochs=100, batch_size=512,validation_data=(X_val,Y_val))\n_, accuracy_train = model.evaluate(X_train, Y_train)\nprint('Accuracy_train: %.2f' % (accuracy_train*100))","execution_count":null,"outputs":[]},{"metadata":{"id":"1j4408m6i0Ee","colab_type":"text"},"cell_type":"markdown","source":"**Plotting the confusion Matrix**"},{"metadata":{"trusted":true,"id":"E9YIa2Nvkfpi","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":436},"outputId":"efc03cd0-eb78-446d-8030-b456f6910c29"},"cell_type":"code","source":"Y_pred = model.predict_classes(X_val)\ncm = confusion_matrix(Y_val, Y_pred)\nplt.figure(figsize = (10,7))\nsns.heatmap(cm, annot=True,fmt='g')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The issue mostly is with 4 and 9 predictions"},{"metadata":{"id":"b85JXrgQj7Ho","colab_type":"text"},"cell_type":"markdown","source":"**The Cases where the model failed to correctly identify the correct value**"},{"metadata":{"id":"KS95pz75xgz-","colab_type":"code","outputId":"528a51cd-1a49-4371-d0eb-fa5762a82c6f","colab":{"base_uri":"https://localhost:8080/","height":1000},"trusted":true},"cell_type":"code","source":"count = 0\nfor i in range(Y_pred.shape[0]):\n    if Y_pred[i] != Y_val[i] and count<10:\n        img = X_val[i].reshape(28,28)\n        plt.imshow(img)\n        plt.show()\n        print(\"Predited Value:\",Y_pred[i])\n        print(\"True Value:\",Y_val[i])\n        count = count + 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"cEKCFBF9kfp0","colab_type":"code","colab":{}},"cell_type":"code","source":"test = np.array(df_test)\nY_pred = model.predict_classes(test)\nimageid = np.array([i for i in range(1,28001)])\nout = np.array((imageid,Y_pred))\nout_df = pd.DataFrame(out,index = [\"ImageId\",\"Label\"],dtype = \"int64\")\nout_df = out_df.T\nout_df\nexport_csv = out_df.to_csv(\"submission.csv\",header = True,index = False)","execution_count":null,"outputs":[]},{"metadata":{"id":"j2zCCp9VjHZo","colab_type":"text"},"cell_type":"markdown","source":"* ** Thank You for viewing this, an upvote will help a lot**"}],"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3"},"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"},"colab":{"name":"Neural Network using Keras (98% Accuracy).ipynb","provenance":[]},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}