What is a Polynomial Expression?
A polynomial expression (or simply a polynomial) is a mathematical expression made up of variables (like (x)) and constants (coefficients), using only addition, subtraction, multiplication, and non-negative integer exponents.It's a sum of terms like , where (a) is the coefficient and (n) is a non-negative integer.General form (in one variable):
Linear: 3x + 2 (degree 1) Quadratic: x² - 4x + 7 (degree 2) Cubic: x³ + 2x² - x + 1 (degree 3)
a x^nP(x) = a_n x^n + a_{n-1} x^{n-1} + ⋯ + a_2 x^2 + a_1 x + a_0
Linear Regression vs. Polynomial Regression: A Quick Guide
Linear Regression models the relationship between variables with a straight line. It finds the best-fitting line (y = mx + b) by minimizing the sum of squared errors. Perfect for data with a constant rate of change, like predicting house prices from size.
Polynomial Regression extends linear regression by fitting a curved line using higher-degree terms (e.g., y = ax² + bx + c for quadratic). It captures non-linear patterns, like accelerating growth or peaks/dips in data.
Key Difference: Use linear for straight trends; switch to polynomial when data curves (e.g., temperature vs. sales). Watch out—high degrees can overfit!
Python Code from Grok
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.metrics import r2_score
# Data: Temperature vs Ice Cream Sales
temp = np.array([10, 15, 20, 25, 30, 35, 40]).reshape(-1, 1)
sales = np.array([5, 20, 50, 80, 100, 90, 60])
# Generate dense points for smooth plotting
temp_smooth = np.linspace(10, 40, 300).reshape(-1, 1)
# Linear Regression
lin_model = LinearRegression()
lin_model.fit(temp, sales)
sales_lin_smooth = lin_model.predict(temp_smooth)
# Polynomial Regression (Degree 3)
poly_model = make_pipeline(PolynomialFeatures(3), LinearRegression())
poly_model.fit(temp, sales)
sales_poly_smooth = poly_model.predict(temp_smooth)
# Side-by-side smooth plots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
# Left: Linear
ax1.scatter(temp, sales, color='blue', s=100, label='Actual Sales')
ax1.plot(temp_smooth, sales_lin_smooth, color='green', linewidth=3, label='Linear Fit')
ax1.set_title('Linear Regression')
ax1.set_xlabel('Temperature (°C)')
ax1.set_ylabel('Sales (units)')
ax1.grid(True)
ax1.legend()
# Right: Polynomial
ax2.scatter(temp, sales, color='blue', s=100, label='Actual Sales')
ax2.plot(temp_smooth, sales_poly_smooth, color='red', linewidth=3, label='Polynomial Fit (Degree 3)')
ax2.set_title('Polynomial Regression')
ax2.set_xlabel('Temperature (°C)')
ax2.set_ylabel('Sales (units)')
ax2.grid(True)
ax2.legend()
# Updated overall title as requested
fig.suptitle('Linear vs Polynomial Regression: Ice Cream Sales vs Temperature', fontsize=16)
plt.tight_layout()
plt.show()
No comments:
Post a Comment