D Dev Duniya
Back to Journal
2 min read

Polynomial Regression with Example Explained in Details | DevDuniya

Ashish Yadav

Ashish Yadav

Published on Dev Duniya
Keep exploring
Compare the details before you buy.

While Simple and Multiple Linear Regression models assume a linear relationship between the variables, Polynomial Regression allows for more complex relationships. It introduces polynomial terms (such as squared or cubed terms) of the independent variable(s) into the regression equation.

Key Concepts

  • Non-Linear Relationships: Polynomial Regression can effectively model non-linear relationships between the variables.
  • Polynomial Terms: By adding polynomial terms (e.g., x², x³, x⁴) to the regression equation, the model can capture curves and more complex patterns.
  • Flexibility: Polynomial Regression offers greater flexibility in fitting the data compared to simple linear regression.

Mathematical Representation (Simple Case)

The equation for a simple polynomial regression with a degree of 2 (quadratic):

y = b0 + b1x + b2x²

where:

  • y: The predicted value of the dependent variable.
  • x: The value of the independent variable.
  • b0, b1, b2: The coefficients to be determined.

Example: Predicting Sales with Advertising Spend (Quadratic)

Let's revisit the sales prediction example, but this time, we suspect a non-linear relationship between advertising spend and sales.

Import necessary libraries:

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

Prepare the data:

X = np.array([[1], [2], [3], [4], [5]]).reshape(-1, 1)  # Independent variable (advertising spend)
y = np.array([10, 15, 18, 20, 25])  # Dependent variable (sales)

Create polynomial features:

polynomial_features = PolynomialFeatures(degree=2) 
X_poly = polynomial_features.fit_transform(X)

Create and train the model:

model = LinearRegression()
model.fit(X_poly, y)

Make predictions:

new_advertising_spend = np.array([[6]])
new_advertising_spend_poly = polynomial_features.transform(new_advertising_spend)
predicted_sales = model.predict(new_advertising_spend_poly)
print("Predicted Sales:", predicted_sales)

This code creates polynomial features (x and x²) and then uses the LinearRegression model to fit the data.

Applications of Polynomial Regression

  • Modeling non-linear relationships in various fields
  • Predicting stock prices with more complex patterns
  • Analyzing growth curves
  • Image and signal processing

Limitations

  • Can overfit the data easily, especially with high degrees.
  • May not always provide meaningful interpretations of the coefficients.
  • Requires careful selection of the degree of the polynomial to avoid overfitting.

Polynomial Regression extends the capabilities of Linear Regression by allowing for more flexible and complex relationships between variables. By incorporating polynomial terms, it can effectively model curves and capture non-linear patterns in the data. However, it's crucial to carefully choose the degree of the polynomial to avoid overfitting and ensure a meaningful and interpretable model.

Keep reading

12 latest articles

The newest stories from Dev Duniya, excluding the article you are reading.

All articles →

From the product catalog

Explore device specifications

Specification cards update automatically from published catalog products, grouped by brand.

Apple specifications

Explore Apple devices →
Apple Apple iPhone Duo
Apple Apple iPhone Duo specifications

Apple iPhone Duo is a new-generation iPhone model focused on a sleek design, powerful performance, and everyday usability. Explore iPhone Duo specifications, features, display, cameras, battery, and expected price in the USA. Get the latest information about Apple iPhone Duo and its key features. Compare iPhone Duo with other popular Apple iPhone models.

Size (inches)
Open: 4.64 x 6.48 x 0.21 Closed: 4.64 x 3.31 x 0.44
Weight (ounces)
8.96
Display size (inches)
Inner: 7.6-inch (diagonal) all-screen OLED folding display Outer: 5.4-inch (diagonal) all-screen OLED display
Resolution (pixels)
Inner: 1878-by-2670-pixel resolution at 430 ppi Outer: 1398-by-2034-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro Max
Apple Apple iPhone 18 Pro Max specifications

Apple iPhone 18 Pro Max is a high-end flagship smartphone designed for powerful performance and advanced photography. Explore iPhone 18 Pro Max specifications, features, display, cameras, battery, and expected price in the USA. Discover the latest upgrades and key features of Apple’s largest Pro iPhone. Compare iPhone 18 Pro Max with other premium flagship smartphones.

Size (inches)
6.43 x 3.07 x 0.34
Weight (ounces)
8.78
Display size (inches)
6.9-inch (diagonal) all-screen OLED display
Resolution (pixels)
2868-by-1320-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro
Apple Apple iPhone 18 Pro specifications

Apple iPhone 18 Pro is a premium flagship smartphone with advanced performance and an upgraded camera system. Explore iPhone 18 Pro specifications, features, display, cameras, battery, and expected price in the USA. Get the latest details about Apple’s new Pro model and its key upgrades. Compare iPhone 18 Pro features with other flagship smartphones.

Size (inches)
5.91 x 2.83 x 0.34
Weight (ounces)
7.44
Display size (inches)
6.3-inch (diagonal) all-screen OLED display
Resolution (pixels)
2622-by-1206-pixel resolution at 460 ppi
See all specifications →

Google specifications

Explore Google devices →

Samsung specifications

Explore Samsung devices →