Blueprint to Polynomial Regression

WTF is a polynomial?

polylinear. Adjective. (not comparable) (mathematics) Involving many linear functions.

Alright then,let’s get started!

  • Import required libraries.
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
  • import the data into a dataframe dataset.Slice the data into X and y.

dataset = pd.read_csv('Polynomial_Regression/Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values
  • Import PolynomialFeatures from sklearn.preprocessing module. fit_transform our previously X into variable polyX.
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)

polyX = poly_reg.fit_transform(X)
poly_reg.fit(X_poly, y)
  • Let’s create a LinearRegression and fit it with polyX which we previously created and y.
from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(X_poly, y)
  • Predict!
reg.predict(poly_reg.fit_transform(6.5))

 

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('/home/z/Documents/Polynomial_Regression/Polynomial_Regression/Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)

X_poly = poly_reg.fit_transform(X)
poly_reg.fit(X_poly, y)

from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(X_poly, y)

reg.predict(poly_reg.fit_transform(6.5))

 

CategoriesML

Leave a Reply

avatar
  Subscribe  
Notify of