Polynomial Regression in Python – Step 3.) Training the Polynomial Regression Model

with No Comments

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

# Import the CSV Data
dataset = pd.read_csv(‘Poly_Data.csv’)
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

# Split the dataset into Training set and Test set
from sklearn.cross_validation import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(X, y, test_size = 0.2)

# Training Polynomial Regression Model
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(xtrain)
poly_reg.fit(X_poly, ytrain)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, ytrain)

Leave a Reply