Polynomial Curve Fitting in Machine Learning

Nirmalya Misra
Jan 30 · 5 min read
Image taken from Unsplash

In this article, we will attempt Polynomial Curve Fitting. The GitHub repository for the same is given at the end of the article and all the code required is included in this article as well. Polynomial curve fitting, here, is done from scratch in Python.

INTRODUCTION

import numpy as np 
import matplotlib.pyplot as plt
import math
import time

CREATING THE DATASET

Creating the Dataset
The Dataset displayed

The above image shows the dataset. The blue points are basically the data-points. The green curve represents sin(x). To display this, we can plot sin(x) against x (that part hasn’t been shown in the article, but is there in the GitHub link given at the end). As we discussed, sin(x) is the benchmark for this problem.

THEORY

Mathematics behind the problem

Basically, this is the theory. Now, we have to find the optimal values of w for given x. That will help us to find the best fit curve. We will talk about the optimization of the w vector later, first we need some helper functions.

HELPER FUNCTIONS

Below is the code for the helper functions:

Helper Functions

OPTIMIZATION

I have chosen the learning rate to be 1e-6 or 10 raised to the power of -6. The process will run for 100000 epochs. l is the final loss and yhat corresponds to the predictions. Now, we have an optimal set of values for w. So, let us check our predictions.

CHECKING THE PREDICTIONS

Our Predictions

Now, let us analyze our curve with respect to the sine curve. Yes, it doesn’t look much like a sine curve but it has generalized well. If we compare data-point for data-point, we will find that the error is actually very less. The curve has some behavior like a sine curve as well, only that the crests and troughs are much smaller. It would have performed better if: either we would have had more data or taken a higher degree polynomial.

GITHUB LINK

The same problem has been solved using the Julia language can be found here done by Deeptendu Santra.

Thank You for reading this article. Hope it helped you in getting an idea of Polynomial Curve Fitting.

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

TheLeanProgrammer

Never stop exploring!

Sign up for The Lean Letters

By TheLeanProgrammer

Biweekly knowledge sharing letters Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

TheLeanProgrammer

The biggest power in the world is to be able to give life to something, and guess what? Code gives you this ability! Here in this publication, we build stuff, we share knowledge in tech, and share our stories, feel free to join — https://theleanprogrammer.com/writer-request/

Nirmalya Misra

Written by

A Machine Learning Enthusiast and a Football freak.

TheLeanProgrammer

The biggest power in the world is to be able to give life to something, and guess what? Code gives you this ability! Here in this publication, we build stuff, we share knowledge in tech, and share our stories, feel free to join — https://theleanprogrammer.com/writer-request/

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store