If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Polynomial Linear Regression is an extension of simple linear regression, where the relationship between the independent variable(s) and the dependent variable is modeled as a higher-degree polynomial. It allows us to capture more complex patterns and non-linear relationships between the variables. Let's perform Polynomial Linear Regression using scikit-learn:
pythonCopy code
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
# Example data
x = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) # Reshape to a column vector
y = np.array([2, 3, 4, 2, 3])
# Transform the features into polynomial features
poly_transformer = PolynomialFeatures(degree=3) # Choose the degree of the polynomial
x_poly = poly_transformer.fit_transform(x)
# Create and fit the polynomial regression model
model = LinearRegression()
model.fit(x_poly, y)
# Predict using the model
x_new = np.array([6, 7, 8]).reshape(-1, 1)
x_new_poly = poly_transformer.transform(x_new)
predictions = model.predict(x_new_poly)
# Visualize the Polynomial Regression Model
plt.scatter(x, y, color='blue', label='Data Points')
plt.plot(x, model.predict(x_poly), color='red', label='Polynomial Model')
plt.scatter(x_new, predictions, color='green', label='Predictions')
plt.xlabel('Independent Variable (x)')
plt.ylabel('Dependent Variable (y)')
plt.title('Polynomial Linear Regression using scikit-learn')
plt.legend()
plt.show()
print("Predictions on new data:", predictions)
In this implementation, we used scikit-learn's PolynomialFeatures
class to transform the original feature x
into polynomial features of a chosen degree (in this case, degree=3). We then used LinearRegression
as before to create and train the polynomial regression model.
We imported the necessary libraries, including numpy
, matplotlib.pyplot
, LinearRegression
, and PolynomialFeatures
.
We defined the example data x
and y
.
We transformed the original features x
into polynomial features of degree 3 using PolynomialFeatures
. This transforms the feature x
into [x, x^2, x^3]
.
We created an instance of LinearRegression
as model
and fit it with the transformed data x_poly
and the target variable y
.
We then made predictions on new data x_new
using the model. To make predictions on polynomial features, we also transformed x_new
using poly_transformer.transform(x_new)
.
Finally, we used matplotlib
to visualize the polynomial regression model's line of best fit, data points, and predictions.
Polynomial Linear Regression allows us to capture more complex relationships between the variables, making it useful for modeling non-linear patterns in the data. The degree of the polynomial is a hyperparameter that needs to be chosen based on the data and the complexity of the relationship.
Comments: 0