Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Building Polynomial Regression | Polynomial Regression
Linear Regression with Python

book
Building Polynomial Regression

Loading file

For this chapter, we have a file named poly.csv. Let's load the file and look at the contents.

import pandas as pd

file_link = 'https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/poly.csv'
df = pd.read_csv(file_link)

print(df.head(5))
123456
import pandas as pd file_link = 'https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/poly.csv' df = pd.read_csv(file_link) print(df.head(5))
copy

So here we have one feature and the target. Let's build a scatter plot.

import pandas as pd
import matplotlib.pyplot as plt

file_link = 'https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/poly.csv'
df = pd.read_csv(file_link)
X = df['Feature']
y = df['Target']
plt.scatter(X,y)
plt.show()
123456789
import pandas as pd import matplotlib.pyplot as plt file_link = 'https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/poly.csv' df = pd.read_csv(file_link) X = df['Feature'] y = df['Target'] plt.scatter(X,y) plt.show()
copy

It is hard to imagine a straight line fitting this data well. So let's build a Polynomial Regression!

Building X̃ matrix

We will once again use the OLS class. Still, we need to create an matrix. We do it manually by adding a squared Feature column to the DataFrame like this:

python
df['Feature_squared'] = df['Feature'] ** 2

But if we want to build a high-degree polynomial regression, that will require adding a lot of columns like this. Luckily Scikit-Learn provides a way to do it less painfully using the PolynomialFeatures class.

fit_transform(X) method expects X to be either 2-d array or pandas DataFrame. If your X is an 1-d numpy array, reshape(-1,1) method will transform it to a 2-d array with the same contents:

python
X = X.reshape(-1,1)

If your X is a column from DataFrame, you can use X = df[['col1']] to get a DataFrame instead of pandas Series, which is not suited for fit_transform():

python
X = df['Feature'] # X is a pandas Series
X = df[['Feature']] # X is a pandas DataFrame

So to build an for the Polynomial Regression of degree n, we would use:

python
from sklearn.preprocessing import PolynomialFeatures # Import the class
poly = PolynomialFeatures(n) # Initialize a PolynomialFeatures object
X_tilde = poly.fit_transform(X)

Note

The PolynomialFeatures class also adds a column with 1s, so you do not need to use sm.add_constant().

Building the Polynomial Regression and making the predictions

Knowing how to get an , we are ready to build the Polynomial Regression the same way as the prior models:

python
y = df['Target']
# Prepare X_tilde
X = df[['Feature']]
X_tilde = PolynomialFeatures(n).fit_transform(X)
# Initialize the OLS object and train it
regression_model = sm.OLS(y, X_tilde).fit()

For predicting new values, X_new should be transformed using PolynomialFeatures too.

python
X_new_tilde = PolynomialFeatures(n).fit_transform(X_new)
y_pred = regression_model.predict(X_new_tilde)

The following runnable example shows the entire process of building polynomial regression. X_new here is a 1-d array of points between -0.1 and 1.5. They are needed for visualization. And since it is a 1-d array, we should apply reshape(-1,1) method before using it in the PolynomialFeatures class.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
from sklearn.preprocessing import PolynomialFeatures # Import PolynomialFeatures class

file_link = 'https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/poly.csv'
df = pd.read_csv(file_link)
n = 2 # A degree of the polynomial regression
X = df[['Feature']] # Assign X as a DataFrame
y = df['Target'] # Assign y
X_tilde = PolynomialFeatures(n).fit_transform(X) # Get X_tilde
regression_model = sm.OLS(y, X_tilde).fit() # Initialize and train the model
X_new = np.linspace(-0.1, 1.5, 80).reshape(-1,1) # 2-d array of new feature values
X_new_tilde = PolynomialFeatures(n).fit_transform(X_new) # Transform X_new for predict() method
y_pred = regression_model.predict(X_new_tilde)
plt.scatter(X, y) # Build a scatterplot
plt.plot(X_new, y_pred) # Build a Polynomial Regression graph
plt.show()
12345678910111213141516171819
import pandas as pd import numpy as np import matplotlib.pyplot as plt import statsmodels.api as sm from sklearn.preprocessing import PolynomialFeatures # Import PolynomialFeatures class file_link = 'https://codefinity-content-media.s3.eu-west-1.amazonaws.com/b22d1166-efda-45e8-979e-6c3ecfc566fc/poly.csv' df = pd.read_csv(file_link) n = 2 # A degree of the polynomial regression X = df[['Feature']] # Assign X as a DataFrame y = df['Target'] # Assign y X_tilde = PolynomialFeatures(n).fit_transform(X) # Get X_tilde regression_model = sm.OLS(y, X_tilde).fit() # Initialize and train the model X_new = np.linspace(-0.1, 1.5, 80).reshape(-1,1) # 2-d array of new feature values X_new_tilde = PolynomialFeatures(n).fit_transform(X_new) # Transform X_new for predict() method y_pred = regression_model.predict(X_new_tilde) plt.scatter(X, y) # Build a scatterplot plt.plot(X_new, y_pred) # Build a Polynomial Regression graph plt.show()
copy

Feel free to play with the values of n in the eighth line. You will see how the plot changes depending on the polynomial regression's degree. If you pay attention, you may notice how different the predictions are for feature values lower than 0, or greater than 1.4. That is the subject of the next chapter.

question mark

Consider the following code. In which case will the code run without errors?

X_tilde = PolynomialFeatures(2).fit_transform(X)

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

We use cookies to make your experience better!
some-alt