本文整理汇总了Python中sklearn.ensemble.RandomTreesEmbedding.apply方法的典型用法代码示例。如果您正苦于以下问题:Python RandomTreesEmbedding.apply方法的具体用法?Python RandomTreesEmbedding.apply怎么用?Python RandomTreesEmbedding.apply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.RandomTreesEmbedding
的用法示例。
在下文中一共展示了RandomTreesEmbedding.apply方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_forest_embedding
# 需要导入模块: from sklearn.ensemble import RandomTreesEmbedding [as 别名]
# 或者: from sklearn.ensemble.RandomTreesEmbedding import apply [as 别名]
def random_forest_embedding(self, data, n_estimators=30, random_state=0, max_depth=3, min_samples_leaf=1):
"""
learn a density with random forest representation
"""
"""
scikit-learn only supports axis-align sepration, let's first stick to this and see how it works
"""
# n_estimators = 400
# random_state = 0
# max_depth = 5
rf_mdl = RandomTreesEmbedding(
n_estimators=n_estimators,
random_state=random_state,
max_depth=max_depth,
min_samples_leaf=min_samples_leaf)
rf_mdl.fit(data)
indices = rf_mdl.apply(data)
samples_by_node = defaultdict(list)
idx_by_node = defaultdict(list)
#kde_by_node = defaultdict(KernelDensity)
for idx, sample, est_data in zip(range(len(data)), data, indices):
for est_ind, leaf in enumerate(est_data):
samples_by_node[ est_ind, leaf ].append(sample)
idx_by_node[ est_ind, leaf ].append(idx)
res_mdl = dict()
res_mdl['rf_mdl'] = rf_mdl
res_mdl['samples_dict'] = samples_by_node
res_mdl['idx_dict'] = idx_by_node
# res_mdl['kde_dict'] = kde_by_node
return res_mdl
示例2: random_forest_embedding
# 需要导入模块: from sklearn.ensemble import RandomTreesEmbedding [as 别名]
# 或者: from sklearn.ensemble.RandomTreesEmbedding import apply [as 别名]
def random_forest_embedding(data, n_estimators=400, random_state=0, max_depth=5, min_samples_leaf=1):
"""
learn a density with random forest representation
"""
"""
scikit-learn only supports axis-align sepration, let's first stick to this and see how it works
"""
# n_estimators = 400
# random_state = 0
# max_depth = 5
rf_mdl = RandomTreesEmbedding(
n_estimators=n_estimators,
random_state=random_state,
max_depth=max_depth,
min_samples_leaf=min_samples_leaf)
rf_mdl.fit(data)
# forestClf.fit(trainingData, trainingLabels)
# indices = forestClf.apply(trainingData)
# samples_by_node = defaultdict(list)
# for est_ind, est_data in enumerate(indices.T):
# for sample_ind, leaf in enumerate(est_data):
# samples_by_node[ est_ind, leaf ].append(sample_ind)
# indexOfSamples = samples_by_node[0,10]
# # samples_by_node[treeIndex, leafIndex within that tree]
# leafNodeSamples = trainingAngles[indexOfSamples]
# kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(leafNodeSamples)
indices = rf_mdl.apply(data)
samples_by_node = defaultdict(list)
idx_by_node = defaultdict(list)
kde_by_node = defaultdict(KernelDensity)
for idx, sample, est_data in zip(range(len(data)), data, indices):
for est_ind, leaf in enumerate(est_data):
samples_by_node[ est_ind, leaf ].append(sample)
idx_by_node[ est_ind, leaf ].append(idx)
#Kernel Density Estimation for each leaf node
# for k,v in samples_by_node.iteritems():
# est_ind, leaf = k
# params = {'bandwidth': np.logspace(-1, 1, 20)}
# grid = GridSearchCV(KernelDensity(), params)
# grid.fit(v)
# kde_by_node[ est_ind, leaf ] = grid.best_estimator_
res_mdl = dict()
res_mdl['rf_mdl'] = rf_mdl
res_mdl['samples_dict'] = samples_by_node
res_mdl['idx_dict'] = idx_by_node
# res_mdl['kde_dict'] = kde_by_node
return res_mdl
示例3: EnsembleIOC
# 需要导入模块: from sklearn.ensemble import RandomTreesEmbedding [as 别名]
# 或者: from sklearn.ensemble.RandomTreesEmbedding import apply [as 别名]
class EnsembleIOC(BaseEstimator, RegressorMixin):
def __init__(self, n_estimators=20,
max_depth=5, min_samples_split=10, min_samples_leaf=10,
random_state=0,
em_itrs=5,
regularization=0.05,
passive_dyn_func=None,
passive_dyn_ctrl=None,
passive_dyn_noise=None,
verbose=False):
'''
n_estimators - number of ensembled models
... - a batch of parameters used for RandomTreesEmbedding, see relevant documents
em_itrs - maximum number of EM iterations to take
regularization - small positive scalar to prevent singularity of matrix inversion
passive_dyn_func - function to evaluate passive dynamics; None for MaxEnt model
passive_dyn_ctrl - function to return the control matrix which might depend on the state...
passive_dyn_noise - covariance of a Gaussian noise; only applicable when passive_dyn is Gaussian; None for MaxEnt model
note this implies a dynamical system with constant input gain. It is extendable to have state dependent
input gain then we need covariance for each data point
verbose - output training information
'''
BaseEstimator.__init__(self)
self.n_estimators=n_estimators
self.max_depth=max_depth
self.min_samples_split=min_samples_split
self.min_samples_leaf=min_samples_leaf
self.random_state=random_state
self.em_itrs=em_itrs
self.reg=regularization
self.passive_dyn_func=passive_dyn_func
self.passive_dyn_ctrl=passive_dyn_ctrl
self.passive_dyn_noise=passive_dyn_noise
self.verbose=verbose
return
def fit(self, X, y=None):
'''
y could be the array of starting state of the demonstrated trajectories/policies
if it is None, it implicitly implies a MaxEnt model. Other wise, it serves as the feature mapping
of the starting state. This data might also be potentially used for learning the passive dynamics
for a pure model-free learning with some regressors and regularization.
'''
#check parameters...
assert(type(self.n_estimators)==int)
assert(self.n_estimators > 0)
assert(type(self.max_depth)==int)
assert(self.max_depth > 0)
assert(type(self.min_samples_split)==int)
assert(self.min_samples_split > 0)
assert(type(self.min_samples_leaf)==int)
assert(self.min_samples_leaf > 0)
assert(type(self.em_itrs)==int)
#an initial partitioning of data with random forest embedding
self.random_embedding_mdl_ = RandomTreesEmbedding(
n_estimators=self.n_estimators,
max_depth=self.max_depth,
min_samples_split=self.min_samples_split,
min_samples_leaf=self.min_samples_leaf,
random_state=self.random_state
)
#we probably do not need the data type to differentiate it is a demonstration
#of trajectory or commanded state, do we?
if self.passive_dyn_func is not None and self.passive_dyn_ctrl is not None and self.passive_dyn_noise is not None:
self.random_embedding_mdl_.fit(X[:, X.shape[1]/2:])
indices = self.random_embedding_mdl_.apply(X[:, X.shape[1]/2:])
# X_tmp = np.array(X)
# X_tmp[:, X.shape[1]/2:] = X_tmp[:, X.shape[1]/2:] - X_tmp[:, :X.shape[1]/2]
# self.random_embedding_mdl_.fit(X_tmp)
# indices = self.random_embedding_mdl_.apply(X_tmp)
else:
self.random_embedding_mdl_.fit(X)
#figure out indices
indices = self.random_embedding_mdl_.apply(X)
partitioned_data = defaultdict(list)
leaf_idx = defaultdict(set)
weight_idx = defaultdict(float)
#group data belongs to the same partition and have the weights...
#is weight really necessary for EM steps? Hmm, seems to be for the initialization
#d_idx: data index; p_idx: partition index (comprised of estimator index and leaf index)
for d_idx, d, p_idx in zip(range(len(X)), X, indices):
for e_idx, l_idx in enumerate(p_idx):
partitioned_data[e_idx, l_idx].append(d)
leaf_idx[e_idx] |= {l_idx}
for e_idx, l_idx in enumerate(p_idx):
weight_idx[e_idx, l_idx] = float(len(partitioned_data[e_idx, l_idx])) / len(X)
# weight_idx[e_idx, l_idx] = 1. / len(p_idx)
#for each grouped data, solve an easy IOC problem by assuming quadratic cost-to-go function
#note that, if the passive dynamics need to be learned, extra steps is needed to train a regressor with weighted data
#otherwise, just a simply gaussian for each conditional probability distribution model
self.estimators_ = []
#.........这里部分代码省略.........