Beispiel #1
0
class RandomForestRewardModel(RewardModel):
    """Models rewards with a random forest.

    Uses a modified version of scikit-learn which returns the predictions of all
    trees in the forest to predict both the mean and variance of the prediction.

    Parameters
    ----------
    incremental : boolean (default False)
        Whether to fit the forest incrementally
    inc_n_trees : integer (default 1)
        If incremental, the number of trees to add for each sample
    min_samples : integer (default 10)
        The minimum number of samples before the regressor is fitted
    """

    def __init__(self, incremental=False, inc_n_trees=1, min_samples=10, **kwargs):
        self._forest = RandomForestRegressor(warm_start=incremental, **kwargs)
        self._min_samples = min_samples
        self._inc_n_trees = inc_n_trees

        self._initialized = False
        self._X = []  # TODO Use a more efficient container?
        self._Y = []

    def report_sample(self, x, reward):
        x = np.atleast_1d(x)
        self._X.append(x)
        self._Y.append(reward)

        if self.num_samples < self._min_samples:
            return

        if self._forest.warm_start:
            self._forest.n_estimators += self._inc_n_trees

        self._forest.fit(self._X, self._Y)

    def predict(self, x):
        x = np.atleast_2d(x)
        if len(x.shape) > 2:
            raise ValueError('x must be at most 2D')

        outs = self._forest.retrieve(x)
        pred_mean = np.mean(outs, axis=0)
        pred_sd = np.std(outs, axis=0)
        return np.squeeze(pred_mean), np.squeeze(pred_sd)

    def clear(self):
        # TODO
        pass

    @property
    def num_samples(self):
        return len(self._X)