{"nbformat": 4, "cells": [{"execution_count": null, "source": ["import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib.image as mpimg\n", "import seaborn as sns\n", "%matplotlib inline\n", "\n", "np.random.seed(2)\n", "\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import confusion_matrix\n", "import itertools\n", "\n", "from keras.utils.np_utils import to_categorical # convert to one-hot-encoding\n", "from keras.models import Sequential\n", "from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\n", "from keras.optimizers import RMSprop\n", "from keras.preprocessing.image import ImageDataGenerator\n", "from keras.callbacks import ReduceLROnPlateau\n", "from keras.utils import np_utils\n", "\n", "\n", "sns.set(style='white', context='notebook', palette='deep')"], "outputs": [], "metadata": {"_cell_guid": "a9766864-45fe-4448-86e3-5a2f6f95d3f7", "collapsed": true, "_kg_hide-input": false, "_uuid": "864187878e9a1bf55f39de23547e1b83d28b4dc0", "_kg_hide-output": false}, "cell_type": "code"}, {"execution_count": null, "source": ["# Load the data\n", "train = pd.read_csv(\"../input/train.csv\")\n", "test = pd.read_csv(\"../input/test.csv\")"], "outputs": [], "metadata": {"_cell_guid": "546fdc55-c5b7-481f-8b0c-8c9f0e17d90d", "collapsed": true, "_uuid": "fa16420251535b0d237dc2e02955525cb7ecaadb"}, "cell_type": "code"}, {"execution_count": null, "source": ["Y_train = train[\"label\"]\n", "\n", "# Drop 'label' column\n", "X_train = train.drop(labels = [\"label\"],axis = 1) \n", "\n", "# free some space\n", "del train \n", "\n", "g = sns.countplot(Y_train)\n", "\n", "Y_train.value_counts()"], "outputs": [], "metadata": {"_cell_guid": "0d8e1418-de96-443c-9c9f-b3bc6ee26290", "collapsed": true, "_uuid": "0b7a6546b0d7dbc164b07d52e3402f4c45952140"}, "cell_type": "code"}, {"execution_count": null, "source": ["# Check the data\n", "X_train.isnull().any().describe()"], "outputs": [], "metadata": {"_cell_guid": "3b2626b4-b653-4139-bf5e-3b3d3ab1d225", "collapsed": true, "_uuid": "9c368a1c353e1733526609f7e13fc4608b985569"}, "cell_type": "code"}, {"execution_count": null, "source": ["test.isnull().any().describe()"], "outputs": [], "metadata": {"_cell_guid": "82675cc4-f93e-4440-802d-cff6e2d92070", "collapsed": true, "_uuid": "323b5708cb693be6589c55f160bea218ede6df23"}, "cell_type": "code"}, {"execution_count": null, "source": ["# Normalize the data\n", "X_train = X_train / 255.0\n", "test = test / 255.0"], "outputs": [], "metadata": {"_cell_guid": "4ad87b4f-bbd2-4928-9931-21316c735bae", "collapsed": true, "_uuid": "dda1aceab48a8ba94d2340edf653825f55c2c4f1"}, "cell_type": "code"}, {"execution_count": null, "source": ["# Reshape image in 3 dimensions (height = 28px, width = 28px , canal = 1)\n", "X_train = X_train.values.reshape(-1,28,28,1)\n", "test = test.values.reshape(-1,28,28,1)"], "outputs": [], "metadata": {"_cell_guid": "5c17d40d-8b3a-45f1-b06a-d5cab4207770", "collapsed": true, "_uuid": "82239a700940fdf7d814d290c038048c88b7edec"}, "cell_type": "code"}, {"execution_count": null, "source": ["# Encode labels to one hot vectors (ex : 2 -> [0,0,1,0,0,0,0,0,0,0])\n", "Y_train = to_categorical(Y_train, num_classes = 10)"], "outputs": [], "metadata": {"_cell_guid": "f16f3c6e-42a7-44cf-b7a9-487cd1800866", "collapsed": true, "_uuid": "45df8f9a11e32f53717e3299f508cac0bf0345f3"}, "cell_type": "code"}, {"execution_count": null, "source": ["# Set the random seed\n", "random_seed = 2"], "outputs": [], "metadata": {"_cell_guid": "a881fbf0-e882-4de1-bd85-b4b64530a5da", "collapsed": true, "_uuid": "8f9c644246f6b54b2718cb1699f050c68e5ecb81"}, "cell_type": "code"}, {"execution_count": null, "source": ["# Split the train and the validation set for the fitting\n", "print(X_train.shape)\n", "print(Y_train.shape)\n", "X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed)"], "outputs": [], "metadata": {"_cell_guid": "39c8ee76-239e-409e-8729-df3d429bba8f", "collapsed": true, "_uuid": "d218e4047873d6eaa96aa4a914bdfb86b8a9cb71"}, "cell_type": "code"}, {"execution_count": null, "source": ["# Some examples\n", "g = plt.imshow(X_train[0][:,:,0])"], "outputs": [], "metadata": {"_cell_guid": "4ebd8e1a-d606-4a91-a671-edec920d59bd", "collapsed": true, "_uuid": "9e991fb162015694bd4bec8f4d8e9c3a3697724d"}, "cell_type": "code"}, {"execution_count": null, "source": ["num_classes=10"], "outputs": [], "metadata": {"_cell_guid": "76d1a50c-c140-478d-b3d5-abbd4e7c3cd6", "collapsed": true, "_uuid": "f08dd34b70856ac09562698408e91b7902164441"}, "cell_type": "code"}, {"execution_count": null, "source": ["def baseline_model():\n", "    # create model\n", "    model = Sequential()\n", "    model.add(Conv2D(64, (5, 5), input_shape=(28, 28, 1), activation='relu'))\n", "    model.add(MaxPooling2D(pool_size=(2, 2)))\n", "    model.add(Conv2D(128, (3, 3), activation='relu'))\n", "    model.add(MaxPooling2D(pool_size=(2, 2)))\n", "    model.add(Dropout(0.25))\n", "    model.add(Flatten())\n", "    model.add(Dense(256, activation='relu'))\n", "    model.add(Dropout(0.5))\n", "    model.add(Dense(num_classes, activation='softmax'))\n", "    # Compile model\n", "    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n", "    return model"], "outputs": [], "metadata": {"_cell_guid": "a2a9c089-f559-44c8-aa9d-6ec7c2174f47", "collapsed": true, "_uuid": "4225764539f36afb4f1043b1cfe2066d7879e99b"}, "cell_type": "code"}, {"execution_count": null, "source": ["# build the model\n", "model = baseline_model()\n", "# Fit the model\n", "history = model.fit(X_train, Y_train, validation_data=(X_val, Y_val), epochs=10, batch_size=200, verbose=2)\n", "# Final evaluation of the model\n", "scores = model.evaluate(X_train, Y_train, verbose=0)\n", "print(\"CNN Error: %.2f%%\" % (100-scores[1]*100))"], "outputs": [], "metadata": {"_cell_guid": "1ec81810-661b-435a-aaa9-de404421919e", "collapsed": true, "_uuid": "df000327dfb609f94e25aa71efedc525552f6a5c"}, "cell_type": "code"}, {"execution_count": null, "source": ["# predict results\n", "results = model.predict(test)\n", "\n", "# select the indix with the maximum probability\n", "results = np.argmax(results,axis = 1)\n", "\n", "results = pd.Series(results,name=\"Label\")"], "outputs": [], "metadata": {"_cell_guid": "507c964e-12d9-407a-b535-64f6c8298d27", "collapsed": true, "_uuid": "20626b053a92432ac7c80ecb177386194699d99d"}, "cell_type": "code"}, {"execution_count": null, "source": ["submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n", "\n", "submission.to_csv(\"result.csv\",index=False)\n", "\n", "print('Saved file') "], "outputs": [], "metadata": {"_cell_guid": "732d433b-4f87-4503-ab70-d6b9fcabdeed", "collapsed": true, "_uuid": "96019a0d0b63e3e5b0b863f27d829bc5bac03e22", "_kg_hide-output": true}, "cell_type": "code"}], "nbformat_minor": 1, "metadata": {"language_info": {"mimetype": "text/x-python", "pygments_lexer": "ipython3", "name": "python", "nbconvert_exporter": "python", "version": "3.6.3", "file_extension": ".py", "codemirror_mode": {"name": "ipython", "version": 3}}, "kernelspec": {"name": "python3", "language": "python", "display_name": "Python 3"}}}