2015-04-29 05:27:02 +08:00
{
" cells " : [
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-05-31 21:59:13 +08:00
" # scikit-learn-random forest "
2015-04-29 05:27:02 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-05-31 21:59:13 +08:00
" Credits: Forked from [PyCon 2015 Scikit-learn Tutorial](https://github.com/jakevdp/sklearn_pycon2015) by Jake VanderPlas "
2015-04-29 05:27:02 +08:00
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 1 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
" % matplotlib inline \n " ,
" import numpy as np \n " ,
" import matplotlib.pyplot as plt \n " ,
" import seaborn; \n " ,
" from sklearn.linear_model import LinearRegression \n " ,
" from scipy import stats \n " ,
" import pylab as pl \n " ,
" \n " ,
" seaborn.set() "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Random Forest Classifier "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-05-31 21:59:13 +08:00
" Random forests are an example of an *ensemble learner* built on decision trees. \n " ,
" For this reason we ' ll start by discussing decision trees themselves. \n " ,
2015-04-29 05:27:02 +08:00
" \n " ,
2015-05-31 21:59:13 +08:00
" Decision trees are extremely intuitive ways to classify or label objects: you simply ask a series of questions designed to zero-in on the classification: "
2015-04-29 05:27:02 +08:00
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 2 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAAAk4AAAFFCAYAAAAadmKrAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz \n AAALEgAACxIB0t1+/AAAIABJREFUeJzsnWd4VcXWgN8TQiihC6j0IkyogRCIqIQmhCIIKk1KAOFT \n ioqICHhtqCDg5SpgoapUReBerlwFpEiviiDFERDpPRAIAdL292POOaScJCch5KSs93nOk+zZs2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KEabb6OU6op5sVsCYdypRA5aYkahU7XWGxKltRGj \n +IBp7F3xpdZ6QbxrNmGUIzBKQl+t9U37uRvYlTmMudQVq7TWwxxz8VrrCKAXxmpUgTsj7+TojLEa \n /IHp0C7Gk+0QdyryIKVUmveCcQPHShAbxvqGXdkbg+n4esXrsNFaR2qthwI7MKOo/vHSehXwARZr \n rd91mKG11jFa6xGYEVdRjNk6ORwjwx+11mfi5RuBUeZW29NJFvsoboRd/ue11j/ES+eW1vpVjJKa \n H6M0JMYC+mitf4t33XbudPxBKeWfgXyktb5kz/8qgFIqEPP8LgKddLzpYK31KcxUbwTwpFKqbry0 \n xtr/DtNafxfvmhhtVlQuxgwsXol3DVrrk1rrPxNPj6WC453fqbX+O164o+EvScoKRkY+/zCgv/39 \n QWt9nTudlQ0YpLXeGy/+J5j2xYs7W0eAsULHAG8ntvBorZdgVktC8u2OOxSLJ3NGk552M6118a7r \n rp20DsBH2/9O1lrPiJdvJMbyfRl4KJ61KzPKcgjGV20f8Kyj/trz2I2x+lpAT6WUq3x+1Fq/pe3T \n xVrrKMCx91+Ru5QtS5P
" text/plain " : [
" <matplotlib.figure.Figure at 0x104e7a790> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
}
] ,
" source " : [
" import fig_code \n " ,
" fig_code.plot_example_decision_tree() "
]
} ,
2015-05-31 21:59:13 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" The binary splitting makes this extremely efficient. \n " ,
" As always, though, the trick is to *ask the right questions*. \n " ,
" This is where the algorithmic process comes in: in training a decision tree classifier, the algorithm looks at the features and decides which questions (or \" splits \" ) contain the most information. \n " ,
" \n " ,
" ### Creating a Decision Tree \n " ,
" \n " ,
" Here ' s an example of a decision tree classifier in scikit-learn. We ' ll start by defining some two-dimensional labeled data: "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 3 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhE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s2ORoyMf+fiSvm03Ezdu40D9hgbrSGrlV2IzsQVBKD0STa57aaVIExubXBbnea65 \n utrxol6n6Kg7hEydyOAzIVQCMoDNsnp4zZ1P7eYtC1XX+bXLsP34E9okaXMwxwEbOnel9z9rsLKy \n KlRdcXFxxLT2oeNj/TuttyQSbm7dRePWhr88GFV/TAwXB/dh2HW5Tvnaeg1osTWISg6OOWU7P/mA \n jkv/okZWFgApwMpOXen1z2oyMtKxtrbB0tIyZ//zu3agmP0OPe/fQ4p2WHpd0+a0XLoC9ypVgeL9 \n TiUlxHPsyzlYnz6JSVYW6Y2bUPuN/1DTp2mR6qvIXuS/vZImrpVxXF3tChzqEgG4AnnRf7GVSiUn \n Nqwl67ocqacnfmMmFDpggvY6hR4L5dqalZilpWHeoiVtBg8r0rBrTEwMSa19aZuaorftHnBl03Z8 \n 2xV9RvGuL+Yw+vef9YaaVMCaWe/R470PAbgachLXIQOon6E7IU0BrHtnNj3e/cBg/Y9iYjj791+Y \n xsdDHRntxk7Qufdb1N8phULBjiH9ePXkcZ3h/iAvb9xWrqe6rF6h66zIXvS/vZIkrpVxjAnAYpzq \n OZaSkszJf5ZCYiKOrfxo5h9g9P3F8mBqakqHkWNKpC6vujK8Pv2i2PW4ublx3seXtseP6m07Xr8h \n 7Vu3LVb9ljcjDN7nMQHMcw1D39u2hY4Z+rPBzQDzkPyXNXRxcyPgvY+K1UZDTqxdxfA8wReg1+1b \n rFz0B9V/FDOtBaG4RAB+ToXt3knKR+8z9HYkZsAdU1MCu3Sj9+LlRepVvsw8336Hg5E36fLgfk7Z \n GQdHrKa/Uex7qYpK+Sf
" text/plain " : [
" <matplotlib.figure.Figure at 0x10cbc3c10> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
}
] ,
" source " : [
" from sklearn.datasets import make_blobs \n " ,
" \n " ,
" X, y = make_blobs(n_samples=300, centers=4, \n " ,
" random_state=0, cluster_std=1.0) \n " ,
" plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap= ' rainbow ' ); "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 4 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAAAd0AAAFRCAYAAAAxT3fNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz \n AAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd4VVXWx/FPOoTeewelg4goIAqIXdHRUbG3UUd9nbEz \n NhDUsYzd0bGPjo6OOjqWcewVwV5Q0FClSu+dhOT94yRyc3MTEkhyL+F8nyePnn1/Z691bmGfs/be \n ayXl5eUJCQkJCQkJqXiS4+1ASEhISEjIrkI46IaEhISEhFQS4aAbEhISEhJSSYSDbkhISEhISCUR \n DrohISEhISGVRDjohoSEhISEVBKpFW4hKWnPqJbOyCrhONTs5JrJ9Mli9LG0iBTNZNOTXDeG9+Pt \n Y6gJNaGmQjXxth9fTV7e14ohfNINKXde56CjowZcaE9GfQ6Oh08hISEhiUA46IaUO5nUTinmtdrU \n q1RnQkJCQhKIcNANKXc28Mv6Yl5bzPxKdSYkJCQkgQgH3ZBy5yw+fJwfo9vHsSSDF+LhU0hISEgi \n UPELqUJ2ORqQ05grbuHizvSqTtoc5i3noZExBuOQkJCQXYXKGHQ7Rx0P2MZxqKkCmuOZgCcXk7aB \n 5IPZEysV/j4k/HWEmlATarZLE2/78dYUu3q5Mgbd6OXWsdpCTRXVNN56vCke9kNNqAk1cdPE2368 \n NTEJ53RDQkJCQkIqiTC8HGripYm3/VATakJN+PuuKE0YXg41CamJt/1QE2pCTfj7rihNTMLwckhI \n SEhISCURhpdDTbw08bYfakJNqAl/3xWlCcPLoSYhNfG2H2pCTagJf98VpYlJGF4OCQkJCQmpJMLw \n cqiJlybe9gf8j+ZZDM6kxmrmnsu6unH0J9SEmiqkibf9eGvC8HKoSUhN3Ow/wP49uPpS6sJGPM7M \n FvznKJYmos+hJtTsZJp424+3JiZheDlkl+Nrqjfl6EH5Ay5Uw/m0n8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RkjZWdISEjJVOktQ4vZ/3GOaUzrbLJrsn4Ef08p+bx4LzXfVTSlOq8G \n +xzHQ3cG88a7Z1B7IZN357WDgi1KdSrQxwFtWTmbnDYxfitrWFlcP+nUzhNVMimfvGDvbaJ/PgO+ \n pH4P2sbQGkizjziqPRsry59Qs1Np4m0/3ppdb8vQe9TOYsgVdCwYZBeTey0rbuaWyvYn1Gz/eR3J \n ujwoeEDx85oV4uPpZN3CftcwIDIs9B2rl/EUFsfqpxUr3mf/6EQiGzAtKCc4oaJ8Li9NbeYsYjUa \n RgvnsSGdjyvTn1Cz02nibT/emphU2Tndjzn7ejpGPmk0Jvk0jriV10fyQ7x8qwzepPnXHN2Y5qtZ \n WY8fzorDIpzK5HFafM+ATiSfa8cKMBSQhoP50w1csjt71aXOz0xdxgvXBoNOzFXhJ7JoFI/g3KHU \n S8Icsh/mo8t50k4QYu7H+hv48lAOjX5if4dvr2BBXBwLCdmJqbLh5Q7sGSu014Vqb3KsrftWEy0s \n scOaf9G5AX+8JiI/chYH3kOzP/J2gvhcbn1vYuCdnDOYvmdSYyG5j3J0ax4/nLk7aqtf8PfiOv6z \n mX0PCQZbtpF9ayw/jOP6xzhlHYszmTSGH1KCATdhvi8laY7j5TtocQxd2pO2gNx/M21vnk9Un0NN \n QmjibT/eml0vvJzNuuIcWsWSqHMTLSyxQ5olXDAiqohNZ9J+YOgkHu5eeB5upw8/3cNxF7N/9fzj \n ZiSfT6e/ctJ6zsoMFjvvsK0awV9uWfoZFPwtK6utRNF0RgfOupm+6ey/hi+v5+P8KMLSRPQ51CSM \n Jt72462JSZVdvTyTiVtitH/NuvqFn/aqFBOp1r6YkOdhtHws2OZSpWhO7+ox2o+n21j2r3SHqhj5 \n RSi++hOv37R1wA0JCdk
" text/plain " : [
" <matplotlib.figure.Figure at 0x10cd3ccd0> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
}
] ,
" source " : [
" # We have some convenience functions in the repository that help \n " ,
" from fig_code import visualize_tree, plot_tree_interactive \n " ,
" \n " ,
" # Now using IPython ' s ``interact`` (available in IPython 2.0+, and requires a live kernel) we can view the decision tree splits: \n " ,
" plot_tree_interactive(X, y); "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" Notice that at each increase in depth, every node is split in two **except** those nodes which contain only a single class. \n " ,
" The result is a very fast **non-parametric** classification, and can be extremely useful in practice. \n " ,
" \n " ,
" **Question: Do you see any problems with this?** "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ### Decision Trees and over-fitting \n " ,
" \n " ,
" One issue with decision trees is that it is very easy to create trees which **over-fit** the data. That is, they are flexible enough that they can learn the structure of the noise in the data rather than the signal! For example, take a look at two trees built on two subsets of this dataset: "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 5 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" data " : {
" text/plain " : [
" <matplotlib.figure.Figure at 0x10dcd7550> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
} ,
{
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAAAd0AAAFRCAYAAAAxT3fNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz \n AAALEgAACxIB0t1+/AAAIABJREFUeJzsnWd4FFUbQM9ms+m9VxJCSagJJLTQe7GgiIiFotgVRURE \n 6RBERFEBUQT87F3sIlho0nsPnUCAdNJ7st+PSWCzmYSE7O5sknueJz7unTPzvrPs5mbeuXOvSqvV \n IhAIBAKBwPhYKJ2AQCAQCASNBdHpCgQCgUBgIkSnKxAIBAKBiRCdrkAgEAgEJkJ0ugKBQCAQmAjR \n 6QoEAoFAYCIsjR5BpYrUawkDYqt5LZx67lyC8H/g1fHQTFc6CJnrYNorsFvpHIUjHOEY1VE6vrKO \n VruPKhBXugKD8zUMeECvwwWIACcruFOJnAQCgcAcEJ2uwODYgrtVFdvcwMukyQgEAoEZITpdgcHJ \n g9TCKrZdgySTJiMQCARmhOh0BQZnFPz9FZzXbz8Emfn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ZgyehG1zIdkHhgdBk3QoOAFb74d3arK/J/Qr73B16QvO \n s2EEsNzgSStELqjehmdeha7OZW15MGApHHoQngjQuR0jEAgMT4MtLzd2JwQ6LpSmGtyp4+jPE9xg \n yk+zYXsJLL4C1t4QZQXbgKY1OY6LzOQPIM2v7CvdE24wtyM+hG5PQBdnnY22wHMQvhSefflGZcBs \n chZOvXSUjq+00zjLy/XJOQNW34LfbXA43AzyMZEju9+70L4YRvtAUBFoT0LpTPhEZ0k72eOouT5q \n qqA2OV6EE0A7fTkHOC99ea7v9zMEHoO73cHrGqSGwP5R5ve+VumUwigP6e+JCtgC9uCnt69Z5Cyc \n eusoHV9pR5ZGW15+A9q
" text/plain " : [
" <matplotlib.figure.Figure at 0x10dcd7510> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
} ,
{
" data " : {
" text/plain " : [
" <matplotlib.figure.Figure at 0x10dcd7990> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
} ,
{
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAAAdgAAAFRCAYAAADXZryJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz \n AAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd4VEXbh+/dTe+NJKSHVCD0moQWQEBAUBEbWFHBjoqg \n NKkWREV9ra9iAdv3gtiRamgB6Z3QCTWd9J7s98cmsNmckM1my0mY+7q4dGd/M/M752x29jxn5hmF \n Wq1GIBAIBAKBcVFa2oBAIBAIBC0RMcAKBAKBQGACxAArEAgEAoEJEAOsQCAQCAQmQAywAoFAIBCY \n ADHACgQCgUBgAqyM2ppC0U2nJBpIbqBMaIRGsmw2TH0V7rbXEhQDC+GnBfC2HDwKjdAIjcEaKeTm \n Ub/jUKv3IIG4gxXIkiRw7An97XXK7YEe0H8nOFjCl0AgEOiLGGAFsmQtBLQHH6n3YsD3b/AztyeB \n QCBoDGKAFciSPnD5OGRKvXcCMvpBqrk9CQQCQWMQA6ygURwCx1cg7gsIMGU/gyF/OySV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2nistUMh+VLwC4eOoSAzV7I3g+7psBl9FvW1VBf \n UmWSj1rLAC9wru7XZOejLaRuh9xYcK0pUwPfwPHZkFJP/zfsywGYCYeXwtbfoEsVVAbD9mmaO3Xx \n 2RSa5qiRQm4em9cynVy4AkRKVc6DTC29Wadcp8I5IEL3jXwgQ3PyzObnEyhLheFWoAyElIflNyVd \n L00uKCvA9hmYuBn6vAflQ+HIK1CA/kuUpLhhvRTYVwWDdMMzv8DFEbCU62Frk5yPgbByESTug/sC \n IbIUKpPh316wxB5ymtLXo5rXP2sVSZ1H2X0WhEZoblCmj6Y5Hof5JzkpYVcKxAXr3GVcgYp02Gxu \n PzWUwS/7oGcXrbsOgK/
" text/plain " : [
" <matplotlib.figure.Figure at 0x10e44ae50> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
}
] ,
" source " : [
" from sklearn.tree import DecisionTreeClassifier \n " ,
" clf = DecisionTreeClassifier() \n " ,
" \n " ,
" plt.figure() \n " ,
" visualize_tree(clf, X[:200], y[:200], boundaries=False) \n " ,
" plt.figure() \n " ,
" visualize_tree(clf, X[-200:], y[-200:], boundaries=False) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" The details of the classifications are completely different! That is an indication of **over-fitting**: when you predict the value for a new point, the result is more reflective of the noise in the model rather than the signal. "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Ensembles of Estimators: Random Forests \n " ,
" \n " ,
" One possible way to address over-fitting is to use an **Ensemble Method**: this is a meta-estimator which essentially averages the results of many individual estimators which over-fit the data. Somewhat surprisingly, the resulting estimates are much more robust and accurate than the individual estimates which make them up! \n " ,
" \n " ,
" One of the most common ensemble methods is the **Random Forest**, in which the ensemble is made up of many decision trees which are in some way perturbed. \n " ,
" \n " ,
" There are volumes of theory and precedent about how to randomize these trees, but as an example, let ' s imagine an ensemble of estimators fit on subsets of the data. We can get an idea of what these might look like as follows: "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 6 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAAAd0AAAFRCAYAAAAxT3fNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz \n AAALEgAACxIB0t1+/AAAIABJREFUeJzsnXdYFNf3h99l6VV6RxBUxN4V1NiixlhiiYkxmmZ6filq \n NIm9pmq6Sb4aNTFdjRpNMRprxBJ7RcGCSheU3uH3x7C4LANSlp0F7vs8PLozn7nnzOzs3Jkz956j \n Ki4uRiAQCAQCQd1jorQDAoFAIBA0FkSnKxAIBAKBgRCdrkAgEAgEBkJ0ugKBQCAQGAjR6QoEAoFA \n YCBEpysQCAQCgYEwrXMLKlVnnSXBQEQln4WmAWri3xocnPxUzzLL3Bf/Feyy6mCl221jafBBpkQA \n HGFqy/dZ9m0wqHXssw8ykmDUaLhVl/shNEIjNFXSKG1fWU1x8VEqQDzpCuoNHXnnwk6sL8mtOwon \n SzpcgUAgMFpEpyuoN6gxI50O636BG4Uly/KBtXDDEj5T0jeBQCCoCnUfXhYI9Egfup9PI3zSHHjI \n CZyTIO4ZONkMIpX2TSAQCO6G6HQF9Y7BkDYYVmgtClbMGYFAIKgGIrwsEAgEAoGBMMSTru5TSOhd \n PgtNA9QUU1x+u+KatC3TjpHtq9AIjdAobl9pTYWjlw3R6eoOt5ZbJjQNXKNCVV6jqknbMu3oyUeh \n ERqh0atGaftKa2QR4WWBQCAQCAyECC8LjUE0IrwsNELTqDRK21daI8LLQqOsRoSXhUZoGp1GaftK \n a2QR4WWBQCAQCAyECC8LjUE0IrwsNELTqDRK21daI8LLQqOsRoSXhUZoGp1GaftKa2QR4WWBQCAQ \n CAyECC8LjUE0IrwsNELTqDRK21daI8LLQqOsRoSXhUZoGp1GaftKa2QR4WWBQCAQCAyECC8LjUE0 \n IrwsNELTqDRK21daI8LLQqOsRoSX765JhkhnKDQWf4RGaGqpUdq+0hpZRD1dgUBB8oH58GQLGOoA \n tsmQeAW2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hgj3YZkBaBmybCyl15btA0JgR4WWhMYjGmOrpVqbJhrR8 \n tLI/lFAEpEGaPm3VRrMBJs2C/pofsBOonodWq+DlQohWV7GdreCnhpengYdmXu1FGLgGjj4uzb/V \n m8/GqsmDsOXSSGy/PLj9NGTL1H40Kp/rgUZp+0prRHhZaJTVGFM93co0AyH6K2j7IoRoJ3dYDZEd \n 4ROkkoCKHtd8wBtayP14h4LrTHB5B/6tiq0L8PhU8NBe0QIsoqDlPrjRGzL04bOxav4Cx/PQbyK0 \n dAFVLrAeYhPh8Gtwwhh9rkcape0rrZFFVBkSCLToDNm+MGUxbPoRrn8PV96GLVYwdRCkKu0fSHVt \n 7SoIdbuDSbZOJ1oR+YCP9J66HIPA+VcYUgs36wVH4bVXIdil5G7OApgAXlbwWpbmtlAg0CMivCw0 \n BtHUl/AywEjpbz0QizSaG8C+5E/x4+oB3IQkwEV3gwOQMwbiKfu7k22n5I5b9hpQCLhL7d+1nar4 \n bIyaW6BuBd3ketaR0OobGPs8nDaUPw1Mo7R9pTUivCw0ymrqS3i5vmji4dtT8FY7rSlMGcBeCH+j \n fGhZth01cAWOAEN1xVshYRisoTQ5WO19NjbNZbCyruAa6AQmFyBTZ1vFfa5nGqXtK62RpUGMXj4D \n lr/DQy7gqwbTa3C+A6waobRjAkEdMRO2L4aiAzDBAZwyIfUS/DtbvsOtEH9Y/iU0exKCNdmj9sPt \n BPhtTPkOt0HRGbKXQxRQLhX0doh/APYq4JaggVPvw8uZYPIHTJ8GbTQjNouhzSroeg12+unRltDU \n XFOfwsv1RTMTYpA62XAdTbl8yBV9fhiIhve/hclFkJ8JWc1h5wvgTyN4NWQF+45Amy5ala5ioSgC \n DkyTcnwYnc/1RKO0faU
" text/plain " : [
" <matplotlib.figure.Figure at 0x10dce33d0> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
}
] ,
" source " : [
" def fit_randomized_tree(random_state=0): \n " ,
" X, y = make_blobs(n_samples=300, centers=4, \n " ,
" random_state=0, cluster_std=2.0) \n " ,
" clf = DecisionTreeClassifier(max_depth=15) \n " ,
" \n " ,
" rng = np.random.RandomState(random_state) \n " ,
" i = np.arange(len(y)) \n " ,
" rng.shuffle(i) \n " ,
" visualize_tree(clf, X[i[:250]], y[i[:250]], boundaries=False, \n " ,
" xlim=(X[:, 0].min(), X[:, 0].max()), \n " ,
" ylim=(X[:, 1].min(), X[:, 1].max())) \n " ,
" \n " ,
" from IPython.html.widgets import interact \n " ,
" interact(fit_randomized_tree, random_state=[0, 100]); "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" See how the details of the model change as a function of the sample, while the larger characteristics remain the same! \n " ,
" The random forest classifier will do something similar to this, but use a combined version of all these trees to arrive at a final answer: "
]
} ,
2015-04-29 05:27:02 +08:00
{
" cell_type " : " code " ,
" execution_count " : 7 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAAAd0AAAFRCAYAAAAxT3fNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz \n AAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd8VFX6x/F3KiH03jsoHURECaKg2BXddVVsa+8/d+2s \n jaauvZdVV13dtbu69t4RxC4qGgERBKT33pLfHzeByWQSkpBkhnA/r1deOme+5zzPnbnDufe55zxP \n Um5urpCQkJCQkJCKJzneDoSEhISEhOwohJNuSEhISEhIJRFOuiEhISEhIZVEOOmGhISEhIRUEuGk \n GxISEhISUkmEk25ISEhISEglkVrhFpKSdo1q6YzsYl6Hmu1cM4k+2Yw8khaRommse4yrR/N+vH0M \n NaEm1FSoJt7246vJzf1KEYR3uiHlzmvsf0TUhAvtqVafA+LhU0hISEgiEE66IeVOJrVTinivNvUq \n 1ZmQkJCQBCKcdEPKnTX8vrqI9+Yzu1KdCQkJCUkgwkk3pNw5lQ8f4cfo9rEsqMZz8fApJCQkJBGo \n +IVUITscDdjYmEtv4ILO9KpO2m/MWswDw2NMxiEhISE7C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L8VpjuLFW2nxR7q0J20OOf9lyu48m6g+h5qE \n 0MTbfrw1O154eQOrinJoGQui+iZaWGKbNAs4d1hUEZvOpH3PPj/wYPeCz+G2+/DTnRx1AXtXz3vd \n jORz6HQPx63m1MxgsfM226oR/OWUZpyBwd+i0tpKFE1ndODU6+mbzt4r+GIUH+dFERYmos+hJmE0 \n 8bYfb01Mquzq5WlM3BSj/StW1S94t1elmEhG+yJCngfT8uFgm0uVojm9q8doP5puY9i70h2qYuQV \n ofjyb7x23ZYJNyQkpAx
" text/plain " : [
" <matplotlib.figure.Figure at 0x100431790> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
}
] ,
" source " : [
" from sklearn.ensemble import RandomForestClassifier \n " ,
" clf = RandomForestClassifier(n_estimators=100, random_state=0, n_jobs=-1) \n " ,
" visualize_tree(clf, X, y, boundaries=False); "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-05-31 21:59:13 +08:00
" By averaging over 100 randomly perturbed models, we end up with an overall model which is a much better fit to our data! \n " ,
" \n " ,
" *(Note: above we randomized the model through sub-sampling... Random Forests use more sophisticated means of randomization, which you can read about in, e.g. the [scikit-learn documentation](http://scikit-learn.org/stable/modules/ensemble.html#forest)*) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" Not good for random forest: \n " ,
" lots of 0, few 1 \n " ,
" structured data like images, neural network might be better \n " ,
" small data, might overfit \n " ,
" high dimensional data, linear model might work better "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Random Forest Regressor "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" Above we were considering random forests within the context of classification. \n " ,
" Random forests can also be made to work in the case of regression (that is, continuous rather than categorical variables). The estimator to use for this is ``sklearn.ensemble.RandomForestRegressor``. \n " ,
" \n " ,
" Let ' s quickly demonstrate how this can be used: "
2015-04-29 05:27:02 +08:00
]
} ,
{
" cell_type " : " code " ,
2015-05-31 21:59:13 +08:00
" execution_count " : 8 ,
2015-04-29 05:27:02 +08:00
" metadata " : {
2015-05-31 21:59:13 +08:00
" collapsed " : false
2015-04-29 05:27:02 +08:00
} ,
2015-05-31 21:59:13 +08:00
" outputs " : [
{
" data " : {
" image/png " : " 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tloi6kaoXuIPjJ6VkZ18farzkt \n pKa2zm3n4hAun87ZEAempeXTcQGkDMLYGHObMebZLAoj9W9O872p7diZC1pYWnFdjMIIsQbWzfdz \n tqw4LvDNSGFsjHlA0pckxT+CJ6V+zWllbtoOVeihzDnrJ44LfDIx4s+/LOlDkn43g7JsiJrToteD \n /l9ZRIO8Qg6wkHDO+onjAp+MVDO21j4t6WpGZQEAoJRGrRkPbHo6/s6zVqvE/n/S+1v9n0u1WkWq \n VFSrVQYqW7Qd3bb62aTt921/pHXPw89Ikh4/foezMnTu47O/+oGN99PuY1/OWV/KsZVRypd0/iy+ \n tapmS7q0uKJHn3pRD330QGH2Rx7KuM0+G1sYz8/HT6ZvNFqx/5/0/lb/51Kj0ZJaLTUarYHKFm1H \n t61+Nm77p6fr3u2PtLI+vmma9R+57z09ZRhlH/tyzvpSjn5GPZfjtvHkuYtau3rtkaXPvzSvj/za \n N9RstjRRq3q9P/IQ0vXCV8Pe7GQ1pyY+VUpoqj45dF9u6AOYANeSFvlYemvVQWmAXiPXjK21r0gi \n SQAASInVJgAEL2mRj7Kufgf/jK3POFTRQh+jLusYPdknej03O5NF8VLpXh+bJvRwlHVRmrnZGR09 \n fV4LS1ckXVvkg7Xg4QvnYVzmC3207UlP9jlyaD/zH0fAhRadg/eOHNqvh554ThKLfMA/NFN7gCfI \n 9FpYWiFMc1Dmh3OwNjd8RhgDQGCOnbnAzWzBOG+mTlK05uvF5VWtNdrzGIft8735xqlNzdQST5AB \n gDLxNoz78S2omy2p2bi2oMCwfb5Jg0vi+DTQC4Pz7ZxFG8cFvqCZOifD9vkO8gSZpIFer77OSjro \n RVMlUByEsScGGVwyroFeC0srzqbARDX/Zqvd9A8MIzp/Li2u6OS5i66L4w1uzPxXyGbqIqDPd3jd \n Nf+1RnPkKV7dffnf/8F
" text/plain " : [
" <matplotlib.figure.Figure at 0x110b05d90> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
}
] ,
" source " : [
" from sklearn.ensemble import RandomForestRegressor \n " ,
" \n " ,
" x = 10 * np.random.rand(100) \n " ,
" \n " ,
" def model(x, sigma=0.3): \n " ,
" fast_oscillation = np.sin(5 * x) \n " ,
" slow_oscillation = np.sin(0.5 * x) \n " ,
" noise = sigma * np.random.randn(len(x)) \n " ,
" \n " ,
" return slow_oscillation + fast_oscillation + noise \n " ,
" \n " ,
" y = model(x) \n " ,
" plt.errorbar(x, y, 0.3, fmt= ' o ' ); "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 9 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" data " : {
" image/png " : " iVBORw0KGgoAAAANSUhEUgAAAeMAAAFVCAYAAADc5IdQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz \n AAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd8ZHW9999nek0ySaYk2exma3aXLazSm2ADeRQLFhSs \n V0RQsQB6H9v1Xr3qlbVcRUSvIBZ0feDiVa9KE5CydJZl2RK2ZTd1SmYyvZ/z/HFm0jbJJpNpWX7v \n 18uX7Mwpv5mcOZ/z7ZKiKAgEAoFAIKgdmlovQCAQCASCVzpCjAUCgUAgqDFCjAUCgUAgqDFCjAUC \n gUAgqDFCjAUCgUAgqDFCjAUCgUAgqDG6Unbq7u7WAv8FrAEU4OM9PT27y7kwgUAgEAheKZRqGb8Z \n kHt6es4Bvgz8e/mWJBAIBALBK4uSxLinp+ePwFWFf3YBoXItSCAQCASCVxoluakBenp68t3d3bcD \n bwfeWbYVCQQCgUDwCkNaaDvM7u5uN/AUsK6npyc53TaKoiiSJC3oPAKBQCAQLCLmJXqlJnC9H1jS \n 09PzLSAJyIX/Tb8iScLvj5ZyKsEccTrt4juuMOI7rg7ie6484juuPE6nfV7bl+qmvgu4vbu7+x+A \n Hvh0T09PusRjCQQCgUDwiqYkMS64o99T5rUIBAKBQPCKRDT9EAgEAoGgxggxFggEAoGgxggxFggE \n AoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGg \n xggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggx \n FggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggE \n AoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGgxggxFggEAoGg \n xggxFggEAoGgxuhqvQCBYDFww83b0Wolvn3VmZNeA7jxmrNqtSyBQHCCICxjgUAgEAhqjBBjgUAg \n EAhqjBBjQdm54ebtYy5cgUAgEBwfIcYCgUAgENQYIcYCgUAgENQYIcYCgUAgENQYIcaCihCKpkTc \n WCAQCOaIEGOBQCAQCGqMEGOBQCAQCGqMEGOBQCAQCGqMEGOBQCAQCGqMEGOBQCAQCGqMEGOB4Dhs \n 3baDkUgKXyjJ1m07Jr02EkmNvSYQCASlUtLUpu7ubj1wG7AMMALf6Onp+XM5FyYQ1ANbt+1gT29o \n 7N97ekN87MaHyOWVSa9d9+PHufbSTSzz2GuxTIFAsMgp1TK+HPD39PScB1wE3FS+JQkE9cPeCUJc \n ZKIQFwlF0/zwv1+sxpIEAsEJSKnzjO8E7ir8twbIlWc5gsVO0X0LEIlnarwagUAgWByUZBn39PTE \n e3p6Yt3d3XZUYf5SeZclWIxMdOlm03Hi8QjX/fhxjgxHx7ZZbBOd1nU5jnlNp5XwjA7hjPjGXnPY \n jVx76aZqLk0gEJxAlGoZ093d3QncDfy4p6dn2/G2dzpFLK3S1Po73nskhJzP4T34NJHAEQD8zR38 \n p5zj1//2ZgC0Wgmo/Vrnyn986jw+9G/3MhJWrf1zRvbxhYduhqNHOehczmfe/31aGk3c/tULa7zS \n E4vFcn0sZsR3XF+UmsDlBu4Drunp6XloLvv4/dHjbyQoGafTXvPvWJEVhvY/QSzYj8nWjCRpiAUH \n OLTrYXy+85AkiXwh3lrrtc6HT759I1//5TMAXHXoPjh6FICV/sNoJPX9xfR56p16uJZP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8VVz53tm7bgW8kTHDEz4v9 \n eYy5nPrGDJZx7owzCT7
" text/plain " : [
" <matplotlib.figure.Figure at 0x110d71cd0> "
]
} ,
" metadata " : { } ,
" output_type " : " display_data "
}
] ,
" source " : [
" xfit = np.linspace(0, 10, 1000) \n " ,
" yfit = RandomForestRegressor(100).fit(x[:, None], y).predict(xfit[:, None]) \n " ,
" ytrue = model(xfit, 0) \n " ,
" \n " ,
" plt.errorbar(x, y, 0.3, fmt= ' o ' ) \n " ,
" plt.plot(xfit, yfit, ' -r ' ); \n " ,
" plt.plot(xfit, ytrue, ' -k ' , alpha=0.5); "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" As you can see, the non-parametric random forest model is flexible enough to fit the multi-period data, without us even specifying a multi-period model! \n " ,
" \n " ,
" Tradeoff between simplicity and thinking about what your data is. \n " ,
" \n " ,
" Feature engineering is important, need to know your domain: Fourier transform frequency distribution. "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Random Forest Limitations \n " ,
" \n " ,
" The following data scenarios are not well suited for random forests: \n " ,
" * y: lots of 0, few 1 \n " ,
" * Structured data like images where a neural network might be better \n " ,
" * Small data size which might lead to overfitting \n " ,
" * High dimensional data where a linear model might work better "
]
2015-04-29 05:27:02 +08:00
}
] ,
" metadata " : {
" kernelspec " : {
" display_name " : " Python 2 " ,
" language " : " python " ,
" name " : " python2 "
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
" version " : 2
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
" pygments_lexer " : " ipython2 " ,
" version " : " 2.7.9 "
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}