{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import numpy as np \n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import tensorflow as tf\n", "\n", "from sklearn.model_selection import train_test_split\n", "from tensorflow.python import keras\n", "from tensorflow.python.keras.models import Sequential\n", "from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# Import Data\n", "train = pd.read_csv(\"train.csv\")\n", "test = pd.read_csv(\"test.csv\")\n", "\n", "num_classes = 10\n", "img_rows = img_cols = 28\n", "\n", "def data_prep(raw):\n", " out_y = keras.utils.to_categorical(raw.label, num_classes)\n", " num_images = raw.shape[0]\n", " x_as_array = raw.values[:,1:]\n", " x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)\n", " out_x = x_shaped_array / 255\n", " return out_x, out_y\n", "\n", "x_train, y_train = data_prep(train)\n", "\n", "x_test = test.values.reshape(test.shape[0], img_rows, img_cols, 1)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train on 33600 samples, validate on 8400 samples\n", "Epoch 1/2\n", "33600/33600 [==============================] - 82s 2ms/sample - loss: 0.2185 - acc: 0.9376 - val_loss: 0.0815 - val_acc: 0.9730\n", "Epoch 2/2\n", "33600/33600 [==============================] - 82s 2ms/sample - loss: 0.0600 - acc: 0.9822 - val_loss: 0.0567 - val_acc: 0.9829\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model = Sequential()\n", "model.add(Conv2D(20, kernel_size=(3, 3),\n", " activation='relu',\n", " input_shape=(img_rows, img_cols, 1)))\n", "model.add(Conv2D(20, kernel_size=(3, 3), activation='relu'))\n", "model.add(Flatten())\n", "model.add(Dense(128, activation='relu'))\n", "model.add(Dense(num_classes, activation='softmax'))\n", "\n", "model.compile(loss=keras.losses.categorical_crossentropy,\n", " optimizer='adam',\n", " metrics=['accuracy'])\n", "model.fit(x_train, y_train,\n", " batch_size=128,\n", " epochs=2,\n", " validation_split = 0.2)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "preds = pd.DataFrame(model.predict(x_test))\n", "\n", "def decode(row):\n", " dev = []\n", " for c in preds.columns:\n", " dev.append(abs(1 - row[c]))\n", " return(np.where(dev == np.amin(dev))[0][0])\n", " \n", "output = pd.DataFrame(preds.apply(decode, axis = 1), dtype = \"int\", columns = [\"Label\"])\n", "output.index.name = \"ImageId\"\n", "output.index += 1\n", "\n", "output.to_csv('submission.csv')" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Label
ImageId
12
20
39
40
53
67
70
83
90
103
115
127
134
140
154
163
173
181
199
200
219
221
231
245
257
264
272
287
294
307
......
279715
279720
279734
279748
279750
279763
279776
279780
279791
279809
279813
279821
279831
279840
279854
279865
279872
279882
279899
279906
279917
279926
279931
279949
279957
279969
279977
279983
279999
280002
\n", "

28000 rows × 1 columns

\n", "
" ], "text/plain": [ " Label\n", "ImageId \n", "1 2\n", "2 0\n", "3 9\n", "4 0\n", "5 3\n", "6 7\n", "7 0\n", "8 3\n", "9 0\n", "10 3\n", "11 5\n", "12 7\n", "13 4\n", "14 0\n", "15 4\n", "16 3\n", "17 3\n", "18 1\n", "19 9\n", "20 0\n", "21 9\n", "22 1\n", "23 1\n", "24 5\n", "25 7\n", "26 4\n", "27 2\n", "28 7\n", "29 4\n", "30 7\n", "... ...\n", "27971 5\n", "27972 0\n", "27973 4\n", "27974 8\n", "27975 0\n", "27976 3\n", "27977 6\n", "27978 0\n", "27979 1\n", "27980 9\n", "27981 3\n", "27982 1\n", "27983 1\n", "27984 0\n", "27985 4\n", "27986 5\n", "27987 2\n", "27988 2\n", "27989 9\n", "27990 6\n", "27991 7\n", "27992 6\n", "27993 1\n", "27994 9\n", "27995 7\n", "27996 9\n", "27997 7\n", "27998 3\n", "27999 9\n", "28000 2\n", "\n", "[28000 rows x 1 columns]" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "output\n", "# arr = [2, 3, 4, 1]\n", "# np.where(arr == np.amin(arr))[0][0]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }