Show Sidebar Hide Sidebar

Comparing Random Forests and the Multi-Output Meta Estimator in Scikit-learn

An example to compare multi-output regression with random forest and the multioutput.MultiOutputRegressor meta-estimator.

This example illustrates the use of the multioutput.MultiOutputRegressor meta-estimator to perform multi-output regression. A random forest regressor is used, which supports multi-output regression natively, so the results can be compared.

The random forest regressor will only ever predict values within the range of observations or closer to zero for each of the targets. As a result the predictions are biased towards the centre of the circle.

Using a single underlying feature the model learns both the x and y coordinate as output

Version

In [1]:
import sklearn
sklearn.__version__
Out[1]:
'0.18.1'

Imports

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.multioutput import MultiOutputRegressor

Calculations

In [3]:
# Create a random dataset
rng = np.random.RandomState(1)
X = np.sort(200 * rng.rand(600, 1) - 100, axis=0)
y = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T
y += (0.5 - rng.rand(*y.shape))

X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                    train_size=400,
                                                    random_state=4)

max_depth = 30
regr_multirf = MultiOutputRegressor(RandomForestRegressor(max_depth=max_depth,
                                                          random_state=0))
regr_multirf.fit(X_train, y_train)

regr_rf = RandomForestRegressor(max_depth=max_depth, random_state=2)
regr_rf.fit(X_train, y_train)

# Predict on new data
y_multirf = regr_multirf.predict(X_test)
y_rf = regr_rf.predict(X_test)

Plot Results

In [4]:
s = 50
a = 0.4

data = go.Scatter(x=y_test[:, 0], y=y_test[:, 1],
                  mode='markers',
                  marker=dict(color="navy",
                             line=dict(width=1, color='black')), 
                  name="Data",
                  opacity=0.8
                 )

multi_rf_score = go.Scatter(x=y_multirf[:, 0], y=y_multirf[:, 1],
                            mode='markers',
                            marker=dict(color="cornflowerblue", 
                                        line=dict(width=1, color='black')),
                            name="Multi RF score=%.2f" % regr_multirf.score(X_test, y_test),
                            opacity=0.8
                           )

rf_score = go.Scatter(x=y_rf[:, 0], y=y_rf[:, 1],
                      mode='markers',
                      marker=dict(color='cyan',
                                  line=dict(width=1, color='black')),
                      name="RF score=%.2f" % regr_rf.score(X_test, y_test),
                      opacity=0.8
                     )
data_ = [data, multi_rf_score, rf_score]
layout = go.Layout(title="Comparing random forests and the multi-output meta estimator",
                   xaxis=dict(title='target1', showgrid=False,
                              zeroline=False),
                   yaxis=dict(title='target2', showgrid=False,
                              zeroline=False),
                   hovermode='closest'
                  )

fig = go.Figure(data=data_, layout=layout)
In [5]:
py.iplot(fig)
Out[5]:

License

Author:

    Tim Head <betatim@gmail.com>

License:

    BSD 3 clause
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.