# Polynomial Regression With Examples

Regression analysis is a statistical technique used to estimate the relationship between a dependent variable to one or more independent variables. And a linear regression model may not provide accurate results. In such cases, polynomial regression can be used.

## 1. What is Polynomial Regression?

This is a type of regression analysis that models the relationship between the independent variable to the dependent variable as an nth-degree polynomial.

Polynomial regression can be used to model a wide range of relationships between the independent variable and the dependent variable. For example, if the relationship is curvilinear, a quadratic or cubic polynomial can be used to model the relationship.

The degree of the polynomial equation depends on the complexity of the relationship between the independent variable and the dependent variable. A higher degree polynomial can better fit the data. But it may also overfit the data and result in poor performance on new data.

Polynomial regression can be performed using the same techniques as linear regression. The coefficients of the polynomial equation can be estimated using the least squares method. Which minimizes the sum of squared errors between the predicted values and the actual values.

### 1.1 The equation for Polynomial regression is:

``````
# Equation for polynomial regression can be represented as:
y = b0 + b1x + b2x^2 + b3x^3 + … + bnx^n
``````

Where `y` is the dependent variable, `x` is the independent variable, and `b0, b1, b2, …, bn` are the coefficients of the polynomial equation. The degree of the polynomial equation, `n`, determines the complexity of the relationship between the independent variable and the dependent variable. The coefficients of the polynomial equation can be estimated using the least squares method, which minimizes the sum of squared errors between the predicted values and the actual values.

## 1.2 Why the name Polynomial Regression?

The name Polynomial Regression comes from the fact that this type of regression analysis models the relationship between the independent variable and the dependent variable as an nth degree polynomial. A polynomial is a mathematical expression that consists of one or more terms, where each term is the product of a constant coefficient, and one or more variables are raised to a non-negative integer power. For example, x^2, 3x, and 4 are all examples of polynomial terms.

In summary, the name Polynomial Regression reflects the fact that this type of regression analysis uses polynomial equations to model the relationship between the independent variable and the dependent variable.

## 2. Linear Regression Vs Polynomial Regression

Both popular techniques are used to model relationships between variables, but they differ in their approach and applicability.

Linear regression models assume a linear relationship between the dependent variable and the independent variable(s). The model is expressed as a linear equation, `y = b0 + b1*x,` where y is the dependent variable, x is the independent variable, `b0` and `b1` are the intercept and slope coefficients, respectively. Linear regression is easy to interpret and can be used to make predictions within the range of the training data. However, it may not be appropriate for datasets with non-linear relationships between variables.

Polynomial regression, on the other hand, extends linear regression by adding higher-order polynomial terms to the model to capture non-linear relationships between variables. The model is expressed as `y = b0 + b1x + b2x^2 + ... + bk*x^k,` where `k` is the degree of the polynomial. Polynomial regression can provide more accurate predictions for non-linear datasets, and it can be more flexible in capturing complex patterns in the data. However, it can be more complex to implement and may overfit the data if the degree of the polynomial is too high.

## 3. Evaluation Metrics for Polynomial Regression?

When performing polynomial regression, there are several evaluation metrics that can be used to assess the accuracy of the model. These metrics provide information about how well the model fits the data and can be used to compare different models or to select the best model for a given problem. Some commonly used evaluation metrics for polynomial regression include:

### 3.1 Mean Squared Error (MSE):

The mean squared error measures the average squared difference between the predicted and actual values. It is calculated as the sum of the squared differences divided by the number of observations. The lower the MSE, the better the model performance.

## 3.2 Root Mean Squared Error (RMSE):

The RMSE is the square root of the MSE and provides a measure of the average deviation of the predictions from the actual values. The lower the RMSE, the better the model performance.

### 3.3 R-squared (R2) Score:

The R-squared score measures the proportion of the variance in the dependent variable that is explained by the independent variable(s) in the model. It ranges from 0 to 1, with higher values indicating better model performance.

The adjusted R-squared score is similar to the R-squared score, but takes into account the number of independent variables in the model. It is adjusted for degrees of freedom and penalizes the model for including unnecessary independent variables.

Let’s take a closer look at each of these evaluation metrics and how they can be calculated in Python using the scikit-learn library:

``````
#Import libraries
import numpy as np
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Generate some sample data
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape(-1, 1)
y = np.array([4, 5, 6, 9, 10, 11, 12, 13, 14, 15]).reshape(-1, 1)

# Fit a polynomial regression model of degree 2
poly = PolynomialFeatures(degree=2)
x_poly = poly.fit_transform(x)
reg = LinearRegression().fit(x_poly, y)

# Make predictions on the test data
y_pred = reg.predict(poly.fit_transform(x))

# Calculate evaluation metrics
mse = mean_squared_error(y, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y, y_pred)
n = len(y)
p = 2 # number of predictors (degree of polynomial + 1)

print("Mean Squared Error (MSE):", mse)
print("Root Mean Squared Error (RMSE):", rmse)
print("R-squared (R2) Score:", r2)
``````

In this example, we generated some sample data and fit a polynomial regression model of degree 2 using scikit-learn. We then made predictions on the test data and calculated the evaluation metrics using the `mean_squared_error`, `r2_score`, and `PolynomialFeatures` functions.

``````
#The output of the code is as follows:

Mean Squared Error (MSE): 0.21013158229031288
Root Mean Squared Error (RMSE): 0.45828318396667815
R-squared (R2) Score: 0.9493669629929204

``````

### 4. Key benefits of Polynomial Regression

Polynomial regression is a useful tool for modeling relationships between variables in a non-linear way. It offers several key benefits, including:

### 4.1 Flexibility:

Polynomial regression allows for greater flexibility in modeling non-linear relationships between variables. By including higher-order polynomial terms, the model can capture more complex patterns in the data.

### 4.2 Improved accuracy:

Polynomial regression can often provide more accurate predictions than linear regression when the relationship between the dependent and independent variables is non-linear. This is because it can capture more complex patterns in the data that may not be captured by a linear model.

### 4.3 Ease of implementation:

Polynomial regression is a straightforward extension of linear regression and can be easily implemented in many statistical software packages. It does not require any specialized knowledge or tools.

### 4.4 Interpretability:

Polynomial regression models are often more interpretable than other non-linear models, such as neural networks or decision trees. The coefficients of the polynomial terms can provide insight into the nature of the relationship between the variables.

### 4.5 Versatility:

Polynomial regression can be applied to a wide range of data types and is not limited to a specific type of data distribution. It can be used for both continuous and categorical independent variables.

Overall, polynomial regression is a powerful tool for modeling non-linear relationships between variables and can provide more accurate predictions than linear regression in many cases. Its flexibility, ease of implementation, and interpretability make it a valuable tool for data analysis and modeling in a variety of fields.

## 5. Applications of the polynomial Regression

Polynomial regression is a widely used technique in various fields for modeling relationships between variables in a non-linear way. Here are some of the common applications of polynomial regression:

### 5.1 Economics:

In economics, polynomial regression can be used to model relationships between economic variables such as income, education, and employment. For example, a polynomial regression model can be used to predict the effect of education level on income.

### 5.2 Engineering:

In engineering, polynomial regression is commonly used for modeling relationships between variables in the physical sciences, such as temperature, pressure, and time. For example, a polynomial regression model can be used to predict the behavior of a material under different environmental conditions.

### 5.3 Medicine:

In medicine, polynomial regression can be used to model relationships between health outcomes and risk factors such as age, gender, and lifestyle. For example, a polynomial regression model can be used to predict the risk of heart disease based on factors such as age, blood pressure, and cholesterol levels.

### 5.4 Marketing:

In marketing, polynomial regression can be used to model relationships between sales and marketing variables such as advertising spending, pricing, and promotion. For example, a polynomial regression model can be used to predict the effect of advertising spending on sales.

### 5.5 Finance:

In finance, polynomial regression can be used to model relationships between financial variables such as stock prices, interest rates, and inflation. For example, a polynomial regression model can be used to predict the behavior of a stock price based on factors such as company earnings and industry trends.

### 5.6 Agriculture:

In agriculture, polynomial regression can be used to model relationships between crop yield and environmental factors such as rainfall, temperature, and soil fertility. For example, a polynomial regression model can be used to predict crop yield based on historical data and environmental factors.

## 6. Challenges and limitations of Polynomial Regression?

While polynomial regression is a useful tool for modeling non-linear relationships between variables, it also has some challenges and limitations. Here are some of the common challenges and limitations of polynomial regression:

## 6.1 Overfitting

One of the main challenges of polynomial regression is overfitting. If the degree of the polynomial is too high, the model may fit the training data too closely and may not generalize well to new data.

## 6.2 Interpretability:

While polynomial regression models can provide insight into the nature of the relationship between variables, they can also be more difficult to interpret than linear models. This is because the coefficients of the polynomial terms may not have a clear intuitive interpretation.

## 6.3 Non-convex optimization:

The optimization problem in polynomial regression can be non-convex, which means that it may have multiple local minima. This can make it difficult to find the global minimum and may require more complex optimization techniques.

## 6.4 Limited extrapolation:

Polynomial regression models are generally limited in their ability to extrapolate beyond the range of the training data. This means that they may not provide accurate predictions for values of the independent variable that are far outside the range of the training data.

## 7. Practical Implementation Code of the Polynomial Regression

Here’s an example of implementing Polynomial Regression in Python using scikit-learn on a house price prediction dataset with output:

``````
# Import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score, mean_squared_error

# Split data into X (features) and y (target variable)
X = data[['sqft_living']]
y = data['price']

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Fit polynomial regression model
poly = PolynomialFeatures(degree=2)
X_poly_train = poly.fit_transform(X_train)
X_poly_test = poly.fit_transform(X_test)
poly_reg = LinearRegression()
poly_reg.fit(X_poly_train, y_train)

# Make predictions on test set
y_pred = poly_reg.predict(X_poly_test)

# Calculate evaluation metrics
r2 = r2_score(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))

# Visualize results
plt.scatter(X_test, y_test, color='blue')
plt.plot(X_test, y_pred, color='red')
plt.title('Polynomial Regression')
plt.xlabel('Living Area (sqft)')
plt.ylabel('Price (\$)')
plt.show()

print('R-squared:', r2)
print('Root Mean Squared Error:', rmse)

``````
``````
#Output

R-squared: 0.5015112699867991
Root Mean Squared Error: 262070.74579156114
``````

In this example, we load a house price prediction dataset, split it into X and y, and then split it into training and testing sets. We use `PolynomialFeatures` from scikit-learn to transform the data by adding polynomial terms up to degree 2. Then, we fit a `LinearRegression` model on the transformed training data and make predictions on the test data. Finally, we evaluate the model using `r2_score` and `mean_squared_error`, and visualize the results using `matplotlib`.

The output shows the R-squared value and the root mean squared error (RMSE) of the model. In this case, the R-squared value is 0.5015, indicating that the model explains 50.15% of the variance in the target variable. The RMSE is 262,070.75, indicating that the average difference between the actual and predicted values is approximately \$262,070.75.

## 8. Frequently Asked Questions On Polynomial Regression

Here are some frequently asked questions on Polynomial Regression:

Q: What is the degree of a polynomial in Polynomial Regression?

A: The degree of a polynomial is the highest power of the independent variable in the polynomial equation. For example, in a polynomial of degree 3, the independent variable is raised to the power of 3.

Q: What is the difference between Simple Linear Regression and Polynomial Regression?

A: Simple Linear Regression is a linear model that assumes a linear relationship between the independent and dependent variables. Polynomial Regression, on the other hand, is a nonlinear model that allows for a more flexible relationship between the independent and dependent variables by fitting a polynomial equation to the data.

Q: What is overfitting in Polynomial Regression?

A: Overfitting occurs when a model fits the training data too closely and captures noise or random fluctuations in the data. Which leads to poor performance on new data. Overfitting can occur when the degree of the polynomial is too high for the amount of data available.

##### Q: What is regularization in Polynomial Regression?

A: Regularization is a technique used to prevent overfitting in Polynomial Regression by adding a penalty term to the cost function of the model. There are two types of regularization commonly used in Polynomial Regression: Ridge Regression and Lasso Regression.

Q: Can Polynomial Regression be used for classification problems?

A: No, Polynomial Regression is a regression technique used for predicting continuous variables. For classification problems, other techniques such as Logistic Regression or Decision Trees are more appropriate.

Q: What is the best degree for a polynomial in Polynomial Regression?

A: The best degree for a polynomial in Polynomial Regression depends on the specific dataset and the problem at hand. It is common to use trial and error or cross-validation to find the best degree for the polynomial.

## Conclusion

In conclusion, Polynomial Regression is a powerful and versatile machine learning technique that allows us to model complex non-linear relationships between input and output variables. By adding polynomial terms to the linear regression model, we can capture non-linear trends and make better predictions on the target variable.