Using decision trees for regression
Although we have so far focused on using decision trees in classification tasks, you can also use them for regression. But you will need to use scikit-learn again, as OpenCV does not provide this flexibility. We therefore only briefly review its functionality here.
Let's say we wanted to use a decision tree to fit a sin
wave. To make things interesting, we will also add some noise to the data points using NumPy's random number generator:
In [1]: import numpy as np ... rng = np.random.RandomState(42)
We then create 100 x values between 0 and 5, and calculate the corresponding sin
values:
In [2]: X = np.sort(5 * rng.rand(100, 1), axis=0) ... y = np.sin(X).ravel()
We then add noise to every other data point in y (using y[::2]
), scaled by 0.5 so we don't introduce too much jitter:
In [3]: y[::2] += 0.5 * (0.5 - rng.rand(50))
You can then create regression tree like any other tree before. A small difference is that the split criteria 'gini'
and 'entropy'
do...