, , , ,

Effectiveness of Extended Linear Modeling in Scikit-Learn

admin Avatar

The effectiveness of extended linear modeling in Scikit-Learn can be studied through polynomial features and pipeline tools, as described below.

Polynomial Features

Linear models trained on non-linear data maintain fast performance and can fit a broader range, which is why they are preferred in machine learning applications. Simple linear regression can be extended using polynomial features from coefficients. Scikit-Learn offers a module, called PolynomialFeatures, which transforms an input data matrix into a new data matrix of specified degree. An array of 8 is transformed into the form (4,2) in the following example.

import numpy as np
from sklearn.preprocessing import PolynomialFeatures

target = np.arange(16).reshape(8, 2)
poly_feature = PolynomialFeatures(degree = 2)
print(poly_feature.fit_transform(target))

Output

[[  1.   0.   1.   0.   0.   1.]
 [  1.   2.   3.   4.   6.   9.]
 [  1.   4.   5.  16.  20.  25.]
 [  1.   6.   7.  36.  42.  49.]
 [  1.   8.   9.  64.  72.  81.]
 [  1.  10.  11. 100. 110. 121.]
 [  1.  12.  13. 144. 156. 169.]
 [  1.  14.  15. 196. 210. 225.]]
Pipeline Tools

Preprocessing data by transforming it into a new matrix can be streamlined using pipeline tools to efficiently chain multiple estimators together. In the example below, pipeline tools streamline the preprocessing for fitting an order-3 polynomial data, where the linear model with polynomial features accurately recovers the input polynomial coefficients.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import numpy as np

model = Pipeline([('poly', PolynomialFeatures(degree=3)), ('linear', LinearRegression(fit_intercept = False))])

data = np.arange(5)
target = 5 - 2 * data + data ** 2 - data ** 3
stream_model = model.fit(data[:, np.newaxis], target)

print(stream_model.named_steps['linear'].coef_)

Output

[ 5. -2.  1. -1.]
References
  1. Hackeling, G. (2017). Mastering Machine Learning with scikit-learn, 2nd Edition. Packt Publishing Ltd.
  2. Géron, A. (2019). Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 2nd Edition. O’Reilly Media, Inc.
  3. Tutorials Point. Scikit Learn Tutorial. Retrieved November 20, 2025, from https://www.tutorialspoint.com/.

Tagged in :

admin Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Love