Exemplo n.º 1
0
 def _alternate_descent(self):
     n, d = self.X.shape
     self.centers = cluster._init_centers(self.centers, self.X,
                                          self.n_clusters)
     self.memberships = cluster._init_memberships(self.memberships,
                                                  self.centers, self.X,
                                                  self.n_clusters)
     self.weights = cluster._init_weights(self.weights, self.X,
                                          self.n_clusters)
     self.memberships, self.centers = super()._alternate_descent()
     for niter in range(self.max_iter):
         if self.verbose & Verbosity.COST_FUNCTION:
             self._log_cost(self.centers, self.memberships, self.weights)
         cst = np.min(
             1 / self._memberships_hessian(self.memberships, self.centers))
         print("Cst is {}".format(cst))
         #print("Cst is now {}".format(cst))
         new_memberships = _pfscm_prox_gradient(
             self.memberships,
             self._memberships_gradient,
             _prox_op,
             cst,
             prox_arg=cst * self.memberships_gamma,
             max_iter=10,
             tol=self.tol,
             grad_args=(self.X, self.centers))
         new_centers = self._update_centers(self.X, self.n_clusters,
                                            self.weights, new_memberships)
         gain = np.linalg.norm(new_centers - self.centers)
         if gain < self.tol:
             break
         self.centers = new_centers
         self.memberships = new_memberships
     return self.memberships, self.centers
Exemplo n.º 2
0
def weighted_fcm(x,
                 n_clusters,
                 max_iter=300,
                 tol=1e-4,
                 centers=None,
                 memberships=None,
                 weights=None,
                 **kwargs):
    n, d = x.shape
    centers = cluster._init_centers(centers, x, n_clusters, **kwargs)
    memberships = cluster._init_memberships(memberships, centers, x,
                                            n_clusters, **kwargs)
    weights = _init_weights(weights, x, n_clusters)
    for i in range(max_iter):
        new_memberships = cluster._update_memberships(x, n_clusters, weights,
                                                      centers, **kwargs)
        new_centers = cluster._update_centers(x, n_clusters, weights,
                                              new_memberships, **kwargs)
        new_weights = _new_update_weights(x, n_clusters, new_memberships,
                                          new_centers, **kwargs)
        if np.linalg.norm(new_centers - centers) < tol:
            break
        memberships = new_memberships
        centers = new_centers
        weights = new_weights
    return (memberships, centers, weights)
Exemplo n.º 3
0
 def _alternate_descent(self):
     n, d = self.X.shape
     self._init_weights()
     centers = cluster._init_centers(self.centers, self.X, self.n_clusters)
     memberships = cluster._init_memberships(self.memberships, centers,
                                             self.X, self.n_clusters)
     for i in range(self.max_iter):
         if self.verbose & Verbosity.COST_FUNCTION:
             self._log_cost(centers, memberships, self.weights)
         new_memberships = self._update_memberships(self.X, self.weights,
                                                    centers)
         new_centers = self._update_centers(self.X, self.weights,
                                            new_memberships)
         #            print(new_memberships)
         #            print("centers")
         #            print(new_centers)
         #            print("weights")
         #            print(self.weights)
         #            print(self._compute_inertia(self.X, centers, memberships,
         #                self.weights, _mode="debug"))
         new_weights = self._update_weights(self.X, new_memberships,
                                            new_centers, self.weights)
         #            print(self._compute_inertia(self.X, centers, memberships,
         #                new_weights, _mode="debug"))
         if np.linalg.norm(new_centers - centers) < self.tol:
             break
         memberships = new_memberships
         centers = new_centers
         self.weights = new_weights
     if self.verbose & Verbosity.COST_FUNCTION:
         self._log_cost(centers, memberships, self.weights)
     return memberships, centers
Exemplo n.º 4
0
def laplacian_weighted_fcm(x,
                           n_clusters,
                           weights,
                           affinity,
                           gamma=1.,
                           max_iter=300,
                           tol=1e-4,
                           centers=None,
                           memberships=None,
                           **kwargs):
    n, d = x.shape
    centers = cluster._init_centers(centers, x, n_clusters, **kwargs)
    memberships = cluster._init_memberships(memberships, centers, x,
                                            n_clusters, **kwargs)
    for i in range(max_iter):
        new_memberships = _new_laplacian_memberships(x,
                                                     n_clusters,
                                                     weights,
                                                     centers,
                                                     affinity,
                                                     memberships,
                                                     gamma=gamma,
                                                     **kwargs)
        new_centers = cluster._update_centers(x, n_clusters, weights,
                                              new_memberships, **kwargs)
        if np.linalg.norm(new_centers - centers) + np.linalg.norm(
                new_memberships - memberships) < tol:
            break
        memberships = new_memberships
        centers = new_centers
    return memberships, centers
Exemplo n.º 5
0
 def _alternate_descent(self):
     n, d = self.X.shape
     self.centers = cluster._init_centers(self.centers, self.X,
                                          self.n_clusters)
     self.memberships = cluster._init_memberships(self.memberships,
                                                  self.centers, self.X,
                                                  self.n_clusters)
     self.weights = cluster._init_weights(self.weights, self.X,
                                          self.n_clusters)
     fcm_init = cluster.FCMeans(self.n_clusters)
     fcm_init.fit(self.X)
     # self.memberships, self.centers = fcm_init.memberships, fcm_init.centers
     for i in range(self.max_iter):
         if self.verbose & Verbosity.COST_FUNCTION:
             self._log_cost(self.centers, self.memberships, self.weights)
         new_memberships = self._update_memberships(self.X, self.n_clusters,
                                                    self.weights,
                                                    self.centers,
                                                    self.memberships)
         new_centers = self._update_centers(self.X, self.n_clusters,
                                            self.weights, new_memberships)
         new_weights = self._update_weights(self.X, self.n_clusters,
                                            new_memberships, new_centers)
         if np.linalg.norm(new_centers - self.centers) < self.tol:
             break
         self.memberships = new_memberships
         self.centers = new_centers
         self.weights = new_weights
     if self.verbose & Verbosity.COST_FUNCTION:
         self._log_cost(self.centers, self.memberships, self.weights)
     self._fcm_init = fcm_init
     return self.memberships, self.centers, self.weights
Exemplo n.º 6
0
 def _alternate_descent(self):
     n, d = self.X.shape
     self._init_weights()
     n_clusters = self.n_clusters
     centers = initialization.max_min_method(self.X, n_clusters)
     memberships = cluster._init_memberships(self.memberships, centers,
                                             self.X, n_clusters)
     eta = _init_eta(self.X, centers, self.beta)
     weights = self.weights
     for i in range(self.max_iter):
         if self.verbose & Verbosity.COST_FUNCTION:
             self._log_cost(centers, memberships, weights)
         new_memberships = self._update_memberships(self.X, n_clusters,
                                                    weights, centers, eta)
         new_centers = self._update_centers(self.X, n_clusters, weights,
                                            new_memberships)
         if np.linalg.norm(new_centers - centers) < self.tol:
             break
         n_clusters, new_memberships, new_centers, new_weights = self._update_clusters(
             self.X, new_memberships, new_centers, weights)
         eta = self._update_eta(self.X, new_memberships, new_centers)
         memberships = new_memberships
         centers = new_centers
         weights = new_weights
     if self.verbose & Verbosity.COST_FUNCTION:
         self._log_cost(centers, memberships, self.weights)
     self.eta = eta
     return memberships, centers
Exemplo n.º 7
0
 def _alternate_descent(self):
     n, d = self.X.shape
     self.centers = cluster._init_centers(self.centers, self.X,
                                          self.n_clusters)
     self.memberships = cluster._init_memberships(self.memberships,
                                                  self.centers, self.X,
                                                  self.n_clusters)
     self.weights = cluster._init_weights(self.weights, self.X,
                                          self.n_clusters)
     self.memberships, self.centers = cluster.FCMeans._alternate_descent(
         self)
     for niter in range(self.max_iter):
         if self.verbose & Verbosity.COST_FUNCTION:
             self._log_cost(self.centers, self.memberships, self.weights)
         memberships_cst = np.min(1 / self._memberships_hessian(
             self.memberships, self.centers, self.weights))
         new_memberships = _pfscm_prox_gradient(
             self.memberships,
             self._memberships_gradient,
             _prox_op,
             memberships_cst,
             prox_arg=memberships_cst * self.memberships_gamma,
             max_iter=10,
             tol=self.tol,
             grad_args=(self.X, self.centers, self.weights))
         #print(np.sum(new_memberships))
         new_centers = self._update_centers(self.X, self.n_clusters,
                                            self.weights, new_memberships)
         weights_cst = np.min(1 / self._weights_hessian(
             new_memberships, new_centers, n, d, self.n_clusters))
         new_weights = _pfscm_prox_gradient(
             self.weights,
             self._weights_gradient,
             _sparsity_prox_op,
             weights_cst,
             prox_arg=weights_cst * self.weights_gamma,
             max_iter=10,
             tol=self.tol,
             grad_args=(self.X, new_centers, new_memberships))
         gain = np.linalg.norm(new_centers - self.centers)
         #print(self.centers)
         #print(gain)
         if gain < self.tol:
             break
         self.weights = new_weights
         self.centers = new_centers
         self.memberships = new_memberships
     return self.memberships, self.centers, self.weights
Exemplo n.º 8
0
 def _alternate_descent(self):
     n, d = self.X.shape
     centers = cluster._init_centers(self.centers, self.X, self.n_clusters)
     memberships = cluster._init_memberships(self.memberships, centers,
                                             self.X, self.n_clusters)
     weights = cluster._init_weights(self.weights, self.X, self.n_clusters)
     for i in range(self.max_iter):
         if self.verbose & Verbosity.COST_FUNCTION:
             self._log_cost(centers, memberships, weights)
         new_memberships = self._update_memberships(self.X, self.n_clusters,
                                                    weights, centers)
         new_centers = self._update_centers(self.X, self.n_clusters,
                                            weights, new_memberships)
         new_weights = self._update_weights(self.X, self.n_clusters,
                                            new_memberships, new_centers)
         if np.linalg.norm(new_centers - centers) < self.tol:
             break
         memberships = new_memberships
         centers = new_centers
         weights = new_weights
     if self.verbose & Verbosity.COST_FUNCTION:
         self._log_cost(centers, memberships, weights)
     return memberships, centers, weights
Exemplo n.º 9
0
 def _init_A(self):
     memberships = cluster._init_memberships(None, None, self.X,
                                             self.n_archetypes)
     return memberships.T  # different conventions between NMF and FCM
Exemplo n.º 10
0
 def _init_W(self):
     if self.W is None:
         memberships = cluster._init_memberships(None, self.H, self.X,
                 self.n_components)
         self.W = memberships.T
     return self.W