我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用sklearn.base.ClusterMixin()。
def _global_clustering(self, X=None): """ Global clustering for the subclusters obtained after fitting """ clusterer = self.n_clusters centroids = self.subcluster_centers_ compute_labels = (X is not None) and self.compute_labels # Preprocessing for the global clustering. not_enough_centroids = False if isinstance(clusterer, int): clusterer = AgglomerativeClustering( n_clusters=self.n_clusters) # There is no need to perform the global clustering step. if len(centroids) < self.n_clusters: not_enough_centroids = True elif (clusterer is not None and not hasattr(clusterer, 'fit_predict')): raise ValueError("n_clusters should be an instance of " "ClusterMixin or an int") # To use in predict to avoid recalculation. self._subcluster_norms = row_norms( self.subcluster_centers_, squared=True) if clusterer is None or not_enough_centroids: self.subcluster_labels_ = np.arange(len(centroids)) if not_enough_centroids: warnings.warn( "Number of subclusters found (%d) by Birch is less " "than (%d). Decrease the threshold." % (len(centroids), self.n_clusters)) else: # The global clustering step that clusters the subclusters of # the leaves. It assumes the centroids of the subclusters as # samples and finds the final centroids. self.subcluster_labels_ = clusterer.fit_predict( self.subcluster_centers_) if compute_labels: self.labels_ = self.predict(X)
def _generate_bases_test(est, pd_est): def test(self): self.assertTrue(isinstance(pd_est, FrameMixin), pd_est) self.assertFalse(isinstance(est, FrameMixin)) self.assertTrue(isinstance(pd_est, base.BaseEstimator)) try: mixins = [ base.ClassifierMixin, base.ClusterMixin, base.BiclusterMixin, base.TransformerMixin, base.DensityMixin, base.MetaEstimatorMixin, base.ClassifierMixin, base.RegressorMixin] except: if _sklearn_ver > 17: raise mixins = [ base.ClassifierMixin, base.ClusterMixin, base.BiclusterMixin, base.TransformerMixin, base.MetaEstimatorMixin, base.ClassifierMixin, base.RegressorMixin] for mixin in mixins: self.assertEqual( isinstance(pd_est, mixin), isinstance(est, mixin), mixin) return test
def get_params_for_est(estimator, name): '''Choose initialization parameters for an estimator for auto-testing''' is_classifier = ClassifierMixin in estimator.__mro__ is_cluster = ClusterMixin in estimator.__mro__ is_ensemble = BaseEnsemble in estimator.__mro__ uses_counts = any(c in name for c in USES_COUNTS) as_1d = name in REQUIRES_1D args, params, _ = get_args_kwargs_defaults(estimator.__init__) est_keys = set(('estimator', 'base_estimator', 'estimators')) est_keys = (set(params) | set(args)) & est_keys if is_classifier: score_func = feat.f_classif else: score_func = feat.f_regression for key in est_keys: if name == 'SelectFromModel': params[key] = sklearn.linear_model.LassoCV() elif is_classifier: params[key] = sklearn.tree.DecisionTreeClassifier() else: params[key] = sklearn.tree.DecisionTreeRegressor() if key == 'estimators': params[key] = [(str(_), clone(params[key])) for _ in range(10)] kw = dict(is_classifier=is_classifier, is_cluster=is_cluster, is_ensemble=is_ensemble, uses_counts=uses_counts) if 'score_func' in params: params['score_func'] = score_func X, y = make_X_y(**kw) return X, y, params, kw