Implementation:Scikit learn Scikit learn ParamValidation
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Parameter Validation |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete utility module for validating estimator parameter types and values provided by scikit-learn.
Description
The _param_validation module provides a framework for declaring and enforcing parameter constraints on scikit-learn estimators and functions. It includes the validate_parameter_constraints function, the InvalidParameterError exception, and constraint classes like Interval, StrOptions, Options, HasMethods, and Hidden that describe valid parameter values.
Usage
Use these utilities when implementing scikit-learn estimators that need to validate their constructor parameters. Declare a _parameter_constraints dictionary and use the @validate_params decorator or call validate_parameter_constraints directly.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/utils/_param_validation.py
Signature
class InvalidParameterError(ValueError, TypeError):
...
def validate_parameter_constraints(parameter_constraints, params, caller_name):
...
def validate_params(prefer_skip_nested_validation):
...
class Interval:
...
class StrOptions:
...
class Options:
...
class HasMethods:
...
class Hidden:
...
Import
from sklearn.utils._param_validation import (
Interval,
InvalidParameterError,
StrOptions,
validate_params,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| parameter_constraints | dict | Yes | Dictionary mapping parameter names to lists of constraints |
| params | dict | Yes | Dictionary of parameter name to parameter value pairs to validate |
| caller_name | str | Yes | Name of the estimator or function for error messages |
Outputs
| Name | Type | Description |
|---|---|---|
| None | None | Raises InvalidParameterError if any parameter is invalid |
Usage Examples
Basic Usage
from sklearn.utils._param_validation import validate_parameter_constraints, Interval
from numbers import Integral
constraints = {
"n_neighbors": [Interval(Integral, 1, None, closed="left")],
}
params = {"n_neighbors": 5}
validate_parameter_constraints(constraints, params, caller_name="KNeighborsClassifier")