本文整理汇总了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)