{"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":{"trusted":true},"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import accuracy_score\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import cross_val_score\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import cross_val_score","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Check for missing values","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"if data.isnull().any().sum() == 0:\n    print(\"There are no missing values\")\nelse:\n    print(\"There are\",\"data.isnull().any().sum()\",\"missing values\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We expect all pixel values to be between 0 and 255, we will check this.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"min_match = data.min().min() == 0\nmax_match = data.max().max() == 255\nprint(\"The value of the minimum pixel in the data is: \", data.min().min())\nprint(\"Does it match what we expected?\", min_match)\nprint(\"-------------\")\nprint(\"The value of the maximum pixel we expected is: \", data.max().max())\nprint(\"Does it match what we expected?\", max_match)\nprint(\"-------------\")\nprint(\"Is the value range of the pixels as we expected?: \", (min_match == True & max_match == True))\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We expect all values to be integers, including labels.\nIndeed it can be seen that all the columns are of type int.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Now we will check the distribution of the labels, we will see if there is a similar number of samples for each label**\n","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"From the graph below we can see that there are indeed a similar number of examples for each label.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x = 'label', data= data)\nplt.title(\"Distribution of labels - full dataset\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Splitting into training and test**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"X = data.iloc[:,1:]\ny = data['label']\nprint(\"There are\", X.shape[0], \"examples in the full data and\", X.shape[1], \"features\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"There are\", X_train.shape[0], \"examples in the training data set\") ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Let's check the distribution of the labels in the training**","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"It can be seen that the distribution is very similar to the distribution of labels in the original datasets.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x = y_train, data= y_train)\nplt.title(\"Distribution of labels - Train\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Normalization**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = np.array(X_train) / 255.0","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Convert labels to One Hot Encoded**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"num_digits = len(y_train.unique())\ny_train = keras.utils.to_categorical(y_train, num_digits)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Model**","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"I built the following model according to experiments that ran in loops, during the iterations I collected the accuracy from a validation set and thus I figured out what are the correct parameters for this model. The following is the model obtained from the experiments:","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model = keras.Sequential()\nmodel.add(tf.keras.layers.Dense(784, activation='relu', input_shape = (784,)))\nmodel.add(tf.keras.layers.Dense(100, activation='relu'))\nmodel.add(tf.keras.layers.Dense(784, activation='relu'))\nmodel.add(tf.keras.layers.Dense(100, activation='relu'))\nmodel.add(tf.keras.layers.Dense(10, activation='softmax'))\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nfit_model = model.fit(X_train, y_train, epochs= 60)\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Predict","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test_array = np.array(X_test) / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"nns_predict = model.predict_classes(X_test_array)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"(nns_predict == y_test).sum() / len(y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_result = pd.DataFrame(y_test)\ntest_result.reset_index(inplace = True)\ntest_result.drop('index', axis = 1,inplace = True)\ntest_result['Predict_label'] = nns_predict\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_result['Correct_prediction'] = test_result['label'] == test_result['Predict_label']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_result1 = test_result[test_result['Correct_prediction'] == False]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x = \"Predict_label\", data= test_result1)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    import itertools\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n    print(cm)\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n    fmt = '.2f' if normalize else 'd'\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, format(cm[i, j], fmt),\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    plt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cm = confusion_matrix(y_test, test_result['Predict_label'],np.sort(y_test.unique()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nplot_confusion_matrix(cm,np.sort(y_test.unique()),title='Confusion matrix')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test['Label'] = model.predict_classes(test) \ntest['ImageId'] = np.array(range(1,test.shape[0] + 1))\ntest = test[['ImageId', 'Label']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.to_csv('submission2.csv', index=False)\nprint('Your submission was successfully saved!')","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}