{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from __future__ import absolute_import, division, print_function\n\nimport os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\nimport sklearn\nfrom sklearn.model_selection import train_test_split\nprint(tf.__version__)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6e2d2497a261d8266658ba65b2b111ae9234d921"},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc39d8648a62263c12742cc047a026e2cbee11d6"},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"402d49f123e99670a2e92f045be569bbc8e07de1"},"cell_type":"code","source":"y_train = train['label']\nx_train = train.drop('label', axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13d6ada541c8ea3593ea2db99891ece7a9bc70ef"},"cell_type":"code","source":"x_train = x_train / 255.0\ntest = test / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"69749d5a40ecd4880d80b081f5b575cff3fcbcfb"},"cell_type":"code","source":"x_train = x_train.values.reshape(-1, 28, 28)\ntest = test.values.reshape(-1, 28, 28)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ad0bf92e0bb0d1ded41ab185afd28e2b04276ef"},"cell_type":"code","source":"plt.figure()\nplt.imshow(x_train[0])\nplt.colorbar()\nplt.grid(False)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2855e8442b7a96eeade194e10e78a34b949af9a6"},"cell_type":"code","source":"y_train[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"805a27f43a848aa4aba25caca11b86d9ce5e7df1"},"cell_type":"code","source":"_x_train, _x_val, _y_train, _y_val = train_test_split(x_train, y_train,\n                                                      test_size=0.2,\n                                                      random_state=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d44549909b50dc813658dd7a7b705f19b8ab4d0"},"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor i in range(25):\n    plt.subplot(5, 5, i + 1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(x_train[i], cmap=plt.cm.binary)\n    plt.xlabel(y_train.iloc[i])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb1e03e25dad6be1bbc475694d2b5aa3d4e01ee9"},"cell_type":"code","source":"model = keras.Sequential([\n    keras.layers.Flatten(input_shape=(28, 28)),\n    keras.layers.Dense(128, activation=tf.nn.relu),\n    keras.layers.Dense(10, activation=tf.nn.softmax)\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"441f973f3fab9ff048555afd0623f61bf9b98282"},"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c600cdc2c3742fdb513dbfddc70228d0482a427d"},"cell_type":"code","source":"model.fit(_x_train, _y_train, epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"079764190958784da9d3b160b4c2b04576e0a404"},"cell_type":"code","source":"test_loss, test_acc = model.evaluate(_x_val, _y_val)\nprint('Test accuracy: ', test_acc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1bda38d2c7ce39216a61de1edf38b7aece7359cb"},"cell_type":"code","source":"prediction = model.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00202252a2f88a4e5b4fb2e256971cba0c5b9008"},"cell_type":"code","source":"np.argmax(prediction[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c24c54af25b19f51c10c35e1d8478f86fbf5eba7"},"cell_type":"code","source":"plt.figure()\nplt.imshow(test[0])\nplt.colorbar()\nplt.grid(False)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba3ae02e526e1a8d49324f53a787c257e7b994e3"},"cell_type":"code","source":"_prediction = np.argmax(prediction, axis=1)\n_prediction = pd.Series(_prediction, name='Label')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e35b455d53987718790003b014b4cd5b3bb8d213"},"cell_type":"code","source":"submission = pd.concat([\n    pd.Series(range(1, 28001), name='ImageId'),\n    _prediction\n], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e20851beee6b60d680cd5292767f62d5f45c26f"},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}