{"nbformat_minor": 1, "nbformat": 4, "cells": [{"source": ["A simple keras model, which gave around 96.8% of accuracy."], "metadata": {"_cell_guid": "26e34a5a-cc45-4794-94e0-49391035bf45", "_uuid": "a411aff2fc60876c73bacded7feda166458ddbfc"}, "cell_type": "markdown"}, {"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", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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", "\n", "from subprocess import check_output\n", "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n", "\n", "# Any results you write to the current directory are saved as output."], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "0eb6603c-9408-4c86-9377-622667d0fee7", "_uuid": "c9aa759a945dfaeb2f6e0b68263fc75630e0d422"}}, {"source": ["# importing some libraries\n", "import keras\n", "from keras.layers import Dense, Dropout\n", "from keras.models import Sequential\n", "from keras.optimizers import RMSprop\n"], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "42c04cb5-3e22-428d-8995-f8006501b859", "_uuid": "d9dd3dbed6315bd515bd7ba333417b7c2be7f300"}}, {"source": ["# load the data\n", "train_data = pd.read_csv(\"../input/train.csv\")\n", "test_data = pd.read_csv(\"../input/test.csv\")"], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "80b6001a-deb4-46e0-a0da-60a36ec97348", "_uuid": "baa182dfb5280f2b69d566732de03277fcff0d69"}}, {"source": ["# define some variables\n", "num_classes = 10\n", "\n", "# get x_train and y_train\n", "x_train = train_data.iloc[:, 1:].values.astype('float32')\n", "x_train /= 256\n", "y_train = train_data.iloc[:, 0:1].values.astype('float32')\n", "\n", "# get x_test\n", "x_test = test_data.values.astype('float32') / 256\n", "\n", "# convert y_train to 1 hot encoding\n", "y_train = keras.utils.to_categorical(y_train, num_classes)\n", "\n", "# print them\n", "# print(x_train[0, :])\n", "# print(y_train[0, :])\n", "print(x_train.shape)\n", "print(y_train.shape)\n", "print(test_data.shape)\n"], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "ab85f1ff-c2ac-403c-aef8-7ef549a1d5a2", "_uuid": "b1055a0b1426713b8ff6dec898296c86781ab071"}}, {"source": ["# build the model architecture\n", "model = Sequential()\n", "model.add(Dense(512, activation='relu', input_shape=(784,)))\n", "model.add(Dropout(0.2))\n", "model.add(Dense(512, activation='relu'))\n", "model.add(Dropout(0.2))\n", "model.add(Dense(num_classes, activation='softmax'))\n", "model.summary()"], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "04bcd7b5-6c8a-48c8-a642-697120ff9d5f", "_uuid": "9966ef07489b7ea8d9a727e72422e5329b65db1b"}}, {"source": ["# train model\n", "epochs = 2\n", "batch_size = 64\n", "model.compile(loss=\"categorical_crossentropy\",\n", "             optimizer=RMSprop(),\n", "             metrics=['accuracy'])\n", "\n", "history = model.fit(x_train, y_train, \n", "                   batch_size=batch_size,\n", "                   epochs=epochs,\n", "                   verbose=1,\n", "                   validation_split=0.2)\n"], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "09e9250e-ed3f-4f30-9a58-bc80ba8b70a3", "_uuid": "022fc167dfdb5815b121a6857fd6a4978e45e681"}}, {"source": ["# do prediction\n", "predictions = model.predict(x_test, batch_size=64, verbose=1)\n", "# print(predictions)\n", "# with open(\"check.txt\", \"w\") as fp:\n", "#     fp.write(\"hello\")"], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "c491ffeb-9194-4383-9628-c02fae46729c", "_uuid": "d3b969f14fe282f0783654f3a7093bac4015991d"}}, {"source": ["# print(check_output([\"ls\", \".\"]).decode(\"utf8\"))\n", "# saving results into file\n", "print(prediction.shape)\n", "with open(\"output.csv\", \"w\") as fp:\n", "    fp.write(\"ImageId,Label\")\n", "    for i,prediction in enumerate(predictions):\n", "        pred_class = prediction.argmax()\n", "        fp.write(\"{},{}\\n\".format(i, pred_class))\n", "        "], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "7880c4f0-2d43-408c-b414-ecd1fb501f4c", "_uuid": "873e2277b6f10b7c8862e9cb33ab6cc1d319a22c"}}, {"source": [], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_cell_guid": "86cd8a84-ce78-4418-a851-cbdc083a48ec", "_uuid": "b9d87518f6eabffcc0bc0715f7d4703fba50e176"}}, {"source": [], "outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "955d0fdb-d67f-48cd-832f-364c5655f9ef", "_uuid": "e3eb0d68a2fc7172b5f075649231ecef95109bcb"}}], "metadata": {"language_info": {"pygments_lexer": "ipython3", "version": "3.6.3", "mimetype": "text/x-python", "name": "python", "codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "file_extension": ".py"}, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}}}