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


Python KMeans.predict_proba方法代码示例

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


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

示例1: ModalPolicy

# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import predict_proba [as 别名]
class ModalPolicy(object):
    """Clusters the input space and returns local policies.
    """

    def __init__(self, optimizer=None, reward_model=None, mode_classifier=KNeighborsClassifier,
                 mode_args=None):
        if reward_model is None:
            self.reward_model = GPRewardModel()
        else:
            self.reward_model = reward_model
        self.reward_model_fitted = False

        self.mode_classifier = mode_classifier
        if mode_args is None:
            self.mode_args = {'weights': 'distance'}

        self.states = []
        self.actions = []
        self.rewards = []

        self.clusters = None
        self.clusters_init = False
        self.cluster_actions = []
        self.cluster_rewards = []
        self.active_clusters = []
        self.n_modes = 0

        self.sa_kde = KernelDensity()  # TODO

        if optimizer is None:
            self.optimizer = BFGSOptimizer(mode='max', num_restarts=3)
            self.optimizer.lower_bounds = -1
            self.optimizer.upper_bounds = 1  # TODO
        else:
            self.optimizer = optimizer

    def report_sample(self, s, a, r):
        x = np.hstack((s, a))
        # try:
        self.reward_model.report_sample(x, r)
        self.reward_model_fitted = True
        # except AttributeError:
            # self.reward_model_fitted = False

        self.states.append(s)
        self.actions.append(a)
        self.rewards.append(r)

    def get_action(self, s):
        s = np.asarray(s)
        if len(s.shape) < 2:
            s = s.reshape(1, -1)

        # TODO Support multiple queries?
        probs = self.clusters.predict_proba(s)
        ind = np.random.choice(self.active_clusters,
                               size=1,
                               p=np.atleast_1d(np.squeeze(probs)))
        a = [self.cluster_actions[i] for i in ind]

        return np.squeeze(a)

    def expected_reward(self, normalize=False):
        self.fit_reward_model()

        X = np.hstack((self.states, self.actions))
        r_pred, r_std = self.reward_model.predict(X, return_std=True)

        if normalize:
            logq = self.sa_kde.score_samples(X)
            logp = np.mean(logq)
            return importance_sample(x=r_pred, p_gen=logq, p_tar=logp,
                                     normalize=True, log_weight_lim=3)
        else:
            return np.mean(r_pred)

    def initialize_modes(self, n_modes, init_clusterer=None):
        if init_clusterer is None:
            self.clusters = KMeans(n_clusters=n_modes)
        else:
            self.clusters = init_clusterer

        self.n_modes = n_modes
        self.clusters.fit(X=self.states)
        self.cluster_actions = [None] * n_modes
        self.cluster_rewards = [None] * n_modes
        self.active_clusters = range(self.n_modes)
        self.optimize_mode_actions()

    def fit_reward_model(self):
        if self.reward_model_fitted:
            return

        # X = np.hstack((self.states, self.actions))
        # r = np.asarray(self.rewards).reshape(-1, 1)
        # self.reward_model.fit(X, r)
        # self.reward_model_fitted = True

    def optimize(self, n_iters, beta=1.0):
        for i in range(n_iters):
#.........这里部分代码省略.........
开发者ID:Humhu,项目名称:percepto,代码行数:103,代码来源:modal.py


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