{"nbformat_minor": 2, "cells": [{"metadata": {"_uuid": "fdcfa2df005392be1f7dac455b928d34e5d21c95", "collapsed": false, "_cell_guid": "261f1bcd-8c06-4bfc-a407-e77cc43beda0", "_execution_state": "idle"}, "outputs": [], "cell_type": "markdown", "source": "This is my first Kaggle kernel submission.", "execution_count": null}, {"metadata": {"_uuid": "40a9ac49901c2113a3e5986d818cbe149e6a489c", "collapsed": true, "_cell_guid": "da98105b-f7fe-491b-934a-03cc7d6b6820", "_execution_state": "idle", "trusted": false}, "outputs": [], "cell_type": "code", "source": "import os, shutil\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\n%matplotlib inline\nfrom keras import layers\nfrom keras import models\nfrom keras.utils import to_categorical\nfrom keras import optimizers", "execution_count": 1}, {"metadata": {"_uuid": "fe17307125ba7861fe1010a4a37d2a1e6a87555c", "collapsed": true, "_cell_guid": "53a3eb28-1edf-43a2-9d25-35ffb4d2b680", "_execution_state": "idle", "trusted": false}, "outputs": [], "cell_type": "code", "source": "# Use pandas Dataframe to read from csv file\nbase_dir = '.'\n#train_dir = os.path.join(base_dir, 'train.csv')\ntrain_df = pd.read_csv(\"../input/train.csv\")\n#test_dir = os.path.join(base_dir, 'test.csv')\ntest_df = pd.read_csv('../input/test.csv')\n\n# Generate training, tests and label ndarrays from dataframes\ntrain_images = train_df.drop(labels=['label'], axis=1).values\ntest_images = test_df.values\ntrain_labels = train_df['label'].values\n\nprint('train_images shape: ', train_images.shape)\nprint('train_labels shape: ', train_labels.shape)\nprint('test_images shape: ', test_images.shape)\n\n# Now reshape the data to a shape that the model expects.\n# Convert flattened training images (42000x784) to 42000x28x28x1 \ntrain_images = np.resize(train_images, (train_images.shape[0], 28, 28, 1))\nprint('train_images shape: ', train_images.shape)\n\n# Convert flattened test images (42000x784) to 42000x28x28x1\ntest_images = np.resize(test_images, (test_images.shape[0], 28, 28, 1))\nprint('test_images shape: ', test_images.shape)", "execution_count": 2}, {"metadata": {"_uuid": "6b68855c668f603230e128dccee5ff6374110d76", "_cell_guid": "66dc263f-43ab-4d07-8410-0a3eed602e25", "trusted": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "code", "source": "# Transform 0 to 255 interval to values between 0 and 1 \ntrain_images = train_images.astype('float32')/255\ntest_images = test_images.astype('float32')/255\n\n# Use the keras one-hot encoding on the labels\ntrain_labels = to_categorical(train_labels)\nprint ('training labels shape: ',train_labels.shape)", "execution_count": 3}, {"metadata": {"_uuid": "485f8ba96d114dd85d31e54c74f9fff117dfc98a", "collapsed": true, "_cell_guid": "ec9581b8-76e9-4571-8f10-b5dca3ea10cd", "_execution_state": "idle", "trusted": false}, "outputs": [], "cell_type": "code", "source": "# Create our model\nmodel = models.Sequential()\n\n# Add Convolution layers\nmodel.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)))\nmodel.add(layers.MaxPooling2D(2,2))\nmodel.add(layers.Conv2D(64, (3,3), activation='relu'))\nmodel.add(layers.MaxPooling2D(2,2))\n\n# Add fully connected layers\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\n\n# Ending the network with Dense layer of size 10. \n# This means for each input sample, our network will output\n# a 10-dimentional vector representing one output class of 1 to 9.\n# Th last layer uses the Softmax activation will output a \n# probability distribution over 10 different output classes. \n# The 10 scores will sum to 1.\nmodel.add(layers.Dense(10, activation='softmax'))\n\nmodel.summary()", "execution_count": 4}, {"metadata": {"_uuid": "4e9abeed4b14ead8fcd70a702e392dcd45d130c0", "collapsed": true, "_cell_guid": "878c8c9d-dbed-4854-a3e2-3a55abc330c8", "_execution_state": "idle", "trusted": false}, "outputs": [], "cell_type": "code", "source": "# Default RMSProp parameters\noptimizers = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)\nmodel.compile(optimizer=optimizers,\n             loss='categorical_crossentropy',\n             metrics=['accuracy'])", "execution_count": 5}, {"metadata": {"_uuid": "9eba908392ddfd98b31c7b2a8f7cdcab49197c3b", "collapsed": false, "_cell_guid": "5431a53f-379c-459f-b5af-46e9a3f9e16f", "_execution_state": "idle", "trusted": false}, "outputs": [], "cell_type": "code", "source": "# Trains the model for a fixed number of epochs.\nmodel.fit(train_images, train_labels, epochs=5, batch_size=64, verbose=0)", "execution_count": 6}, {"metadata": {"_uuid": "b5b54b0da4d02cc1a51627db2c304d663cfa5856", "collapsed": true, "_cell_guid": "c71c17bf-0ba1-44d4-bdcb-316bbb08cc16", "_execution_state": "idle", "trusted": false}, "outputs": [], "cell_type": "code", "source": "# Predict our test labels \ntest_labels = model.predict(test_images)", "execution_count": 7}, {"metadata": {"_uuid": "a88a2c0471481cfeef33ce47168e4677c5bd1bbd", "collapsed": true, "_cell_guid": "de1ee169-714a-41c3-9f81-7f8721c5ae8e", "_execution_state": "idle", "trusted": false}, "outputs": [], "cell_type": "code", "source": "# Create the submission array by getting the column index vector \n# to 1D with the highest probability value \nsubmission = np.argmax(test_labels, axis=1)", "execution_count": 8}, {"metadata": {"_uuid": "4910b94376a0888733f3c15826fcf881d7378a60", "collapsed": true, "_cell_guid": "1a38af5e-f1ba-4a5a-af10-cf8b5f0644d3", "_execution_state": "idle", "trusted": false}, "outputs": [], "cell_type": "code", "source": "# Create a submission csv file\ndf = pd.DataFrame(data =(np.arange(1,submission.shape[0]+1)))\ndf.columns = ['ImageId']\ndf = df.assign(Label=submission)\ndf.to_csv('submission.csv', index=False)", "execution_count": 9}, {"metadata": {"_uuid": "63d53a62579f907d30855f3c81fbd74f9c3f1a32", "collapsed": true, "_cell_guid": "882565a4-f1fa-44c1-9ded-1b4ffa59a0ef", "_execution_state": "idle", "trusted": false}, "outputs": [], "cell_type": "code", "source": "", "execution_count": null}], "metadata": {"language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "codemirror_mode": {"version": 3, "name": "ipython"}, "file_extension": ".py", "name": "python", "pygments_lexer": "ipython3", "version": "3.6.1"}, "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}}, "nbformat": 4}