Skip to content
Related Articles

Related Articles

Polynomial Regression using Turicreate
  • Difficulty Level : Medium
  • Last Updated : 24 Jan, 2021

In this article, we will discuss the implementation of Polynomial Regression using Turicreate. Polynomial Regression: Polynomial regression is a form of regression analysis that models the relationship between a dependent say y and an independent variable say x as a nth degree polynomial. It is expressed as : 

y= b0+b1x1+ b2x12+ b2x13+…… bnx1n

[where b0, b1, b2, …… bn are regression coefficients]

So let’s learn this concept through practicals.

Step 1: Import the important libraries and generate a very small data set using SArray and SFrame in turicreate that we are going to use to perform Polynomial Regression.


# importing required python libraries
import turicreate
import matplotlib.pyplot as plt
import random
# Generating datapoints
X = [data for data in range(1, 21)]
Y = [random.randrange(100, 1000, 1) for data in range(20)]
# Creating Sarrays from the generated data points
Xs = turicreate.SArray(X, dtype=float)
Ys = turicreate.SArray(Y, dtype=float)
print(f"""Xs : {Xs}
Ys : {Ys}""")


Step 2: Plotting the generated data


# plotting the generated data
plt.scatter(Xs, Ys)

Step 3: Create an SFrame containing the input, its polynomial_degrees, and the output in order to fit our regression model.


# Creating an Sframe where all the inputs and the polynomial degree and output
def createSframe(inputs, pol_degree):
    datapoints = turicreate.SFrame({'x1': inputs})
    for degree in range(2, pol_degree+1):
        datapoints[f'x{degree}'] = datapoints[f'x{degree-1}']*datapoints['x1']
    return datapoints
# Creating a SFrame with polynomial degree 20
data_points = createSframe(Xs, 20)
data_points['y'] = Ys
# showing the first 10 entries in the SFrame

Step 4: Fitting Polynomial Regression to the generated Data set.


# Polynomial Regression
features = [f'x{i}' for i in range(1, 21)]
poly_model = turicreate.linear_regression.create(
    data_points, features=features, target='y')

Step 5: Predicting the result using the fitted model and storing the result in the SFrame.


# predicting the some data
# Generating test datapoints
test_X = [random.randrange(1, 60, 1) for data in range(20)]
test_Xs = turicreate.SArray(X, dtype=float)
test_data = createSframe(test_Xs, 5)
data_points['predicted_y'] = poly_model.predict(test_data)

Step 6: Measuring the accuracy of our predicted result


# Measuring the accuracy
# Generating test datapoints
test_X = [random.randrange(1, 60, 1) for data in range(20)]
test_Xs = turicreate.SArray(X, dtype=float)
test_data = createSframe(test_Xs, 20)

Step 7: Visualizing the Polynomial Regression results using scatter plot and line plot of the input data and the predicted result.


plt.scatter(data_points['x1'], data_points['y'])
plt.plot(data_points['x1'], data_points['predicted_y'])

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :