Interpolating functions on your data using Python [PART 2]

Hey scientist! How is it going?
Today we’ll talk more about interpolation. We’ll use radial basis functions, tougher tools for a more civilized age. Let’s do this!

In order to use radial basis functions on SciPy we’ll use Rbf, a function within interpolate.rbf. Just like interp1d, Rbf generates a function. It receives an argument, ‘function’, which specifies the function used on the interpolation. The default function is ‘multiquadric’.

The valid arguments are ‘multiquadric’, ‘inverse’, ‘gaussian’, ‘linear’, ‘cubic’, ‘quintic’ and ‘thin_plate’.
The basis functions are:

  • ‘multiquadric’: sqrt((r/self.epsilon)**2 + 1)
  • ‘inverse’: 1.0/sqrt((r/self.epsilon)**2 + 1)
  • ‘gaussian’: exp(-(r/self.epsilon)**2)
  • ‘linear’: r
  • ‘cubic’: r**3
  • ‘quintic’: r**5
  • ‘thin_plate’: r**2 * log(r)

For more info on Rbf, check the SciPy documentation:

from scipy.interpolate.rbf import Rbf  # radial basis functions
from urllib.request import urlopen

import matplotlib.pyplot as plt
import numpy as np

url = ''
yahoo_csv = urlopen(url)
data_yahoo = np.genfromtxt(yahoo_csv, delimiter=',', dtype=None)
data_yahoo = data_yahoo[1:,1:]

On the previous post, the data are decreasing
according to time (from 2015 to the first year).
Let's invert them.

data_yahoo = np.flipud(data_yahoo)
adj_close = np.array(data_yahoo[:, 5:].flatten(), dtype=float)  # "Squeeze" the vector into 1 dimension
last = np.shape(adj_close)[0]

First we import the necessary packages and the Rbf function. We also obtain the dataset and get rid of the first row and column, as we saw on the previous post. After these steps, we invert the dataset, so the data will be increasing over time.

adj_x = np.linspace(0, last, last, endpoint=True)  # Creating X axis with days.
rbf_adj = Rbf(adj_x, adj_close)

Here we create the X axis to support the function. Note that applying Rbf is easy: we provide only the values of X and Y axes. Y are the interest points. We didn’t specify the ‘function’ parameter, so Rbf will use the multiquadric function.

# Getting the first 60 days. Creating also the X axis
adj_bimes = adj_close[:60]  
x_axis = np.linspace(0, 60, 500)

# Interpolating the result between the 60 days.

# Plotting the original points.
plot3 = plt.plot(adj_bimes, 'ko', markersize=12)  # the original points.

We defined an interval to check the interpolation quality (as we did on this post). This time we took an interval of 60 days. After that we create the X axis to support this interval and plotted the original points.

# Interpolating.
# function = 'linear':
rbf_adj = Rbf(adj_x, adj_close, function='linear')
lin_bimes = rbf_adj(x_axis)
plot3 = plt.plot(x_axis, lin_bimes, '-', label='lin')

Now we use Rbf. The ‘function’ argument which we use here is ‘linear’. We obtain the fit using the function rbf_adj, generated by Rbf. After that, we plot the result.

Note that we provided the label argument inside plot. With this argument, we can inform the label of a function on the plot legend.

Let’s try another function!

# function = 'cubic':
rbf_adj = Rbf(adj_x, adj_close, function='cubic')
cub_bimes = rbf_adj(x_axis)
plot3 = plt.plot(x_axis, cub_bimes, '-', label='cubic')

This time we use ‘cubic’ as the ‘function’ argument. We obtain the function rbf_adj and the fit cub_bimes. After that we plot the result.

leg = plt.legend()

Finally, we put a legend on it and plot. As we said, it uses the arguments on label to label each plot.
This is the resulting plot:


This is it scientist! Today we saw how to use Rbf, another function to interpolate data. We saw also how to label functions and to put legends on a plot using them. Test Rbf using another parameters, and also another areas from the plot! Modify the colors as you like too. Please share your modifications with us!

Thanks scientist! Gigaregards!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s