Polynomial Regression

Society of AI
3 min readSep 21, 2020

--

Polynomial Regression is an important method to fit data points in complex non-linear functions that are not possible through linear regression.

Linear and Non-Linear

Let us understand with the help of an example. We will be using Python to obtain some relationship between the data-points and obtain the function that fits it using polynomial regression. Further down the article, we will see the methods required, rather than going about the problem using mathematics.

In the given example, we have considered the cost of 9 toys, in a wholesale market.

First, we import our module, i.e. matplotlib and pyplot function.

Then, we store the values of our input variables in x and y in an array. The number of toys is stored in x and the cost of each toy in the wholesale market is store in y. The functions, “plt.scatter(x,y)”&“plt.show”, are used to map the values of input variables in a graphical format and display it to the user.

In this piece of code, we have imported the required modules i.e. numpy, matplotlib and pyplot funtion. After that, we will insert the values of input variables, in an array ,just like the previous figure.

mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))” is a method used by numpy to implement the polynomial model.

myline = numpy.linspace(1, 9, 100)” : This part of the code is written to imply that the line representing the polynomial function, will commence from x=1 an terminate at x=9.

plt.plot(myline, mymodel(myline))”: This statement of the code is used to plot the line of polynomial regression.

Finally, the plot of the graph, showing the input data and the polynomial regression line, is displayed to the userwhich fits the data quite accurately.

The aim of this piece of code is to check the accuracy of the linear regression model. Similar to the previous pieces of code, we have inputted the required modules i.e. numpy and the fuction “r2_score” to determine accuracy.

The input variables, x and y, have been accepted as two separate arrays.

print(r2_score(y, mymodel(x)))”: This function is used to display the accuracy of the input model or represent the relationship between the polynomial function and our input data as a value between 0 & 1.

The output value shown is 0.94. This clearly shows that there is indeed a very good relationship, as the function fits our data with about 94% accuracy.

If you liked the story and want to appreciate us you can clap as much as you can. Appreciate our work by your constructive comment and also you can connect to us on….

Youtube: https://www.youtube.com/channel/SocietyOFAI

LinkedIn : https://www.linkedin.com/company/society-of-ai

Facebook: https://www.facebook.com/societyofai/

Website : https://www.societyofai.in/

--

--

Society of AI
Society of AI

Written by Society of AI

Society of AI has an vision to educate people how Artificial Intelligence can change their life!