Book Image

Learning NumPy Array

By : Ivan Idris
Book Image

Learning NumPy Array

By: Ivan Idris

Overview of this book

Table of Contents (14 chapters)
Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Interpolation


Interpolation predicts values within a range based on observations. For instance, we could have a relationship between two variables x and y and we have a set of observed x-y pairs. In this scenario, we could try to predict the y value given a range of x values. This range will start at the lowest x value already observed and end at the highest x value already observed. The scipy.interpolate function interpolates a function based on experimental data. The interp1d class can create a linear or cubic interpolation function. By default, a linear interpolation function is constructed, but if the kind parameter is set, a cubic interpolation function is created instead. The interp2d class works in the same way but is two dimensional.

We will create data points using a sinc function and then add some random noise to it. After that, we will do a linear and cubic interpolation and plot the results as follows:

  1. Create the data points and add noise as follows:

    x = np.linspace(-18, 18, 36...