machine learning
scikit-learn
gaussian regression
polynomial regression
python tutorials

How to do gaussian/polynomial regression with scikit-learn?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Polynomial regression and Gaussian Process Regression are both useful for non-linear relationships, but they behave very differently. Polynomial regression is a parametric model with fixed feature expansion, while Gaussian processes are non-parametric and provide uncertainty estimates. In scikit-learn, both can be implemented cleanly with pipelines and proper validation.

When to Use Each Method

Polynomial regression is usually better when:

  • dataset is medium or large
  • relationship shape can be approximated by low-degree polynomial
  • fast training and simple deployment are priorities

Gaussian Process Regression is usually better when:

  • dataset is small to medium
  • uncertainty estimates are required
  • smooth function assumptions are reasonable

Choosing based on problem constraints avoids unnecessary complexity.

Prepare a Non-Linear Example Dataset

python
1import numpy as np
2from sklearn.model_selection import train_test_split
3
4rng = np.random.default_rng(42)
5X = np.linspace(-3, 3, 200).reshape(-1, 1)
6y = np.sin(X).ravel() + 0.15 * rng.normal(size=X.shape[0])
7
8X_train, X_test, y_train, y_test = train_test_split(
9    X, y, test_size=0.25, random_state=42
10)

This gives a smooth but noisy target function, good for comparing both models.

Polynomial Regression with a Pipeline

Use PolynomialFeatures and LinearRegression in one pipeline so transforms are consistent across train and test data.

python
1from sklearn.pipeline import Pipeline
2from sklearn.preprocessing import PolynomialFeatures
3from sklearn.linear_model import LinearRegression
4from sklearn.metrics import mean_squared_error, r2_score
5
6poly_model = Pipeline([
7    ("poly", PolynomialFeatures(degree=5, include_bias=False)),
8    ("linreg", LinearRegression())
9])
10
11poly_model.fit(X_train, y_train)
12poly_pred = poly_model.predict(X_test)
13
14print("Polynomial RMSE:", mean_squared_error(y_test, poly_pred, squared=False))
15print("Polynomial R2:", r2_score(y_test, poly_pred))

Degree controls flexibility. Higher degree can overfit quickly.

Gaussian Process Regression

Gaussian Process Regression needs a kernel choice. A common default is RBF plus white noise term.

python
1from sklearn.gaussian_process import GaussianProcessRegressor
2from sklearn.gaussian_process.kernels import RBF, WhiteKernel, ConstantKernel
3
4kernel = ConstantKernel(1.0, (1e-3, 1e3)) * RBF(length_scale=1.0) + WhiteKernel(noise_level=0.1)
5
6gpr_model = GaussianProcessRegressor(
7    kernel=kernel,
8    alpha=1e-6,
9    normalize_y=True,
10    random_state=42
11)
12
13gpr_model.fit(X_train, y_train)
14gpr_pred, gpr_std = gpr_model.predict(X_test, return_std=True)
15
16print("GPR RMSE:", mean_squared_error(y_test, gpr_pred, squared=False))
17print("GPR R2:", r2_score(y_test, gpr_pred))

gpr_std gives prediction uncertainty, which is often a major reason to choose GPR.

Visualize Predictions and Uncertainty

python
1import matplotlib.pyplot as plt
2
3X_plot = np.linspace(-3.5, 3.5, 300).reshape(-1, 1)
4poly_plot = poly_model.predict(X_plot)
5gpr_plot, gpr_plot_std = gpr_model.predict(X_plot, return_std=True)
6
7plt.figure(figsize=(10, 6))
8plt.scatter(X_train, y_train, s=15, alpha=0.5, label="Train")
9plt.plot(X_plot, poly_plot, label="Polynomial", linewidth=2)
10plt.plot(X_plot, gpr_plot, label="GPR", linewidth=2)
11plt.fill_between(
12    X_plot.ravel(),
13    gpr_plot - 1.96 * gpr_plot_std,
14    gpr_plot + 1.96 * gpr_plot_std,
15    alpha=0.2,
16    label="GPR 95% interval"
17)
18plt.legend()
19plt.show()

The confidence band is a practical advantage of GPR in risk-sensitive tasks.

Model Selection and Tuning

For polynomial models, tune degree with cross-validation. For GPR, tune kernel structure and bounds. Compare with metrics plus latency and memory footprint.

Simple degree search example:

python
1from sklearn.model_selection import GridSearchCV
2
3param_grid = {"poly__degree": [2, 3, 4, 5, 6, 7]}
4search = GridSearchCV(poly_model, param_grid, cv=5, scoring="neg_mean_squared_error")
5search.fit(X_train, y_train)
6print("Best degree:", search.best_params_)

For GPR, kernel tuning is more computationally expensive, so keep search space focused.

Practical Tradeoffs in Deployment

Polynomial regression exports and serves easily with low overhead. GPR can become expensive as training size grows because complexity increases strongly with sample count. For large data, consider approximate methods or different model families if uncertainty estimates are not required.

In production, monitor drift and retrain cadence for both models. Non-linear regressors can degrade silently when feature distributions shift.

Common Pitfalls

  • Using very high polynomial degree and overfitting noise.
  • Training GPR on large datasets without considering computational cost.
  • Comparing models on training score only instead of held-out metrics.
  • Ignoring feature scaling and kernel sensitivity in Gaussian processes.
  • Treating GPR uncertainty output as calibration guarantee without validation.

Summary

  • Polynomial regression and GPR both model non-linear behavior but with different assumptions.
  • Use pipelines for reproducible polynomial feature transformations.
  • Use GPR when uncertainty estimates add business value.
  • Validate with cross-validation, held-out metrics, and visual diagnostics.
  • Select model based on accuracy, uncertainty needs, and operational cost.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.