{"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\n#Imports\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt # visualization\n\nfrom tensorflow import keras\nimport tensorflow as tf\n#The first method I attempt to use; a simple neural network.\n\nfrom sklearn.neighbors import KNeighborsClassifier\n# The second method I attempt to use; k-neighbors classifier.\n\nfrom sklearn.ensemble import RandomForestClassifier\n# The third method I attempt to use; random forest classifier.\n\n\n\n\n\n\n\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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\ntest = pd.read_csv('../input/test.csv')\ntrain = pd.read_csv('../input/train.csv')\n\n\n\n# Any results you write to the current directory are saved as output.","execution_count":77,"outputs":[{"output_type":"stream","text":"['sample_submission.csv', 'test.csv', 'train.csv']\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntraining_size = 5000\nX_train, X_test, y_train, y_test = train_test_split(train.drop('label', axis = 1)[:training_size], train['label'][:training_size], random_state = 0)","execution_count":102,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Neural Network\n\nlayers = keras.layers\n#print(X_train.iloc[0])\n#print(X_train.values)\nnX_train = keras.utils.normalize(X_train.values, axis = 1)\nnX_test = keras.utils.normalize(X_test.values, axis = 1)\nmodel = keras.models.Sequential()\nmodel.add(keras.layers.Flatten())\nmodel.add(keras.layers.Dense(128, activation = tf.nn.relu))\nmodel.add(keras.layers.Dense(128, activation = tf.nn.relu))\nmodel.add(keras.layers.Dense(128, activation = tf.nn.relu))\nmodel.add(keras.layers.Dense(128, activation = tf.nn.relu))\nmodel.add(keras.layers.Dense(10, activation = tf.nn.softmax))\n\nmodel.compile(optimizer = 'sgd',\n    loss='sparse_categorical_crossentropy',\n    metrics = ['accuracy'])\n#keras.optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)\nmodel.fit(nX_train, y_train.values, epochs = 5)","execution_count":93,"outputs":[{"output_type":"stream","text":"Epoch 1/5\n3750/3750 [==============================] - 1s 332us/sample - loss: 2.2970 - acc: 0.1891\nEpoch 2/5\n3750/3750 [==============================] - 1s 178us/sample - loss: 2.2882 - acc: 0.2749\nEpoch 3/5\n3750/3750 [==============================] - 1s 181us/sample - loss: 2.2794 - acc: 0.2893\nEpoch 4/5\n3750/3750 [==============================] - 1s 194us/sample - loss: 2.2689 - acc: 0.2693\nEpoch 5/5\n3750/3750 [==============================] - 1s 188us/sample - loss: 2.2558 - acc: 0.3435\n","name":"stdout"},{"output_type":"execute_result","execution_count":93,"data":{"text/plain":"<tensorflow.python.keras.callbacks.History at 0x7fbbc7e8ffd0>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# K Neighbors Classifier\nfor x in range (5):\n    knn = KNeighborsClassifier(n_neighbors = (x + 1))\n    knn.fit(X_train, y_train)\n    preds_training = knn.predict(X_train)\n    preds_testing = knn.predict(X_test)\n    print(\"accuracy: \" + str(sum(preds_testing == y_test)/y_test.size))\n    # k = 5 is the most accurate at 0.9224","execution_count":3,"outputs":[{"output_type":"stream","text":"accuracy: 0.9208\naccuracy: 0.9104\naccuracy: 0.9208\naccuracy: 0.9176\naccuracy: 0.9224\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Random Forest Classifier\nfor x in range(1, 51):\n    model = RandomForestClassifier(n_estimators = x)\n    model.fit(X_train, y_train)\n    print(\"n_estimators: \" + str(x) + \" accuracy: \" + str(model.score(X_test, y_test)))\n    #\n    #41","execution_count":91,"outputs":[{"output_type":"stream","text":"n_estimators: 1 accuracy: 0.6464\nn_estimators: 2 accuracy: 0.6736\nn_estimators: 3 accuracy: 0.7632\nn_estimators: 4 accuracy: 0.7968\nn_estimators: 5 accuracy: 0.8208\nn_estimators: 6 accuracy: 0.8288\nn_estimators: 7 accuracy: 0.8552\nn_estimators: 8 accuracy: 0.864\nn_estimators: 9 accuracy: 0.868\nn_estimators: 10 accuracy: 0.8784\nn_estimators: 11 accuracy: 0.884\nn_estimators: 12 accuracy: 0.9072\nn_estimators: 13 accuracy: 0.8984\nn_estimators: 14 accuracy: 0.8864\nn_estimators: 15 accuracy: 0.8952\nn_estimators: 16 accuracy: 0.9048\nn_estimators: 17 accuracy: 0.8976\nn_estimators: 18 accuracy: 0.9112\nn_estimators: 19 accuracy: 0.9024\nn_estimators: 20 accuracy: 0.8992\nn_estimators: 21 accuracy: 0.9008\nn_estimators: 22 accuracy: 0.9048\nn_estimators: 23 accuracy: 0.9088\nn_estimators: 24 accuracy: 0.9024\nn_estimators: 25 accuracy: 0.9072\nn_estimators: 26 accuracy: 0.9112\nn_estimators: 27 accuracy: 0.9104\nn_estimators: 28 accuracy: 0.9168\nn_estimators: 29 accuracy: 0.9088\nn_estimators: 30 accuracy: 0.912\nn_estimators: 31 accuracy: 0.9216\nn_estimators: 32 accuracy: 0.9176\nn_estimators: 33 accuracy: 0.9144\nn_estimators: 34 accuracy: 0.9152\nn_estimators: 35 accuracy: 0.9136\nn_estimators: 36 accuracy: 0.9136\nn_estimators: 37 accuracy: 0.9152\nn_estimators: 38 accuracy: 0.912\nn_estimators: 39 accuracy: 0.92\nn_estimators: 40 accuracy: 0.9152\nn_estimators: 41 accuracy: 0.9224\nn_estimators: 42 accuracy: 0.9216\nn_estimators: 43 accuracy: 0.9216\nn_estimators: 44 accuracy: 0.916\nn_estimators: 45 accuracy: 0.9176\nn_estimators: 46 accuracy: 0.9192\nn_estimators: 47 accuracy: 0.9208\nn_estimators: 48 accuracy: 0.9176\nn_estimators: 49 accuracy: 0.9112\nn_estimators: 50 accuracy: 0.9144\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Optimal Random Forest Classifier\n#Fits this classifier to all of the data\nmodel = RandomForestClassifier(n_estimators = 41)\nmodel.fit(train.drop('label', axis = 1), train['label'])\n","execution_count":107,"outputs":[{"output_type":"execute_result","execution_count":107,"data":{"text/plain":"RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n            max_depth=None, max_features='auto', max_leaf_nodes=None,\n            min_impurity_decrease=0.0, min_impurity_split=None,\n            min_samples_leaf=1, min_samples_split=2,\n            min_weight_fraction_leaf=0.0, n_estimators=41, n_jobs=None,\n            oob_score=False, random_state=None, verbose=0,\n            warm_start=False)"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"#print(pd.read_csv('../input/sample_submission.csv'))","execution_count":119,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"type(pd.read_csv('../input/sample_submission.csv'))","execution_count":97,"outputs":[{"output_type":"execute_result","execution_count":97,"data":{"text/plain":"pandas.core.frame.DataFrame"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction = model.predict(test)\nd = {'ImageId' : range(1, 28001), 'Label': [0 for x in range(0, 28000)]}\nfor i in range(0, 28000):\n    d['Label'][i] = prediction[i]# {'ImageId': i, 'Label': prediction[i]}\ndf = pd.DataFrame(data=d)\nprint(df)","execution_count":146,"outputs":[{"output_type":"stream","text":"       ImageId  Label\n0            1      2\n1            2      0\n2            3      9\n3            4      9\n4            5      3\n5            6      7\n6            7      0\n7            8      3\n8            9      0\n9           10      3\n10          11      5\n11          12      7\n12          13      4\n13          14      0\n14          15      4\n15          16      3\n16          17      3\n17          18      1\n18          19      9\n19          20      0\n20          21      9\n21          22      1\n22          23      1\n23          24      5\n24          25      7\n25          26      4\n26          27      2\n27          28      7\n28          29      9\n29          30      7\n...        ...    ...\n27970    27971      5\n27971    27972      0\n27972    27973      4\n27973    27974      8\n27974    27975      0\n27975    27976      3\n27976    27977      6\n27977    27978      0\n27978    27979      1\n27979    27980      9\n27980    27981      3\n27981    27982      1\n27982    27983      1\n27983    27984      0\n27984    27985      4\n27985    27986      5\n27986    27987      2\n27987    27988      2\n27988    27989      9\n27989    27990      6\n27990    27991      7\n27991    27992      6\n27992    27993      1\n27993    27994      9\n27994    27995      7\n27995    27996      9\n27996    27997      7\n27997    27998      3\n27998    27999      9\n27999    28000      2\n\n[28000 rows x 2 columns]\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","execution_count":155,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#print(pd.read_csv('../input/sample_submission.csv'))\n#df\n#pd.read_csv('../input/sample_submission.csv')\n#print(test)","execution_count":154,"outputs":[{"output_type":"execute_result","execution_count":154,"data":{"text/plain":"       ImageId  Label\n0            1      0\n1            2      0\n2            3      0\n3            4      0\n4            5      0\n5            6      0\n6            7      0\n7            8      0\n8            9      0\n9           10      0\n10          11      0\n11          12      0\n12          13      0\n13          14      0\n14          15      0\n15          16      0\n16          17      0\n17          18      0\n18          19      0\n19          20      0\n20          21      0\n21          22      0\n22          23      0\n23          24      0\n24          25      0\n25          26      0\n26          27      0\n27          28      0\n28          29      0\n29          30      0\n...        ...    ...\n27970    27971      0\n27971    27972      0\n27972    27973      0\n27973    27974      0\n27974    27975      0\n27975    27976      0\n27976    27977      0\n27977    27978      0\n27978    27979      0\n27979    27980      0\n27980    27981      0\n27981    27982      0\n27982    27983      0\n27983    27984      0\n27984    27985      0\n27985    27986      0\n27986    27987      0\n27987    27988      0\n27988    27989      0\n27989    27990      0\n27990    27991      0\n27991    27992      0\n27992    27993      0\n27993    27994      0\n27994    27995      0\n27995    27996      0\n27996    27997      0\n27997    27998      0\n27998    27999      0\n27999    28000      0\n\n[28000 rows x 2 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ImageId</th>\n      <th>Label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>6</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>7</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>8</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>9</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>10</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>11</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>12</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>12</th>\n      <td>13</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>14</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>15</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>16</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>17</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>17</th>\n      <td>18</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>18</th>\n      <td>19</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>19</th>\n      <td>20</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>20</th>\n      <td>21</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>21</th>\n      <td>22</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>22</th>\n      <td>23</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>23</th>\n      <td>24</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>24</th>\n      <td>25</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>25</th>\n      <td>26</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>26</th>\n      <td>27</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27</th>\n      <td>28</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>29</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>29</th>\n      <td>30</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>27970</th>\n      <td>27971</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27971</th>\n      <td>27972</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27972</th>\n      <td>27973</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27973</th>\n      <td>27974</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27974</th>\n      <td>27975</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27975</th>\n      <td>27976</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27976</th>\n      <td>27977</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27977</th>\n      <td>27978</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27978</th>\n      <td>27979</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27979</th>\n      <td>27980</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27980</th>\n      <td>27981</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27981</th>\n      <td>27982</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27982</th>\n      <td>27983</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27983</th>\n      <td>27984</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27984</th>\n      <td>27985</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27985</th>\n      <td>27986</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27986</th>\n      <td>27987</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27987</th>\n      <td>27988</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27988</th>\n      <td>27989</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27989</th>\n      <td>27990</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27990</th>\n      <td>27991</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27991</th>\n      <td>27992</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27992</th>\n      <td>27993</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27993</th>\n      <td>27994</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27994</th>\n      <td>27995</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27995</th>\n      <td>27996</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27996</th>\n      <td>27997</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27997</th>\n      <td>27998</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27998</th>\n      <td>27999</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27999</th>\n      <td>28000</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>28000 rows × 2 columns</p>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}