def kmeans(phate_op, n_clusters=8, random_state=None, k=None): """KMeans on the PHATE potential Clustering on the PHATE operator as introduced in Moon et al. This is similar to spectral clustering. Parameters ---------- phate_op : phate.PHATE Fitted PHATE operator n_clusters : int, optional (default: 8) Number of clusters random_state : int or None, optional (default: None) Random seed for k-means k : deprecated for `n_clusters` Returns ------- clusters : np.ndarray Integer array of cluster assignments """ if k is not None: warnings.warn("k is deprecated. Please use n_clusters in future.", FutureWarning) n_clusters = k if phate_op.graph is not None: diff_potential = phate_op.diff_potential return cluster.KMeans( n_clusters, random_state=random_state).fit_predict(diff_potential) else: raise exceptions.NotFittedError( "This PHATE instance is not fitted yet. Call " "'fit' with appropriate arguments before " "using this method.")
def kmeans(phate_op, k=8, random_state=None): """KMeans on the PHATE potential Clustering on the PHATE operator as introduced in Moon et al. This is similar to spectral clustering. Parameters ---------- phate_op : phate.PHATE Fitted PHATE operator k : int, optional (default: 8) Number of clusters random_state : int or None, optional (default: None) Random seed for k-means Returns ------- clusters : np.ndarray Integer array of cluster assignments """ if phate_op.graph is not None: diff_potential = phate_op.calculate_potential() if isinstance(phate_op.graph, graphtools.graphs.LandmarkGraph): diff_potential = phate_op.graph.interpolate(diff_potential) return cluster.KMeans( k, random_state=random_state).fit_predict(diff_potential) else: raise exceptions.NotFittedError( "This PHATE instance is not fitted yet. Call " "'fit' with appropriate arguments before " "using this method.")
def _check_is_fitted(self, method_name): if not self._cv.refit: raise exceptions.NotFittedError( 'This %s instance was initialized ' 'with refit=False. %s is ' 'available only after refitting on the best ' 'parameters. You can refit an estimator ' 'manually using the ``best_parameters_`` ' 'attribute' % (type(self).__name__, method_name)) else: check_is_fitted(self, 'best_estimator_')
def x_columns(self): """ The columns set in the last call to fit. Set this property at fit, and call it in other methods: """ try: return self.__cols except AttributeError: raise exceptions.NotFittedError()
def kmeans(phate_op, n_clusters="auto", max_clusters=10, random_state=None, k=None, **kwargs): """KMeans on the PHATE potential Clustering on the PHATE operator as introduced in Moon et al. This is similar to spectral clustering. Parameters ---------- phate_op : phate.PHATE Fitted PHATE operator n_clusters : int, optional (default: 'auto') Number of clusters. If 'auto', uses the Silhouette score to determine the optimal number of clusters max_clusters : int, optional (default: 10) Maximum number of clusters to test if using the Silhouette score. random_state : int or None, optional (default: None) Random seed for k-means k : deprecated for `n_clusters` kwargs : additional arguments for `sklearn.cluster.KMeans` Returns ------- clusters : np.ndarray Integer array of cluster assignments """ if k is not None: warnings.warn("k is deprecated. Please use n_clusters in future.", FutureWarning) n_clusters = k if not isinstance(phate_op, PHATE): raise TypeError( "Expected phate_op to be of type PHATE. Got {}".format(phate_op)) if phate_op.graph is not None: if n_clusters == "auto": n_clusters = np.arange(2, max_clusters) silhouette_scores = [ silhouette_score(phate_op, k, random_state=random_state, **kwargs) for k in n_clusters ] n_clusters = n_clusters[np.argmax(silhouette_scores)] return cluster.KMeans(n_clusters, random_state=random_state, **kwargs).fit_predict(phate_op.diff_potential) else: raise exceptions.NotFittedError( "This PHATE instance is not fitted yet. Call " "'fit' with appropriate arguments before " "using this method.")
def predict(self, X): """Compute predicted drawing""" if not self._is_fitted: raise skle.NotFittedError("This model is not fitted yet. Call " "'fit' with appropriate arguments " "before using this method.") if not isinstance(X, pd.core.frame.DataFrame): raise TypeError("`X` must be a pandas DataFrame, not %s" % type(X)) # Make sure we calculated the current maps for all electrodes in `X`: self.calc_curr_map(X) # Predict percept engine = 'serial' if self.engine == 'cython' else self.engine y_pred = p2pu.parfor(self._predicts, X.iterrows(), engine=engine, scheduler=self.scheduler, n_jobs=self.n_jobs) # Convert to DataFrame, preserving the index of `X` (otherwise # subtraction in the scoring function produces nan) return pd.DataFrame(y_pred, index=X.index)
def nbytes(self): try: return self._nbytes_ except AttributeError: raise exceptions.NotFittedError("Encoder has not yet been given " "a dataset; call fit() first")