Implementation:Scikit learn Scikit learn Exceptions
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Error Handling |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for providing custom warnings and error classes used across scikit-learn.
Description
The sklearn.exceptions module defines custom exception and warning classes used throughout scikit-learn. Key classes include NotFittedError (raised when an estimator is used before fitting), ConvergenceWarning (for convergence issues), DataConversionWarning, FitFailedWarning, UndefinedMetricWarning, and UnsetMetadataPassedError for metadata routing errors.
Usage
Use these exception and warning classes when you need to handle scikit-learn-specific errors, such as catching NotFittedError to check if an estimator has been fitted, or filtering specific warnings during model training and evaluation.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/exceptions.py
Signature
class NotFittedError(ValueError, AttributeError):
class ConvergenceWarning(UserWarning):
class DataConversionWarning(UserWarning):
class DataDimensionalityWarning(UserWarning):
class EfficiencyWarning(UserWarning):
class FitFailedWarning(RuntimeWarning):
class UndefinedMetricWarning(UserWarning):
class UnsetMetadataPassedError(ValueError):
def __init__(self, *, message, unrequested_params, routed_params):
Import
from sklearn.exceptions import NotFittedError, ConvergenceWarning
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| message | str | Yes | The error or warning message (for UnsetMetadataPassedError) |
| unrequested_params | dict | Yes | Parameters provided but not requested (for UnsetMetadataPassedError) |
| routed_params | dict | Yes | Dictionary of routed parameters (for UnsetMetadataPassedError) |
Outputs
| Name | Type | Description |
|---|---|---|
| exception | Exception | Raised exception to be caught by the caller |
Usage Examples
Basic Usage
from sklearn.svm import LinearSVC
from sklearn.exceptions import NotFittedError
try:
LinearSVC().predict([[1, 2], [2, 3]])
except NotFittedError as e:
print("Estimator not fitted:", e)