Open In App

Polynomial Interpolation Using Sklearn

Improve
Improve
Like Article
Like
Save
Share
Report

Newton’s polynomial interpolation is a way to fit exactly for a set of data points which we also call curve fitting. Newton’s polynomial is also known as Newton’s divided differences interpolation polynomial because the coefficients of the polynomial are calculated using Newton’s divided differences method. Divided differences (an algorithm used for computing tables of logarithmic and trigonometric functions). So, Newton polynomial interpolation is a recursive division process for Given a sequence of data points, the method calculates the coefficients of the interpolation polynomial of these points in the Newton form. Practically we do it using the previous two elements (to the left) we can calculate each element in the table. 

There is a divided differences table that summarizes the whole process of finding these coefficients. Here’s an example using five data points :
 

Interpolation

Now after understanding the basics of newton interpolation, let’s code it with python. We will be using Hindalco stock data here to go further with this technique. We will just have a look at the dataset and move to the main problem, you try it with the data set you have.

We will need ridge ( module used to solve a regression model where the loss function is the linear least squares function and regularization is L2), PolynomialFeature (Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree ) and make_pipeline ( This is a shorthand for the Pipeline constructor ) for meeting the required result.

Here is the code for polynomial Interpolation using python pandas, Numpy and Sklearn.

Importing Libraries and the Dataset

Python libraries make it very easy for us to handle the data and perform typical and complex tasks with a single line of code.

  • Pandas – This library helps to load the data frame in a 2D array format and has multiple functions to perform analysis tasks in one go.
  • Numpy – Numpy arrays are very fast and can perform large computations in a very short time.
  • Sklearn – This module contains multiple libraries having pre-implemented functions to perform tasks from data preprocessing to model development and evaluation.

Python3




#import all the required files
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
  
#sklearn import
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline


Now let’s load the dataset into the panda’s data frame. You can download the dataset from here.

Python3




df = pd.read_excel('HINDALCO_1D.xlsx')
df.head()


Output:

 

Python3




plt.figure(figsize=(10, 8))
df['close'].plot.line()
  
X = np.array(range(len(df['close'].index))).reshape(-1, 1)
y = df['close']
models = []
for degree in range(3):
    model = make_pipeline(PolynomialFeatures(degree),
                          Ridge(alpha=0.001))
    model.fit(X, y)
    models.append(model)
    y_pred = model.predict(X)
    plt.plot(X, y_pred,
             linewidth=2,
             label='degree %d' % degree)
      
plt.legend(loc='upper left')
plt.scatter(X, y,
            s=20, marker='o',
            label='training points')
  
plt.show()


Output:

 



Last Updated : 21 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads