{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Digit Recognizer using Random Forest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* https://www.kaggle.com/c/digit-recognizer" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.model_selection import train_test_split" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prepare Data" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((42000, 785), (28000, 784))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train = pd.read_csv('../input/digit-recognizer/train.csv')\n", "test = pd.read_csv('../input/digit-recognizer/test.csv')\n", "train.shape, test.shape" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((42000, 784), (42000, 1), (28000, 784))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "images = train.iloc[:,1:]\n", "labels = train.iloc[:,:1]\n", "images.shape, labels.shape, test.shape" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((33600, 784), (8400, 784))" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train_images, valid_images, train_labels, valid_labels = train_test_split(images, labels, train_size=0.8, test_size=0.2, random_state=0)\n", "train_images.shape, valid_images.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Build Model" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n", " max_depth=None, max_features='auto', max_leaf_nodes=None,\n", " min_impurity_decrease=0.0, min_impurity_split=None,\n", " min_samples_leaf=1, min_samples_split=2,\n", " min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,\n", " oob_score=False, random_state=0, verbose=0, warm_start=False)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clf = RandomForestClassifier(random_state=0)\n", "clf.fit(train_images.values, train_labels.values.ravel())" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.93690476190476191" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clf.score(valid_images, valid_labels)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Submit" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "predictions = clf.predict(test)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "submissions = pd.DataFrame({\n", " \"ImageId\": list(range(1, len(predictions)+1)),\n", " \"Label\": predictions})\n", "submissions.to_csv(\"output.csv\", index=False, header=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.6.2" }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "30px", "width": "254px" }, "navigate_menu": true, "number_sections": false, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }