Fit the clustering model, computing the centers then embeds the centers
        into 2D space using the embedding method specified.
169900
def fit(self, X, y=None):
        
        with Timer() as self.fit_time_:
            # Fit the underlying estimator
            self.estimator.fit(X, y)

            # Get the centers
            # TODO: is this how sklearn stores all centers in the model?
            C = self.cluster_centers_

            # Embed the centers in 2D space and get the cluster scores
            self.embedded_centers_ = self.transformer.fit_transform(C)
            self.scores_ = self._score_clusters(X, y)

        # Draw the clusters and fit returns self
        self.draw()
        return self
980787
def neighborhood_preserving_embedding(self, X, num_dims=None, reweight=True):
    ''''''
    if reweight:
      W = self.barycenter_edge_weights(X).matrix()
    else:
      W = self.matrix()
    # compute M = (I-W)'(I-W) as in LLE
    M = W.T.dot(W) - W.T - W
    if issparse(M):
      M = M.toarray()
    M.flat[::M.shape[0] + 1] += 1
    # solve generalized eig problem: X'MXa = \lambda X'Xa
    vals, vecs = eig(X.T.dot(M).dot(X), X.T.dot(X), overwrite_a=True,
                     overwrite_b=True)
    if num_dims is None:
      return vecs
    return vecs[:,:num_dims]
387428
public List<double[]> addToClusteringCenters(List<double[]> clustering) {
		if (center != null && getClusteringFeature() != null) {
			clustering.add(getClusteringFeature().toClusterCenter());
		}
		for (ClusteringTreeNode child : children) {
			child.addToClusteringCenters(clustering);
		}
		return clustering;
	}
112257
def fit_cluster(data, assignments, k, P_init, R_init, means):
    
    for c in range(k):
        if data[:,assignments==c].shape[1] == 0:
            _, assignments = kmeans_pp(data, k)
    genes, cells = data.shape
    nb_gene_indices = np.array([True for i in range(genes)])
    for c in range(k):
        c_data = data[:,assignments==c]
        nb_gene_indices = nb_gene_indices & find_nb_genes(c_data)
    for c in range(k):
        c_data = data[:,assignments==c]
        nb_genes = c_data[nb_gene_indices,:]
        poisson_genes = c_data[~nb_gene_indices, :]
        P_init[nb_gene_indices, c], R_init[nb_gene_indices, c] = nb_fit(nb_genes)
        means[~nb_gene_indices, c] = poisson_genes.mean(1)
    return nb_gene_indices
1196532
def init_pp(X, n_clusters, random_state):
    
    x_squared_norms = row_norms(X, squared=True).compute()
    logger.info("Initializing with k-means++")
    with _timer("initialization of %2d centers" % n_clusters, _logger=logger):
        centers = sk_k_means._k_init(
            X, n_clusters, random_state=random_state, x_squared_norms=x_squared_norms
        )

    return centers
86689
private void setMeanDistancesToCentroids() {
        meanDistancesToCentroids = new HashMap<>();
        for (int i = 0; i < clusterCentroids.numInstances(); i++) {    //For each centroid
            int countAssignedInstances = 0;
            double sumDistancesToCentroid = 0.0;
            Instance centroidInstance = clusterCentroids.instance(i);
            for (int j = 0; j < trainingData.numInstances(); j++) {       //For each data point
                 if (i == centroidAssignments[j]) {
                     Instance valueInstance = trainingData.instance(j);
                     double distanceToCentroid = Math.abs(valueInstance.value(0) -
                                                    centroidInstance.value(0));
                     sumDistancesToCentroid += distanceToCentroid;
                     countAssignedInstances++;
                 }
            }
            double meanDistanceToCentroid = sumDistancesToCentroid / countAssignedInstances;
            meanDistancesToCentroids.put(centroidInstance, meanDistanceToCentroid);
        }
    }
396902
def _embedding_kernel_pca(matrix, dimensions=3, affinity_matrix=None,
                          sigma=1):
    
    if affinity_matrix is None:
        aff = rbf(matrix, sigma)
    else:
        aff = affinity_matrix
    kpca = sklearn.decomposition.KernelPCA(kernel='precomputed',
                                           n_components=dimensions)
    return kpca.fit_transform(aff)
798220
public ClusteringModelPrediction predictClustering(RowData data) throws PredictException {
    ClusteringModelPrediction p = new ClusteringModelPrediction();
    if (useExtendedOutput && (m instanceof IClusteringModel)) {
      IClusteringModel cm = (IClusteringModel) m;
      // setup raw input
      double[] rawData = nanArray(m.nfeatures());
      rawData = fillRawData(data, rawData);
      // get cluster assignment & distances
      final int k = cm.getNumClusters();
      p.distances = new double[k];
      p.cluster = cm.distances(rawData, p.distances);
    } else {
      double[] preds = preamble(ModelCategory.Clustering, data);
      p.cluster = (int) preds[0];
    }

    return p;
  }
390616
private List<Pair<Subspace, ModifiableDBIDs>> determineClusters(List<CLIQUESubspace> denseSubspaces) {
    List<Pair<Subspace, ModifiableDBIDs>> clusters = new ArrayList<>();

    for(CLIQUESubspace subspace : denseSubspaces) {
      List<Pair<Subspace, ModifiableDBIDs>> clustersInSubspace = subspace.determineClusters();
      if(LOG.isDebugging()) {
        LOG.debugFine("Subspace " + subspace + " clusters " + clustersInSubspace.size());
      }
      clusters.addAll(clustersInSubspace);
    }
    return clusters;
  }
254207
def _bisect_kmeans(X, n_clusters, n_trials, max_iter, tol):
    
    membs = np.empty(shape=X.shape[0], dtype=int)
    centers = dict() #np.empty(shape=(n_clusters,X.shape[1]), dtype=float)
    sse_arr = dict() #-1.0*np.ones(shape=n_clusters, dtype=float)

    ## data structure to store cluster hierarchies
    tree = treelib.Tree()
    tree = _add_tree_node(tree, 0, ilev=0, X=X) 

    km = _kmeans.KMeans(n_clusters=2, n_trials=n_trials, max_iter=max_iter, tol=tol)
    for i in range(1,n_clusters):
        sel_clust_id,sel_memb_ids = _select_cluster_2_split(membs, tree)
        X_sub = X[sel_memb_ids,:]
        km.fit(X_sub)

        #print("Bisecting Step %d    :"%i, sel_clust_id, km.sse_arr_, km.centers_)
        ## Updating the clusters & properties
        #sse_arr[[sel_clust_id,i]] = km.sse_arr_
        #centers[[sel_clust_id,i]] = km.centers_
        tree = _add_tree_node(tree, 2*i-1, i, \
                              size=np.sum(km.labels_ == 0), center=km.centers_[0], \
                              sse=km.sse_arr_[0], parent= sel_clust_id)
        tree = _add_tree_node(tree, 2*i,   i, \
                             size=np.sum(km.labels_ == 1), center=km.centers_[1], \
                             sse=km.sse_arr_[1], parent= sel_clust_id)

        pred_labels = km.labels_
        pred_labels[np.where(pred_labels == 1)[0]] = 2*i
        pred_labels[np.where(pred_labels == 0)[0]] = 2*i - 1
        #if sel_clust_id == 1:
        #    pred_labels[np.where(pred_labels == 0)[0]] = sel_clust_id
        #    pred_labels[np.where(pred_labels == 1)[0]] = i
        #else:
        #    pred_labels[np.where(pred_labels == 1)[0]] = i
        #    pred_labels[np.where(pred_labels == 0)[0]] = sel_clust_id

        membs[sel_memb_ids] = pred_labels


    for n in tree.leaves():
        label = n.data['label']
        centers[label] = n.data['center']
        sse_arr[label] = n.data['sse']

    return(centers, membs, sse_arr, tree)
1240247
def classify_clusters(points, n=10):
    
    arr = [[p.x, p.y] for p in points.values]
    clf = KMeans(n_clusters=n)
    clf.fit(arr)
    classes = clf.predict(arr)
    return classes
714096
private void computePrincipleVectors(int numPrincipleVectors) {

        // Group all of the sspace's vectors into a Matrix so that we can
        // cluster them
        final int numTerms = sspace.getWords().size();
        final List<DoubleVector> termVectors = 
            new ArrayList<DoubleVector>(numTerms);
        String[] termAssignments = new String[numTerms];
        int k = 0;
        for (String term : sspace.getWords()) {
            termVectors.add(Vectors.asDouble(sspace.getVector(term)));
            termAssignments[k++] = term;
        }

        Random random = new Random();

        final DoubleVector[] principles = new DoubleVector[numPrincipleVectors];
        for (int i = 0; i < principles.length; ++i)
            principles[i] = new DenseVector(sspace.getVectorLength());

        // Randomly assign the data set to different intitial clusters 
        final MultiMap<Integer,Integer> clusterAssignment = 
            new HashMultiMap<Integer,Integer>();
        for (int i = 0; i < numTerms; ++i) 
            clusterAssignment.put(random.nextInt(numPrincipleVectors), i);
        
        int numIters = 1;
        for (int iter = 0; iter < numIters; ++iter) {
            verbose(LOGGER, "Computing principle vectors (round %d/%d)", iter+1, numIters);
            // Compute the centroids
            for (Map.Entry<Integer,Set<Integer>> e
                     : clusterAssignment.asMap().entrySet()) {
                int cluster = e.getKey();
                DoubleVector principle = new DenseVector(sspace.getVectorLength());
                principles[cluster] = principle;
                for (Integer row : e.getValue())
                    VectorMath.add(principle, termVectors.get(row));
            }

            // Reassign each element to the centroid to which it is closest
            clusterAssignment.clear();
            final int numThreads = workQueue.availableThreads();
            Object key = workQueue.registerTaskGroup(numThreads);

            for (int threadId_ = 0; threadId_ < numThreads; ++threadId_) {
                final int threadId = threadId_;
                workQueue.add(key, new Runnable() {
                        public void run() {
                            // Thread local cache of all the cluster assignments
                            MultiMap<Integer,Integer> clusterAssignment_ = 
                                new HashMultiMap<Integer,Integer>();
                            // For each of the vectors that this thread is
                            // responsible for, find the principle vector to
                            // which it is closest
                            for (int i = threadId; i < numTerms; i += numThreads) {
                                DoubleVector v = termVectors.get(i);
                                double highestSim = -Double.MAX_VALUE;
                                int pVec = -1;
                                for (int j = 0; j < principles.length; ++j) {
                                    DoubleVector principle = principles[j];
                                    double sim = Similarity.cosineSimilarity(
                                        v, principle);
                                    assert sim >= -1 && sim <= 1 : "similarity "
                                        + " to principle vector " + j + " is "
                                        + "outside the expected range: " + sim;
                                    if (sim > highestSim) {
                                        highestSim = sim;
                                        pVec = j;
                                    }
                                }
                                assert pVec != -1 : "Could not find match to "
                                    + "any of the " + principles.length + 
                                    " principle vectors";
                                clusterAssignment_.put(pVec, i);
                            }
                            // Once all the vectors for this thread have been
                            // mapped, update the thread-shared mapping.  Do
                            // this here, rather than for each comparison to
                            // minimize the locking
                            synchronized (clusterAssignment) {
                                clusterAssignment.putAll(clusterAssignment_);
                            }
                        }
                    });
            }
            workQueue.await(key);
        }

        double mean = numTerms / (double)numPrincipleVectors;
        double variance = 0d;
        for (Map.Entry<Integer,Set<Integer>> e
                 : clusterAssignment.asMap().entrySet()) {
            Set<Integer> rows = e.getValue();
            Set<String> terms = new HashSet<String>();
            for (Integer i : rows)
                terms.add(termAssignments[i]);
             verbose(LOGGER, "Principle vectod %d is closest to %d terms",
                     e.getKey(), terms.size());
            double diff = mean - terms.size();
            variance += diff * diff;
            principleVectorToNearestTerms.putMany(
                principles[e.getKey()], terms);
        }

        verbose(LOGGER, "Average number terms per principle vector: %f, "+
                "(%f stddev)", mean, Math.sqrt(variance / numPrincipleVectors));
    }
630587
def _embedding_spectral(matrix, dimensions=3, unit_length=True,
                        affinity_matrix=None, sigma=1):
    
    if affinity_matrix is None:
        aff = rbf(matrix, sigma=sigma)
    else:
        aff = affinity_matrix
    coords = sklearn.manifold.spectral_embedding(aff, dimensions)
    return normalise_rows(coords) if unit_length else coords
1288754
def flesh_out(X, W, embed_dim, CC_labels, dist_mult=2.0, angle_thresh=0.2,
              min_shortcircuit=4, max_degree=5, verbose=False):
  ''''''
  W = W.astype(bool)
  assert np.all(W == W.T), 'graph given to flesh_out must be symmetric'
  D = pairwise_distances(X, metric='sqeuclidean')

  # compute average edge lengths for each point
  avg_edge_length = np.empty(X.shape[0])
  for i,nbr_mask in enumerate(W):
    avg_edge_length[i] = D[i,nbr_mask].mean()

  # candidate edges must satisfy edge length for at least one end point
  dist_thresh = dist_mult * avg_edge_length
  dist_mask = (D < dist_thresh) | (D < dist_thresh[:,None])
  # candidate edges must connect points >= min_shortcircuit hops away
  hops_mask = np.isinf(dijkstra(W, unweighted=True, limit=min_shortcircuit-1))
  # candidate edges must not already be connected, or in the same initial CC
  CC_mask = CC_labels != CC_labels[:,None]
  candidate_edges = ~W & dist_mask & hops_mask & CC_mask
  if verbose:  # pragma: no cover
    print('before F:', candidate_edges.sum(), 'potentials')

  # calc subspaces
  subspaces, _ = cluster_subspaces(X, embed_dim, CC_labels.max()+1, CC_labels)

  # upper triangular avoids p,q <-> q,p repeats
  ii,jj = np.where(np.triu(candidate_edges))
  # Get angles
  edge_dirs = X[ii] - X[jj]
  ssi = subspaces[CC_labels[ii]]
  ssj = subspaces[CC_labels[jj]]
  F = edge_cluster_angle(edge_dirs, ssi, ssj)

  mask = F < angle_thresh
  edge_ii = ii[mask]
  edge_jj = jj[mask]
  edge_order = np.argsort(F[mask])
  if verbose:  # pragma: no cover
    print('got', len(edge_ii), 'potential edges')
  # Prevent any one node from getting a really high degree
  degree = W.sum(axis=0)
  sorted_edges = np.column_stack((edge_ii, edge_jj))[edge_order]
  for e in sorted_edges:
    if degree[e].max() < max_degree:
      W[e[0],e[1]] = True
      W[e[1],e[0]] = True
      degree[e] += 1
  return Graph.from_adj_matrix(np.where(W, np.sqrt(D), 0))
1026046
def fit(self, X, y=None):
        

        if self.max_landmarks is not None:
            if self.n_clusters > self.n_landmarks:
                self.n_landmarks = self.max_landmarks
        
        if self.n_landmarks is None:
            distances = pdist(X, self.metric)
            tree = linkage(distances, method=self.linkage)
            self.landmark_labels_ = fcluster(tree, criterion='maxclust',
                                             t=self.n_clusters) - 1
            self.cardinality_ = np.bincount(self.landmark_labels_)
            self.squared_distances_within_cluster_ = np.zeros(self.n_clusters)

            n = len(X)
            for k in range(len(distances)):
                i = int(n - 2 - np.floor(np.sqrt(-8*k + 4*n*(n-1)-7)/2.0 - 0.5))
                j = int(k + i + 1 - n*(n-1)/2 + (n-i)*((n-i)-1)/2)
                if self.landmark_labels_[i] == self.landmark_labels_[j]:
                    self.squared_distances_within_cluster_[
                        self.landmark_labels_[i]] += distances[k] ** 2

            self.landmarks_ = X

        else:
            if self.landmark_strategy == 'random':
                land_indices = check_random_state(self.random_state).randint(
                    len(X), size=self.n_landmarks)
            else:
                land_indices = np.arange(len(X))[::(len(X) //
                                        self.n_landmarks)][:self.n_landmarks]

            distances = pdist(X[land_indices], self.metric)
            tree = linkage(distances, method=self.linkage)
            self.landmark_labels_ = fcluster(tree, criterion='maxclust',
                                             t=self.n_clusters) - 1
            self.cardinality_ = np.bincount(self.landmark_labels_)
            self.squared_distances_within_cluster_ = np.zeros(self.n_clusters)

            n = len(X[land_indices])
            for k in range(len(distances)):
                i = int(n - 2 - np.floor(np.sqrt(-8*k + 4*n*(n-1)-7)/2.0 - 0.5))
                j = int(k + i + 1 - n*(n-1)/2 + (n-i)*((n-i)-1)/2)
                if self.landmark_labels_[i] == self.landmark_labels_[j]:
                    self.squared_distances_within_cluster_[
                        self.landmark_labels_[i]] += distances[k] ** 2

            self.landmarks_ = X[land_indices]

        if self.metric != 'rmsd':
            cluster_centers_ = []
            for i in range(self.n_clusters):
                temp = list(np.mean(self.landmarks_[self.landmark_labels_==i], axis=0))
                cluster_centers_.append(temp)
            self.cluster_centers_ = np.array(cluster_centers_)

        return self
593052
def kmeans_clustering(self, numc, X=None, npcs=15):
        

        from sklearn.cluster import KMeans
        if X is None:
            D_sub = self.adata.uns['X_processed']
            X = (
                D_sub -
                D_sub.mean(0)).dot(
                self.adata.uns['pca_obj'].components_[
                    :npcs,
                    :].T)
            save = True
        else:
            save = False

        cl = KMeans(n_clusters=numc).fit_predict(Normalizer().fit_transform(X))

        if save:
            self.adata.obs['kmeans_clusters'] = pd.Categorical(cl)
        else:
            return cl
940414
def init_cao(X, n_clusters, dissim):
    
    n_points, n_attrs = X.shape
    centroids = np.empty((n_clusters, n_attrs), dtype='object')
    # Method is based on determining density of points.
    dens = np.zeros(n_points)
    for iattr in range(n_attrs):
        freq = defaultdict(int)
        for val in X[:, iattr]:
            freq[val] += 1
        for ipoint in range(n_points):
            dens[ipoint] += freq[X[ipoint, iattr]] / float(n_points) / float(n_attrs)

    # Choose initial centroids based on distance and density.
    centroids[0] = X[np.argmax(dens)]
    if n_clusters > 1:
        # For the remaining centroids, choose maximum dens * dissim to the
        # (already assigned) centroid with the lowest dens * dissim.
        for ik in range(1, n_clusters):
            dd = np.empty((ik, n_points))
            for ikk in range(ik):
                dd[ikk] = dissim(X, centroids[ikk]) * dens
            centroids[ik] = X[np.argmax(np.min(dd, axis=0))]

    return centroids