Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Custom Transformers and Extensibility | Transformers and Preprocessing Workflows
Mastering scikit-learn API and Workflows

bookCustom Transformers and Extensibility

When you need to perform a data transformation that is not available in scikit-learn’s built-in suite, you can create your own transformer by following a few essential requirements. To make your custom transformer compatible with scikit-learn workflows, it must implement both the fit and transform methods. The fit method should learn any parameters needed from the data (even if there are none, it should still return self), while the transform method applies the actual transformation to the input data. For best compatibility, your transformer should inherit from both BaseEstimator and TransformerMixin, which provide helpful default implementations and ensure your class works seamlessly with pipeline utilities.

12345678910111213141516171819202122
from sklearn.base import BaseEstimator, TransformerMixin import numpy as np class AddConstantTransformer(BaseEstimator, TransformerMixin): def __init__(self, constant=1.0): self.constant = constant def fit(self, X, y=None): # No fitting necessary; just return self. return self def transform(self, X): # Add the constant to all elements of X. return np.asarray(X) + self.constant # Example usage: from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) transformer = AddConstantTransformer(constant=2.5) X_transformed = transformer.fit_transform(X) print(X_transformed[:3])
copy

With this approach, your custom transformer can be used in any place where a built-in transformer like StandardScaler would fit. Since it follows the same conventions — implementing fit and transform, and inheriting from BaseEstimator and TransformerMixin — you can drop it into a pipeline, combine it with other preprocessing steps, and use cross-validation just as you would with scikit-learn’s native tools. This interoperability is a core advantage of the scikit-learn API, letting you extend workflows with your own logic while preserving a consistent and reliable interface.

question mark

Which of the following statements about custom transformers in scikit-learn are correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookCustom Transformers and Extensibility

Swipe um das Menü anzuzeigen

When you need to perform a data transformation that is not available in scikit-learn’s built-in suite, you can create your own transformer by following a few essential requirements. To make your custom transformer compatible with scikit-learn workflows, it must implement both the fit and transform methods. The fit method should learn any parameters needed from the data (even if there are none, it should still return self), while the transform method applies the actual transformation to the input data. For best compatibility, your transformer should inherit from both BaseEstimator and TransformerMixin, which provide helpful default implementations and ensure your class works seamlessly with pipeline utilities.

12345678910111213141516171819202122
from sklearn.base import BaseEstimator, TransformerMixin import numpy as np class AddConstantTransformer(BaseEstimator, TransformerMixin): def __init__(self, constant=1.0): self.constant = constant def fit(self, X, y=None): # No fitting necessary; just return self. return self def transform(self, X): # Add the constant to all elements of X. return np.asarray(X) + self.constant # Example usage: from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) transformer = AddConstantTransformer(constant=2.5) X_transformed = transformer.fit_transform(X) print(X_transformed[:3])
copy

With this approach, your custom transformer can be used in any place where a built-in transformer like StandardScaler would fit. Since it follows the same conventions — implementing fit and transform, and inheriting from BaseEstimator and TransformerMixin — you can drop it into a pipeline, combine it with other preprocessing steps, and use cross-validation just as you would with scikit-learn’s native tools. This interoperability is a core advantage of the scikit-learn API, letting you extend workflows with your own logic while preserving a consistent and reliable interface.

question mark

Which of the following statements about custom transformers in scikit-learn are correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
some-alt