From 1dc23d7a1a798151a45ce1d72954821d61728411 Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Tue, 18 Oct 2022 17:20:17 -0400 Subject: [PATCH] ENH Makes OneToOneFeatureMixin and ClassNamePrefixFeaturesOutMixin public (#24688) Co-authored-by: Guillaume Lemaitre --- doc/developers/develop.rst | 14 ++++++++++-- doc/modules/classes.rst | 2 ++ doc/whats_new/v1.2.rst | 8 +++++++ sklearn/base.py | 25 +++++++++++++++++----- sklearn/cluster/_agglomerative.py | 4 ++-- sklearn/cluster/_birch.py | 4 ++-- sklearn/cluster/_kmeans.py | 4 ++-- sklearn/cross_decomposition/_pls.py | 6 +++--- sklearn/decomposition/_base.py | 4 ++-- sklearn/decomposition/_dict_learning.py | 4 ++-- sklearn/decomposition/_factor_analysis.py | 4 ++-- sklearn/decomposition/_fastica.py | 4 ++-- sklearn/decomposition/_kernel_pca.py | 4 ++-- sklearn/decomposition/_lda.py | 4 ++-- sklearn/decomposition/_nmf.py | 4 ++-- sklearn/decomposition/_sparse_pca.py | 4 ++-- sklearn/decomposition/_truncated_svd.py | 4 ++-- sklearn/discriminant_analysis.py | 4 ++-- sklearn/feature_extraction/text.py | 4 ++-- sklearn/isotonic.py | 2 +- sklearn/kernel_approximation.py | 10 ++++----- sklearn/manifold/_isomap.py | 4 ++-- sklearn/manifold/_locally_linear.py | 4 ++-- sklearn/neighbors/_graph.py | 6 +++--- sklearn/neighbors/_nca.py | 4 ++-- sklearn/neural_network/_rbm.py | 4 ++-- sklearn/preprocessing/_data.py | 26 +++++++++++------------ sklearn/preprocessing/_encoders.py | 4 ++-- sklearn/random_projection.py | 6 +++--- 29 files changed, 108 insertions(+), 73 deletions(-) diff --git a/doc/developers/develop.rst b/doc/developers/develop.rst index ef55be3cdffca..2278207ef08c3 100644 --- a/doc/developers/develop.rst +++ b/doc/developers/develop.rst @@ -647,8 +647,18 @@ scikit-learn introduces the `set_output` API for configuring transformers to output pandas DataFrames. The `set_output` API is automatically defined if the transformer defines :term:`get_feature_names_out` and subclasses :class:`base.TransformerMixin`. :term:`get_feature_names_out` is used to get the -column names of pandas output. You can opt-out of the `set_output` API by -setting `auto_wrap_output_keys=None` when defining a custom subclass:: +column names of pandas output. + +:class:`base.OneToOneFeatureMixin` and +:class:`base.ClassNamePrefixFeaturesOutMixin` are helpful mixins for defining +:term:`get_feature_names_out`. :class:`base.OneToOneFeatureMixin` is useful when +the transformer has a one-to-one correspondence between input features and output +features, such as :class:`~preprocessing.StandardScaler`. +:class:`base.ClassNamePrefixFeaturesOutMixin` is useful when the transformer +needs to generate its own feature names out, such as :class:`~decomposition.PCA`. + +You can opt-out of the `set_output` API by setting `auto_wrap_output_keys=None` +when defining a custom subclass:: class MyTransformer(TransformerMixin, BaseEstimator, auto_wrap_output_keys=None): diff --git a/doc/modules/classes.rst b/doc/modules/classes.rst index f575bf0ce01ab..b92561d3fc2bb 100644 --- a/doc/modules/classes.rst +++ b/doc/modules/classes.rst @@ -34,6 +34,8 @@ Base classes base.DensityMixin base.RegressorMixin base.TransformerMixin + base.OneToOneFeatureMixin + base.ClassNamePrefixFeaturesOutMixin feature_selection.SelectorMixin Functions diff --git a/doc/whats_new/v1.2.rst b/doc/whats_new/v1.2.rst index 7b7c33cb71cfd..68a5d1cfbe61d 100644 --- a/doc/whats_new/v1.2.rst +++ b/doc/whats_new/v1.2.rst @@ -115,6 +115,14 @@ Changelog :pr:`123456` by :user:`Joe Bloggs `. where 123456 is the *pull request* number, not the issue number. +:mod:`sklearn.base` +------------------- + +- |Enhancement| Introduces :class:`base.ClassNamePrefixFeaturesOutMixin` and + :class:`base.ClassNamePrefixFeaturesOutMixin` mixins that defines + :term:`get_feature_names_out` for common transformer uses cases. + :pr:`24688` by `Thomas Fan`_. + :mod:`sklearn.calibration` .......................... diff --git a/sklearn/base.py b/sklearn/base.py index d7d5d8f6644b5..db82353662c0d 100644 --- a/sklearn/base.py +++ b/sklearn/base.py @@ -811,6 +811,10 @@ class TransformerMixin(_SetOutputMixin): If :term:`get_feature_names_out` is defined, then `BaseEstimator` will automatically wrap `transform` and `fit_transform` to follow the `set_output` API. See the :ref:`developer_api_set_output` for details. + + :class:`base.OneToOneFeatureMixin` and + :class:`base.ClassNamePrefixFeaturesOutMixin` are helpful mixins for + defining :term:`get_feature_names_out`. """ def fit_transform(self, X, y=None, **fit_params): @@ -847,11 +851,11 @@ def fit_transform(self, X, y=None, **fit_params): return self.fit(X, y, **fit_params).transform(X) -class _OneToOneFeatureMixin: +class OneToOneFeatureMixin: """Provides `get_feature_names_out` for simple transformers. - Assumes there's a 1-to-1 correspondence between input features - and output features. + This mixin assumes there's a 1-to-1 correspondence between input features + and output features, such as :class:`~preprocessing.StandardScaler`. """ def get_feature_names_out(self, input_features=None): @@ -877,15 +881,26 @@ def get_feature_names_out(self, input_features=None): return _check_feature_names_in(self, input_features) -class _ClassNamePrefixFeaturesOutMixin: +class ClassNamePrefixFeaturesOutMixin: """Mixin class for transformers that generate their own names by prefixing. - Assumes that `_n_features_out` is defined for the estimator. + This mixin is useful when the transformer needs to generate its own feature + names out, such as :class:`~decomposition.PCA`. For example, if + :class:`~decomposition.PCA` outputs 3 features, then the generated feature + names out are: `["pca0", "pca1", "pca2"]`. + + This mixin assumes that a `_n_features_out` attribute is defined when the + transformer is fitted. `_n_features_out` is the number of output features + that the transformer will return in `transform` of `fit_transform`. """ def get_feature_names_out(self, input_features=None): """Get output feature names for transformation. + The feature names out will prefixed by the lowercased class name. For + example, if the transformer outputs 3 features, then the feature names + out are: `["class_name0", "class_name1", "class_name2"]`. + Parameters ---------- input_features : array-like of str or None, default=None diff --git a/sklearn/cluster/_agglomerative.py b/sklearn/cluster/_agglomerative.py index bcc89efab2135..ec54a915cc17a 100644 --- a/sklearn/cluster/_agglomerative.py +++ b/sklearn/cluster/_agglomerative.py @@ -15,7 +15,7 @@ from scipy import sparse from scipy.sparse.csgraph import connected_components -from ..base import BaseEstimator, ClusterMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, ClusterMixin, ClassNamePrefixFeaturesOutMixin from ..metrics.pairwise import paired_distances from ..metrics.pairwise import _VALID_METRICS from ..metrics import DistanceMetric @@ -1100,7 +1100,7 @@ def fit_predict(self, X, y=None): class FeatureAgglomeration( - _ClassNamePrefixFeaturesOutMixin, AgglomerativeClustering, AgglomerationTransform + ClassNamePrefixFeaturesOutMixin, AgglomerativeClustering, AgglomerationTransform ): """Agglomerate features. diff --git a/sklearn/cluster/_birch.py b/sklearn/cluster/_birch.py index 2241f87e8af3c..4c9d7921fdc70 100644 --- a/sklearn/cluster/_birch.py +++ b/sklearn/cluster/_birch.py @@ -15,7 +15,7 @@ TransformerMixin, ClusterMixin, BaseEstimator, - _ClassNamePrefixFeaturesOutMixin, + ClassNamePrefixFeaturesOutMixin, ) from ..utils.extmath import row_norms from ..utils._param_validation import Interval @@ -357,7 +357,7 @@ def radius(self): class Birch( - _ClassNamePrefixFeaturesOutMixin, ClusterMixin, TransformerMixin, BaseEstimator + ClassNamePrefixFeaturesOutMixin, ClusterMixin, TransformerMixin, BaseEstimator ): """Implements the BIRCH clustering algorithm. diff --git a/sklearn/cluster/_kmeans.py b/sklearn/cluster/_kmeans.py index 1d473b218e239..8fac729725b38 100644 --- a/sklearn/cluster/_kmeans.py +++ b/sklearn/cluster/_kmeans.py @@ -22,7 +22,7 @@ BaseEstimator, ClusterMixin, TransformerMixin, - _ClassNamePrefixFeaturesOutMixin, + ClassNamePrefixFeaturesOutMixin, ) from ..metrics.pairwise import euclidean_distances from ..metrics.pairwise import _euclidean_distances @@ -813,7 +813,7 @@ def _labels_inertia_threadpool_limit( class _BaseKMeans( - _ClassNamePrefixFeaturesOutMixin, TransformerMixin, ClusterMixin, BaseEstimator, ABC + ClassNamePrefixFeaturesOutMixin, TransformerMixin, ClusterMixin, BaseEstimator, ABC ): """Base class for KMeans and MiniBatchKMeans""" diff --git a/sklearn/cross_decomposition/_pls.py b/sklearn/cross_decomposition/_pls.py index 7c95f9fcdd701..bf3456791e660 100644 --- a/sklearn/cross_decomposition/_pls.py +++ b/sklearn/cross_decomposition/_pls.py @@ -15,7 +15,7 @@ from ..base import BaseEstimator, RegressorMixin, TransformerMixin from ..base import MultiOutputMixin -from ..base import _ClassNamePrefixFeaturesOutMixin +from ..base import ClassNamePrefixFeaturesOutMixin from ..utils import check_array, check_consistent_length from ..utils.fixes import sp_version from ..utils.fixes import parse_version @@ -159,7 +159,7 @@ def _svd_flip_1d(u, v): class _PLS( - _ClassNamePrefixFeaturesOutMixin, + ClassNamePrefixFeaturesOutMixin, TransformerMixin, RegressorMixin, MultiOutputMixin, @@ -901,7 +901,7 @@ def __init__( ) -class PLSSVD(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class PLSSVD(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """Partial Least Square SVD. This transformer simply performs a SVD on the cross-covariance matrix diff --git a/sklearn/decomposition/_base.py b/sklearn/decomposition/_base.py index 888fc3856d1b8..20bf7af4f284a 100644 --- a/sklearn/decomposition/_base.py +++ b/sklearn/decomposition/_base.py @@ -11,13 +11,13 @@ import numpy as np from scipy import linalg -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..utils.validation import check_is_fitted from abc import ABCMeta, abstractmethod class _BasePCA( - _ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator, metaclass=ABCMeta + ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator, metaclass=ABCMeta ): """Base class for PCA methods. diff --git a/sklearn/decomposition/_dict_learning.py b/sklearn/decomposition/_dict_learning.py index 9f9eba263d5b3..1957d5290c4cd 100644 --- a/sklearn/decomposition/_dict_learning.py +++ b/sklearn/decomposition/_dict_learning.py @@ -15,7 +15,7 @@ from scipy import linalg from joblib import Parallel, effective_n_jobs -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..utils import check_array, check_random_state, gen_even_slices, gen_batches from ..utils import deprecated from ..utils._param_validation import Hidden, Interval, StrOptions @@ -1152,7 +1152,7 @@ def dict_learning_online( return dictionary -class _BaseSparseCoding(_ClassNamePrefixFeaturesOutMixin, TransformerMixin): +class _BaseSparseCoding(ClassNamePrefixFeaturesOutMixin, TransformerMixin): """Base class from SparseCoder and DictionaryLearning algorithms.""" def __init__( diff --git a/sklearn/decomposition/_factor_analysis.py b/sklearn/decomposition/_factor_analysis.py index 825274f64383a..a6507d167b9cb 100644 --- a/sklearn/decomposition/_factor_analysis.py +++ b/sklearn/decomposition/_factor_analysis.py @@ -26,7 +26,7 @@ from scipy import linalg -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..utils import check_random_state from ..utils._param_validation import Interval, StrOptions from ..utils.extmath import fast_logdet, randomized_svd, squared_norm @@ -34,7 +34,7 @@ from ..exceptions import ConvergenceWarning -class FactorAnalysis(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class FactorAnalysis(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """Factor Analysis (FA). A simple linear generative model with Gaussian latent variables. diff --git a/sklearn/decomposition/_fastica.py b/sklearn/decomposition/_fastica.py index 3a30dccc05605..92de875f64ea3 100644 --- a/sklearn/decomposition/_fastica.py +++ b/sklearn/decomposition/_fastica.py @@ -15,7 +15,7 @@ import numpy as np from scipy import linalg -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..exceptions import ConvergenceWarning from ..utils import check_array, as_float_array, check_random_state from ..utils.validation import check_is_fitted @@ -337,7 +337,7 @@ def my_g(x): return returned_values -class FastICA(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class FastICA(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """FastICA: a fast algorithm for Independent Component Analysis. The implementation is based on [1]_. diff --git a/sklearn/decomposition/_kernel_pca.py b/sklearn/decomposition/_kernel_pca.py index 091bdfa95e62f..f109f96757bf8 100644 --- a/sklearn/decomposition/_kernel_pca.py +++ b/sklearn/decomposition/_kernel_pca.py @@ -17,12 +17,12 @@ ) from ..utils._param_validation import Interval, StrOptions from ..exceptions import NotFittedError -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..preprocessing import KernelCenterer from ..metrics.pairwise import pairwise_kernels -class KernelPCA(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class KernelPCA(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """Kernel Principal component analysis (KPCA) [1]_. Non-linear dimensionality reduction through the use of kernels (see diff --git a/sklearn/decomposition/_lda.py b/sklearn/decomposition/_lda.py index eea3ddbbb3e25..d187611251eda 100644 --- a/sklearn/decomposition/_lda.py +++ b/sklearn/decomposition/_lda.py @@ -17,7 +17,7 @@ from scipy.special import gammaln, logsumexp from joblib import Parallel, effective_n_jobs -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..utils import check_random_state, gen_batches, gen_even_slices from ..utils.validation import check_non_negative from ..utils.validation import check_is_fitted @@ -154,7 +154,7 @@ def _update_doc_distribution( class LatentDirichletAllocation( - _ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator + ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator ): """Latent Dirichlet Allocation with online variational Bayes algorithm. diff --git a/sklearn/decomposition/_nmf.py b/sklearn/decomposition/_nmf.py index f1c0b61c64eb5..16b6efca955d1 100644 --- a/sklearn/decomposition/_nmf.py +++ b/sklearn/decomposition/_nmf.py @@ -18,7 +18,7 @@ from ._cdnmf_fast import _update_cdnmf_fast from .._config import config_context -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..exceptions import ConvergenceWarning from ..utils import check_random_state, check_array, gen_batches from ..utils.extmath import randomized_svd, safe_sparse_dot, squared_norm @@ -1130,7 +1130,7 @@ def non_negative_factorization( return W, H, n_iter -class _BaseNMF(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator, ABC): +class _BaseNMF(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator, ABC): """Base class for NMF and MiniBatchNMF.""" _parameter_constraints: dict = { diff --git a/sklearn/decomposition/_sparse_pca.py b/sklearn/decomposition/_sparse_pca.py index 0404544787445..5974b86381e1a 100644 --- a/sklearn/decomposition/_sparse_pca.py +++ b/sklearn/decomposition/_sparse_pca.py @@ -11,11 +11,11 @@ from ..utils._param_validation import Hidden, Interval, StrOptions from ..utils.validation import check_array, check_is_fitted from ..linear_model import ridge_regression -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ._dict_learning import dict_learning, MiniBatchDictionaryLearning -class _BaseSparsePCA(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class _BaseSparsePCA(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """Base class for SparsePCA and MiniBatchSparsePCA""" _parameter_constraints: dict = { diff --git a/sklearn/decomposition/_truncated_svd.py b/sklearn/decomposition/_truncated_svd.py index 5d337d8c1abb8..999266a4f3f78 100644 --- a/sklearn/decomposition/_truncated_svd.py +++ b/sklearn/decomposition/_truncated_svd.py @@ -11,7 +11,7 @@ import scipy.sparse as sp from scipy.sparse.linalg import svds -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..utils import check_array, check_random_state from ..utils._arpack import _init_arpack_v0 from ..utils.extmath import randomized_svd, safe_sparse_dot, svd_flip @@ -22,7 +22,7 @@ __all__ = ["TruncatedSVD"] -class TruncatedSVD(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class TruncatedSVD(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """Dimensionality reduction using truncated SVD (aka LSA). This transformer performs linear dimensionality reduction by means of diff --git a/sklearn/discriminant_analysis.py b/sklearn/discriminant_analysis.py index cbc61ca61e479..0017c218e2fe0 100644 --- a/sklearn/discriminant_analysis.py +++ b/sklearn/discriminant_analysis.py @@ -16,7 +16,7 @@ from numbers import Real, Integral from .base import BaseEstimator, TransformerMixin, ClassifierMixin -from .base import _ClassNamePrefixFeaturesOutMixin +from .base import ClassNamePrefixFeaturesOutMixin from .linear_model._base import LinearClassifierMixin from .covariance import ledoit_wolf, empirical_covariance, shrunk_covariance from .utils.multiclass import unique_labels @@ -171,7 +171,7 @@ def _class_cov(X, y, priors, shrinkage=None, covariance_estimator=None): class LinearDiscriminantAnalysis( - _ClassNamePrefixFeaturesOutMixin, + ClassNamePrefixFeaturesOutMixin, LinearClassifierMixin, TransformerMixin, BaseEstimator, diff --git a/sklearn/feature_extraction/text.py b/sklearn/feature_extraction/text.py index 9d0d847c1d35c..8b931864169ac 100644 --- a/sklearn/feature_extraction/text.py +++ b/sklearn/feature_extraction/text.py @@ -24,7 +24,7 @@ import numpy as np import scipy.sparse as sp -from ..base import BaseEstimator, TransformerMixin, _OneToOneFeatureMixin +from ..base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin from ..preprocessing import normalize from ._hash import FeatureHasher from ._stop_words import ENGLISH_STOP_WORDS @@ -1486,7 +1486,7 @@ def _make_int_array(): class TfidfTransformer( - _OneToOneFeatureMixin, TransformerMixin, BaseEstimator, auto_wrap_output_keys=None + OneToOneFeatureMixin, TransformerMixin, BaseEstimator, auto_wrap_output_keys=None ): """Transform a count matrix to a normalized tf or tf-idf representation. diff --git a/sklearn/isotonic.py b/sklearn/isotonic.py index 3b312ee98c50d..0b5cedc5beb4e 100644 --- a/sklearn/isotonic.py +++ b/sklearn/isotonic.py @@ -413,7 +413,7 @@ def predict(self, T): return self.transform(T) # We implement get_feature_names_out here instead of using - # `_ClassNamePrefixFeaturesOutMixin`` because `input_features` are ignored. + # `ClassNamePrefixFeaturesOutMixin`` because `input_features` are ignored. # `input_features` are ignored because `IsotonicRegression` accepts 1d # arrays and the semantics of `feature_names_in_` are not clear for 1d arrays. def get_feature_names_out(self, input_features=None): diff --git a/sklearn/kernel_approximation.py b/sklearn/kernel_approximation.py index 0c483c4812bf5..d91fbdd1db7d2 100644 --- a/sklearn/kernel_approximation.py +++ b/sklearn/kernel_approximation.py @@ -22,7 +22,7 @@ from .base import BaseEstimator from .base import TransformerMixin -from .base import _ClassNamePrefixFeaturesOutMixin +from .base import ClassNamePrefixFeaturesOutMixin from .utils import check_random_state from .utils.extmath import safe_sparse_dot from .utils.validation import check_is_fitted @@ -35,7 +35,7 @@ class PolynomialCountSketch( - _ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator + ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator ): """Polynomial kernel approximation via Tensor Sketch. @@ -240,7 +240,7 @@ def transform(self, X): return data_sketch -class RBFSampler(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class RBFSampler(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """Approximate a RBF kernel feature map using random Fourier features. It implements a variant of Random Kitchen Sinks.[1] @@ -398,7 +398,7 @@ def _more_tags(self): class SkewedChi2Sampler( - _ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator + ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator ): """Approximate feature map for "skewed chi-squared" kernel. @@ -802,7 +802,7 @@ def _more_tags(self): return {"stateless": True, "requires_positive_X": True} -class Nystroem(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class Nystroem(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """Approximate a kernel map using a subset of the training data. Constructs an approximate feature map for an arbitrary kernel diff --git a/sklearn/manifold/_isomap.py b/sklearn/manifold/_isomap.py index f7e8d8b669812..27675c2663a0e 100644 --- a/sklearn/manifold/_isomap.py +++ b/sklearn/manifold/_isomap.py @@ -11,7 +11,7 @@ from scipy.sparse.csgraph import shortest_path from scipy.sparse.csgraph import connected_components -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..neighbors import NearestNeighbors, kneighbors_graph from ..neighbors import radius_neighbors_graph from ..utils.validation import check_is_fitted @@ -22,7 +22,7 @@ from ..metrics.pairwise import _VALID_METRICS -class Isomap(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class Isomap(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """Isomap Embedding. Non-linear dimensionality reduction through Isometric Mapping diff --git a/sklearn/manifold/_locally_linear.py b/sklearn/manifold/_locally_linear.py index 1c4d79feefdec..5a5b8218e03cf 100644 --- a/sklearn/manifold/_locally_linear.py +++ b/sklearn/manifold/_locally_linear.py @@ -15,7 +15,7 @@ BaseEstimator, TransformerMixin, _UnstableArchMixin, - _ClassNamePrefixFeaturesOutMixin, + ClassNamePrefixFeaturesOutMixin, ) from ..utils import check_random_state, check_array from ..utils._arpack import _init_arpack_v0 @@ -551,7 +551,7 @@ def locally_linear_embedding( class LocallyLinearEmbedding( - _ClassNamePrefixFeaturesOutMixin, + ClassNamePrefixFeaturesOutMixin, TransformerMixin, _UnstableArchMixin, BaseEstimator, diff --git a/sklearn/neighbors/_graph.py b/sklearn/neighbors/_graph.py index 0e09da534b6ae..418761c2d21ee 100644 --- a/sklearn/neighbors/_graph.py +++ b/sklearn/neighbors/_graph.py @@ -7,7 +7,7 @@ from ._base import KNeighborsMixin, RadiusNeighborsMixin from ._base import NeighborsBase from ._unsupervised import NearestNeighbors -from ..base import TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..utils._param_validation import StrOptions from ..utils.validation import check_is_fitted @@ -225,7 +225,7 @@ def radius_neighbors_graph( class KNeighborsTransformer( - _ClassNamePrefixFeaturesOutMixin, KNeighborsMixin, TransformerMixin, NeighborsBase + ClassNamePrefixFeaturesOutMixin, KNeighborsMixin, TransformerMixin, NeighborsBase ): """Transform X into a (weighted) graph of k nearest neighbors. @@ -448,7 +448,7 @@ def _more_tags(self): class RadiusNeighborsTransformer( - _ClassNamePrefixFeaturesOutMixin, + ClassNamePrefixFeaturesOutMixin, RadiusNeighborsMixin, TransformerMixin, NeighborsBase, diff --git a/sklearn/neighbors/_nca.py b/sklearn/neighbors/_nca.py index cf6e520767718..4a83fcc7bc080 100644 --- a/sklearn/neighbors/_nca.py +++ b/sklearn/neighbors/_nca.py @@ -14,7 +14,7 @@ from scipy.optimize import minimize from ..utils.extmath import softmax from ..metrics import pairwise_distances -from ..base import BaseEstimator, TransformerMixin, _ClassNamePrefixFeaturesOutMixin +from ..base import BaseEstimator, TransformerMixin, ClassNamePrefixFeaturesOutMixin from ..preprocessing import LabelEncoder from ..decomposition import PCA from ..utils.multiclass import check_classification_targets @@ -25,7 +25,7 @@ class NeighborhoodComponentsAnalysis( - _ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator + ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator ): """Neighborhood Components Analysis. diff --git a/sklearn/neural_network/_rbm.py b/sklearn/neural_network/_rbm.py index e9187657922a9..0624145116180 100644 --- a/sklearn/neural_network/_rbm.py +++ b/sklearn/neural_network/_rbm.py @@ -16,7 +16,7 @@ from ..base import BaseEstimator from ..base import TransformerMixin -from ..base import _ClassNamePrefixFeaturesOutMixin +from ..base import ClassNamePrefixFeaturesOutMixin from ..utils import check_random_state from ..utils import gen_even_slices from ..utils.extmath import safe_sparse_dot @@ -25,7 +25,7 @@ from ..utils._param_validation import Interval -class BernoulliRBM(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class BernoulliRBM(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): """Bernoulli Restricted Boltzmann Machine (RBM). A Restricted Boltzmann Machine with binary visible units and diff --git a/sklearn/preprocessing/_data.py b/sklearn/preprocessing/_data.py index b75491cf06ab9..b38bc6940b140 100644 --- a/sklearn/preprocessing/_data.py +++ b/sklearn/preprocessing/_data.py @@ -20,8 +20,8 @@ from ..base import ( BaseEstimator, TransformerMixin, - _OneToOneFeatureMixin, - _ClassNamePrefixFeaturesOutMixin, + OneToOneFeatureMixin, + ClassNamePrefixFeaturesOutMixin, ) from ..utils import check_array from ..utils._param_validation import Interval, StrOptions @@ -267,7 +267,7 @@ def scale(X, *, axis=0, with_mean=True, with_std=True, copy=True): return X -class MinMaxScaler(_OneToOneFeatureMixin, TransformerMixin, BaseEstimator): +class MinMaxScaler(OneToOneFeatureMixin, TransformerMixin, BaseEstimator): """Transform features by scaling each feature to a given range. This estimator scales and translates each feature individually such @@ -641,7 +641,7 @@ def minmax_scale(X, feature_range=(0, 1), *, axis=0, copy=True): return X -class StandardScaler(_OneToOneFeatureMixin, TransformerMixin, BaseEstimator): +class StandardScaler(OneToOneFeatureMixin, TransformerMixin, BaseEstimator): """Standardize features by removing the mean and scaling to unit variance. The standard score of a sample `x` is calculated as: @@ -1058,7 +1058,7 @@ def _more_tags(self): return {"allow_nan": True, "preserves_dtype": [np.float64, np.float32]} -class MaxAbsScaler(_OneToOneFeatureMixin, TransformerMixin, BaseEstimator): +class MaxAbsScaler(OneToOneFeatureMixin, TransformerMixin, BaseEstimator): """Scale each feature by its maximum absolute value. This estimator scales and translates each feature individually such @@ -1359,7 +1359,7 @@ def maxabs_scale(X, *, axis=0, copy=True): return X -class RobustScaler(_OneToOneFeatureMixin, TransformerMixin, BaseEstimator): +class RobustScaler(OneToOneFeatureMixin, TransformerMixin, BaseEstimator): """Scale features using statistics that are robust to outliers. This Scaler removes the median and scales the data according to @@ -1860,7 +1860,7 @@ def normalize(X, norm="l2", *, axis=1, copy=True, return_norm=False): return X -class Normalizer(_OneToOneFeatureMixin, TransformerMixin, BaseEstimator): +class Normalizer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator): """Normalize samples individually to unit norm. Each sample (i.e. each row of the data matrix) with at least one @@ -2037,7 +2037,7 @@ def binarize(X, *, threshold=0.0, copy=True): return X -class Binarizer(_OneToOneFeatureMixin, TransformerMixin, BaseEstimator): +class Binarizer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator): """Binarize data (set feature values to 0 or 1) according to a threshold. Values greater than the threshold map to 1, while values less than @@ -2166,7 +2166,7 @@ def _more_tags(self): return {"stateless": True} -class KernelCenterer(_ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): +class KernelCenterer(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator): r"""Center an arbitrary kernel matrix :math:`K`. Let define a kernel :math:`K` such that: @@ -2308,9 +2308,9 @@ def transform(self, K, copy=True): @property def _n_features_out(self): """Number of transformed output features.""" - # Used by _ClassNamePrefixFeaturesOutMixin. This model preserves the + # Used by ClassNamePrefixFeaturesOutMixin. This model preserves the # number of input features but this is not a one-to-one mapping in the - # usual sense. Hence the choice not to use _OneToOneFeatureMixin to + # usual sense. Hence the choice not to use OneToOneFeatureMixin to # implement get_feature_names_out for this class. return self.n_features_in_ @@ -2375,7 +2375,7 @@ def add_dummy_feature(X, value=1.0): return np.hstack((np.full((n_samples, 1), value), X)) -class QuantileTransformer(_OneToOneFeatureMixin, TransformerMixin, BaseEstimator): +class QuantileTransformer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator): """Transform features using quantiles information. This method transforms the features to follow a uniform or a normal @@ -2949,7 +2949,7 @@ def quantile_transform( ) -class PowerTransformer(_OneToOneFeatureMixin, TransformerMixin, BaseEstimator): +class PowerTransformer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator): """Apply a power transform featurewise to make data more Gaussian-like. Power transforms are a family of parametric, monotonic transformations diff --git a/sklearn/preprocessing/_encoders.py b/sklearn/preprocessing/_encoders.py index 2398f7d68120a..ceedfccf729f3 100644 --- a/sklearn/preprocessing/_encoders.py +++ b/sklearn/preprocessing/_encoders.py @@ -9,7 +9,7 @@ import numpy as np from scipy import sparse -from ..base import BaseEstimator, TransformerMixin, _OneToOneFeatureMixin +from ..base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin from ..utils import check_array, is_scalar_nan from ..utils.validation import check_is_fitted from ..utils.validation import _check_feature_names_in @@ -1055,7 +1055,7 @@ def get_feature_names_out(self, input_features=None): return np.array(feature_names, dtype=object) -class OrdinalEncoder(_OneToOneFeatureMixin, _BaseEncoder): +class OrdinalEncoder(OneToOneFeatureMixin, _BaseEncoder): """ Encode categorical features as an integer array. diff --git a/sklearn/random_projection.py b/sklearn/random_projection.py index e6b60cfaeb5da..df146e98ae732 100644 --- a/sklearn/random_projection.py +++ b/sklearn/random_projection.py @@ -35,7 +35,7 @@ import scipy.sparse as sp from .base import BaseEstimator, TransformerMixin -from .base import _ClassNamePrefixFeaturesOutMixin +from .base import ClassNamePrefixFeaturesOutMixin from .utils import check_random_state from .utils._param_validation import Interval, StrOptions @@ -292,7 +292,7 @@ def _sparse_random_matrix(n_components, n_features, density="auto", random_state class BaseRandomProjection( - TransformerMixin, BaseEstimator, _ClassNamePrefixFeaturesOutMixin, metaclass=ABCMeta + TransformerMixin, BaseEstimator, ClassNamePrefixFeaturesOutMixin, metaclass=ABCMeta ): """Base class for random projections. @@ -419,7 +419,7 @@ def fit(self, X, y=None): def _n_features_out(self): """Number of transformed output features. - Used by _ClassNamePrefixFeaturesOutMixin.get_feature_names_out. + Used by ClassNamePrefixFeaturesOutMixin.get_feature_names_out. """ return self.n_components