This program creates a custom transformer that squares input numbers, then trains a linear regression model on the transformed data. It shows how to use the transformer inside a pipeline.
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import numpy as np
# Custom transformer that squares the input features
class SquareTransformer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X):
return X ** 2
# Create sample data
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# Build pipeline with custom transformer and linear regression
pipeline = Pipeline([
('square', SquareTransformer()),
('model', LinearRegression())
])
# Train model
pipeline.fit(X_train, y_train)
# Predict on test data
predictions = pipeline.predict(X_test)
# Print predictions and score
print('Predictions:', predictions)
print('Model R^2 score:', pipeline.score(X_test, y_test))