Python ML Multiple and Polynomial Regression
Multiple regression uses several inputs at once. Polynomial regression adds curved inputs, like a squared column, to bend the line.
Add a second column
Two input columns give the model two slopes to learn instead of one.
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
X = housing.data[:, [0, 1]]
print(X.shape)
print(X[0])A squared column bends the line
PolynomialFeatures turns one input into the input and its square.
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=2, include_bias=False)
print(poly.fit_transform([[2.0]]))More features, better score
Three features beat one feature on the test set. The extra columns carry real signal.
from sklearn.datasets import fetch_california_housing
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
housing = fetch_california_housing()
X = housing.data
y = housing.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
one = LinearRegression().fit(X_train[:, [0]], y_train)
three = LinearRegression().fit(X_train[:, [0, 1, 2]], y_train)
print(round(one.score(X_test[:, [0]], y_test), 4))
print(round(three.score(X_test[:, [0, 1, 2]], y_test), 4))Example
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
housing = fetch_california_housing()
X = housing.data
y = housing.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
one = LinearRegression().fit(X_train[:, [0]], y_train)
print("One feature train:", round(one.score(X_train[:, [0]], y_train), 4), "test:", round(one.score(X_test[:, [0]], y_test), 4))
three = LinearRegression().fit(X_train[:, [0, 1, 2]], y_train)
print("Three features train:", round(three.score(X_train[:, [0, 1, 2]], y_train), 4), "test:", round(three.score(X_test[:, [0, 1, 2]], y_test), 4))
poly = PolynomialFeatures(degree=2, include_bias=False)
X_train_poly = poly.fit_transform(X_train[:, [0, 1, 2]])
X_test_poly = poly.transform(X_test[:, [0, 1, 2]])
curved = LinearRegression().fit(X_train_poly, y_train)
print("Curved train:", round(curved.score(X_train_poly, y_train), 4), "test:", round(curved.score(X_test_poly, y_test), 4))
wild = PolynomialFeatures(degree=9, include_bias=False)
X_train_wild = wild.fit_transform(X_train[:, [0]])
X_test_wild = wild.transform(X_test[:, [0]])
too_much = LinearRegression().fit(X_train_wild, y_train)
print("Degree 9 train:", round(too_much.score(X_train_wild, y_train), 4), "test:", round(too_much.score(X_test_wild, y_test), 4))Key points
- Multiple regression: one model with several input columns
- Use more columns when one column misses part of the pattern
- Polynomial regression: a model that also sees squared or cubed inputs
- Use PolynomialFeatures to build the curved columns for you
- A curved model can fit the training data better than a straight one
- A fancier model is not always better on new data
- Use the test score to judge a fancier model, never the training score alone
In this example, the California Housing data is modelled four ways on the same split. One feature, the median income, scores 0.4708 on the test set. Three features score 0.5118, so the extra columns help. Adding squared and multiplied columns lifts the test score to 0.5276, so a gentle curve helps too. But a degree 9 curve scores only 0.4161 on the test set, worse than the simple line, and even its training score falls to 0.4404. The huge ninth powers make the fit unstable, so the extra curves hurt instead of helping.
Note
PolynomialFeatures with degree 9 builds powers up to the ninth, and those huge numbers make the fit unstable.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
How do you tell whether a fancier model is really better?