当前位置: 首页>>代码示例>>Python>>正文


Python RandomForestRegressor.retrieve方法代码示例

本文整理汇总了Python中sklearn.ensemble.RandomForestRegressor.retrieve方法的典型用法代码示例。如果您正苦于以下问题:Python RandomForestRegressor.retrieve方法的具体用法?Python RandomForestRegressor.retrieve怎么用?Python RandomForestRegressor.retrieve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.ensemble.RandomForestRegressor的用法示例。


在下文中一共展示了RandomForestRegressor.retrieve方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: RandomForestRewardModel

# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import retrieve [as 别名]
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)
开发者ID:Humhu,项目名称:percepto,代码行数:57,代码来源:reward_models.py


注:本文中的sklearn.ensemble.RandomForestRegressor.retrieve方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。