Skip to content

Make all estimators use _validate_params #23462

Closed
@jeremiedbb

Description

PR #22722 introduced a common method for the validation of the parameters of an estimator. We now need to use it in all estimators.

Please open one PR per estimator or family of estimators (if one inherits from another). The title of the PR must mention which estimator it's dealing with. We recommend using the following pattern for titles:

MAINT Parameters validation for <Estimator>

where <Estimator> is a placeholder to be replaced with the Estimator you chose.

The description of the PR must begin with Towards #23462 so that this issue and the PR are mutually crossed-linked.

Steps

  • The estimator must define a class attribute _parameter_constraints that defines the valid types and values for the parameters of the estimator. Do not rely only on the docstring of the estimator to define it: although it can help, it's important to primarily rely on the implementation to find the valid values because the docstring might not be completely accurate. See how it's done in KMeans for instance
    _parameter_constraints: dict = {
    "n_clusters": [Interval(Integral, 1, None, closed="left")],
    "init": [StrOptions({"k-means++", "random"}), callable, "array-like"],
    "n_init": [
    StrOptions({"auto"}),
    Hidden(StrOptions({"warn"})),
    Interval(Integral, 1, None, closed="left"),
    ],
    "max_iter": [Interval(Integral, 1, None, closed="left")],
    "tol": [Interval(Real, 0, None, closed="left")],
    "verbose": ["verbose"],
    "random_state": ["random_state"],
    }
  • If the estimator class inherits from a base class that already defines _parameter_constraints, we just need to extend it.
  • Then, the first thing that fit and partial_fit should do is call self._validate_params.
  • All existing simple param validation can now be removed. (simple means that does not depend on the input data or that does not depend on the value of another parameter for instance). Missing removal of such validation should be easy to spot with codecov since they become unreachable code.
  • Tests that checks error messages from simple param validation can also be removed (carefully: we need to keep the tests checking for more complex param validation !).
  • Finally, remove the estimator from the list of skipped estimators for the common param validation test
    PARAM_VALIDATION_ESTIMATORS_TO_IGNORE = [

    and make sure the test passes: pytest -vl sklearn/tests/test_common.py -k check_param_validation

Estimators to update

  • ARDRegression
  • AdaBoostClassifier
  • AdaBoostRegressor
  • AdditiveChi2Sampler
  • AffinityPropagation
  • AgglomerativeClustering
  • BaggingClassifier
  • BaggingRegressor
  • BayesianGaussianMixture
  • BayesianRidge
  • BernoulliNB
  • BernoulliRBM
  • Binarizer
  • Birch
  • CCA
  • CalibratedClassifierCV
  • CategoricalNB
  • ClassifierChain
  • ComplementNB
  • CountVectorizer
  • DBSCAN
  • DecisionTreeClassifier
  • DecisionTreeRegressor
  • DictVectorizer
  • DictionaryLearning
  • DummyClassifier
  • DummyRegressor
  • ElasticNet
  • ElasticNetCV
  • EllipticEnvelope
  • EmpiricalCovariance
  • ExtraTreeClassifier
  • ExtraTreeRegressor
  • ExtraTreesClassifier
  • ExtraTreesRegressor
  • FactorAnalysis
  • FastICA
  • FeatureAgglomeration
  • FeatureHasher
  • FunctionTransformer
  • GammaRegressor
  • GaussianMixture
  • GaussianNB
  • GaussianProcessClassifier
  • GaussianProcessRegressor
  • GaussianRandomProjection
  • GenericUnivariateSelect
  • GradientBoostingClassifier
  • GradientBoostingRegressor
  • GraphicalLasso
  • GraphicalLassoCV
  • HashingVectorizer
  • HistGradientBoostingClassifier
  • HistGradientBoostingRegressor
  • HuberRegressor
  • IncrementalPCA
  • IsolationForest
  • Isomap
  • IsotonicRegression
  • IterativeImputer
  • KBinsDiscretizer
  • KNNImputer
  • KNeighborsClassifier
  • KNeighborsRegressor
  • KNeighborsTransformer
  • KernelDensity
  • KernelPCA
  • KernelRidge
  • LabelBinarizer
  • LabelPropagation
  • LabelSpreading
  • Lars
  • LarsCV
  • Lasso
  • LassoCV
  • LassoLars
  • LassoLarsCV
  • LassoLarsIC
  • LatentDirichletAllocation
  • LedoitWolf
  • LinearDiscriminantAnalysis
  • LinearRegression
  • LinearSVC
  • LinearSVR
  • LocalOutlierFactor
  • LocallyLinearEmbedding
  • LogisticRegression
  • LogisticRegressionCV
  • MDS
  • MLPClassifier
  • MLPRegressor
  • MaxAbsScaler
  • MeanShift
  • MinCovDet
  • MinMaxScaler
  • MiniBatchDictionaryLearning
  • MiniBatchNMF
  • MiniBatchSparsePCA
  • MissingIndicator
  • MultiLabelBinarizer
  • MultiOutputClassifier
  • MultiOutputRegressor
  • MultiTaskElasticNet
  • MultiTaskElasticNetCV
  • MultiTaskLasso
  • MultiTaskLassoCV
  • MultinomialNB
  • NMF
  • NearestCentroid
  • NearestNeighbors
  • NeighborhoodComponentsAnalysis
  • Normalizer
  • NuSVC
  • NuSVR
  • Nystroem
  • OAS
  • OPTICS
  • OneClassSVM
  • OneHotEncoder
  • OneVsOneClassifier
  • OneVsRestClassifier
  • OrdinalEncoder
  • OrthogonalMatchingPursuit
  • OrthogonalMatchingPursuitCV
  • OutputCodeClassifier
  • PCA
  • PLSCanonical
  • PLSRegression
  • PLSSVD
  • PassiveAggressiveClassifier
  • PassiveAggressiveRegressor
  • PatchExtractor
  • Perceptron
  • PoissonRegressor
  • PolynomialCountSketch
  • PolynomialFeatures
  • PowerTransformer
  • QuadraticDiscriminantAnalysis
  • QuantileRegressor
  • QuantileTransformer
  • RANSACRegressor
  • RBFSampler
  • RFE
  • RFECV
  • RadiusNeighborsClassifier
  • RadiusNeighborsRegressor
  • RadiusNeighborsTransformer
  • RandomForestClassifier
  • RandomForestRegressor
  • RandomTreesEmbedding
  • RegressorChain
  • Ridge
  • RidgeCV
  • RidgeClassifier
  • RidgeClassifierCV
  • RobustScaler
  • SGDClassifier
  • SGDOneClassSVM
  • SGDRegressor
  • SVC
  • SVR
  • SelectFdr
  • SelectFpr
  • SelectFromModel
  • SelectFwe
  • SelectKBest
  • SelectPercentile
  • SelfTrainingClassifier
  • SequentialFeatureSelector
  • ShrunkCovariance
  • SimpleImputer
  • SkewedChi2Sampler
  • SparsePCA
  • SparseRandomProjection
  • SpectralBiclustering
  • SpectralClustering
  • SpectralCoclustering
  • SpectralEmbedding
  • SplineTransformer
  • StackingClassifier
  • StackingRegressor
  • StandardScaler
  • TSNE
  • TfidfTransformer
  • TfidfVectorizer
  • TheilSenRegressor
  • TransformedTargetRegressor
  • TruncatedSVD
  • TweedieRegressor
  • VarianceThreshold
  • VotingClassifier
  • VotingRegressor

Metadata

Assignees

No one assigned

    Labels

    EasyWell-defined and straightforward way to resolveMeta-issueGeneral issue associated to an identified list of tasksValidationrelated to input validationgood first issueEasy with clear instructions to resolve

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions