{"cells": [{"outputs": [], "source": "# MNIST digit recognition with Random Forests\nIn this kernel I will try to predict digits from the MNIST database using a Random Forest Classifier. ", "cell_type": "markdown", "execution_count": null, "metadata": {"_uuid": "8252d41579979b59955f24f518884e535c379ac1", "_execution_state": "idle", "_cell_guid": "8997366d-32a4-4f3a-9d0b-36bd65acb25a", "collapsed": false}}, {"outputs": [], "source": "import pandas as pd\nimport numpy as np", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "6d5db80516ccbf372ca5f0fdb16711236fa07352", "_execution_state": "idle", "_cell_guid": "b43a1841-1c5c-4f27-a399-99c94f54f4ae", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "%matplotlib notebook\nimport matplotlib.pyplot as plt", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "e48b2b08b57c527d189d0eb8688d97af00039533", "_execution_state": "idle", "_cell_guid": "1ef0119b-4f60-42eb-ab1e-5e2f9cfbaca4", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "data = pd.read_csv(\"../input/train.csv\")", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "4b42653b11a893a4ec6896dcde3c43174839e7d0", "_execution_state": "idle", "_cell_guid": "e2d45e7f-d301-4a01-9624-26156d504620", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "data.head()", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "8b1e0d657cfa4772c90789627359f6d8245b4b47", "_execution_state": "idle", "_cell_guid": "16d584ec-8767-4ec5-9ed8-fd431cf5747f", "trusted": false, "collapsed": false}}, {"outputs": [], "source": "L = np.sqrt(784)\nL", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "f991b8817ee5352fcc09fde9eaaa22bf81fd52a1", "_execution_state": "idle", "_cell_guid": "b4136ddf-4dec-41e7-b734-f5ece2402088", "trusted": false, "collapsed": false}}, {"outputs": [], "source": "### Plot a few images", "cell_type": "markdown", "execution_count": null, "metadata": {"_uuid": "c9c262ec2fe7ec47bf125ef65b422ab23efee220", "_execution_state": "idle", "_cell_guid": "b96cdc68-3c8b-4352-afcc-6e3d2ce193e6", "collapsed": false}}, {"outputs": [], "source": "def plotNum(ind):\n    plt.imshow(np.reshape(np.array(data.iloc[ind,1:]), (28, 28)), cmap=\"gray\")", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "d69a2e27411a954f90484319bd8e04eb1fba7530", "_execution_state": "idle", "_cell_guid": "bfb55d3b-35bd-461a-8037-7c028d42fa91", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "plt.figure()\nfor ii in range(1,17):\n    plt.subplot(4,4,ii)\n    plotNum(ii)", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "9b3f293fd3145e034c06ab9f64b8441d1c29af7f", "_execution_state": "idle", "_cell_guid": "9d077e92-a635-49cd-bf7b-db89b61afc5a", "trusted": false, "collapsed": false}}, {"outputs": [], "source": "### Process data", "cell_type": "markdown", "execution_count": null, "metadata": {"_uuid": "1d037442c67b1c5bc947c4317cee27ac587d9688", "_execution_state": "idle", "_cell_guid": "dd2d2745-bb09-4d93-a1f7-60b36776edc1", "collapsed": false}}, {"outputs": [], "source": "X = data.iloc[:, 1:]\ny = data['label']", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "6180fbeedfbf96c7b12e90d13460b52c1a5bbcbb", "_execution_state": "idle", "_cell_guid": "85a0c142-c991-45ea-a317-0b50a6c9f888", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "from sklearn.model_selection import train_test_split", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "a4e983f36b21a47f6060f44dd2c67c0957e14377", "_execution_state": "idle", "_cell_guid": "972a0c7f-1260-48a1-a170-5aa080b70771", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "7dcc4b881e55a73ff31c7be01ca0b57239f8e4b1", "_execution_state": "idle", "_cell_guid": "dec4b141-a8eb-4a6b-9d13-bc597a5b80af", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "from sklearn.ensemble import RandomForestClassifier", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "858af50be124bdb2205efc90ed10cf45979994a3", "_execution_state": "idle", "_cell_guid": "1f70fb41-3705-42f3-8c63-2d7df2af2262", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "rfc = RandomForestClassifier(n_jobs=-1, n_estimators=10)", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "aac2713c45f72c7b8051dd62d0bf1cd1e98b9d71", "_execution_state": "idle", "_cell_guid": "4f91bf3e-d3f8-4241-a6d2-b6322a5b2e9b", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "rfc.fit(X_train, y_train)", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "d2d1eed68220129de063bceaac2a83f021015bf1", "_execution_state": "idle", "_cell_guid": "9e84c51c-0eee-4e93-a384-cb2d0824b8d8", "trusted": false, "collapsed": false}}, {"outputs": [], "source": "rfc.score(X_test, y_test)", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "a623f005944028dea243a09a0f1b354d601fe5c4", "_execution_state": "idle", "_cell_guid": "9af051f0-ab91-481d-b821-65ec70108d4c", "trusted": false, "collapsed": false}}, {"outputs": [], "source": "Try to increase the number of n_estimators!", "cell_type": "markdown", "execution_count": null, "metadata": {"_uuid": "8d17e4f4214c62c2461e208b8a1ac2764aa06e51", "_execution_state": "idle", "_cell_guid": "c2abea2b-a024-4823-a97f-a4cc419a4ba3", "collapsed": false}}, {"outputs": [], "source": "## Create Test output", "cell_type": "markdown", "execution_count": null, "metadata": {"_uuid": "54b22334120a9405f3a42f080a7105c12115fd33", "_cell_guid": "d29f8349-d8ef-4b4e-a537-a5c4cd22a28c"}}, {"outputs": [], "source": "unknown = pd.read_csv(\"../input/test.csv\")", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "015dab11583d16a19bda4127d1d04cc4efc3ba49", "_execution_state": "idle", "_cell_guid": "4730e1d7-e964-4246-892d-ba8f1c46177b", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "unknown.head()", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "b919d7dcab49b51f0af61e1ec9dc1d6cc56c7052", "_execution_state": "idle", "_cell_guid": "6d14b5bc-bb46-4df9-8d31-2fe7ee141c45", "trusted": false, "collapsed": false}}, {"outputs": [], "source": "y_out = rfc.predict(unknown)", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "a106d85842f6b4d10c9760cd20f697128ed1d03e", "_execution_state": "idle", "_cell_guid": "331945ce-d446-4388-8ce5-0927a2c17dfa", "trusted": false, "collapsed": true}}, {"outputs": [], "source": "y_out", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "8c5f03bf80e755b35b4f8eb6adfa435e9e58618f", "_execution_state": "idle", "_cell_guid": "2f7c353d-5d44-4002-a04d-eeb2a171f720", "trusted": false, "collapsed": false}}], "nbformat": 4, "nbformat_minor": 2, "metadata": {"language_info": {"file_extension": ".py", "version": "3.6.1", "codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "mimetype": "text/x-python", "name": "python"}, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}}}