Polynomial regression

Polynomial regression is a type of regression analysis in which the relationship between the independent variable x and the dependent variable y is modeled as an nth-degree polynomial.  This means that the relationship is not a straight line, but instead a curve. Polynomial regression is a useful tool for modeling relationships that are not linear, but it is important to note that it can be overfitted easily.

To perform polynomial regression with sklearn, you will need to use the PolynomialFeatures and LinearRegression modules. The PolynomialFeatures module will create new features by raising the original features to a power. The LinearRegression module will then fit a linear regression model to these new features.

Here is an example of how to perform polynomial regression with sklearn:

Import the necessary libraries:

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

2. Load the data:

data = pd.read_csv('data.csv')

3- Separate the features and target variable:

X = data[['feature1', 'feature2']]
y = data['target_variable']

4. Split the data into training and testing sets:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

5. Create a polynomial features object

poly = PolynomialFeatures(degree=2)
X_train_poly = poly.fit_transform(X_train)
X_test_poly = poly.transform(X_test)

6. Create a linear regression object:

model = LinearRegression()

7-Fit the model to the training data

model.fit(X_train_poly, y_train)

8-Make predictions on the test data

y_pred = model.predict(X_test_poly)

Evaluate the model's performance

mse = mean_squared_error(y_test, y_pred)
print('MSE:', mse)

References

1-Polynomial regression

Last modified: Tuesday, 21 November 2023, 4:11 PM