<jupyter_start><jupyter_text>Using radial basis functions for smoothing/interpolation========================================================Radial basis functions can be used for smoothing/interpolating scattereddata in n-dimensions, but should be used with caution for extrapolationoutside of the observed data range.1d example----------This example compares the usage of the Rbf and UnivariateSpline classesfrom the scipy.interpolate module.<jupyter_code>import numpy as np
from scipy.interpolate import Rbf, InterpolatedUnivariateSpline

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

# setup data
x = np.linspace(0, 10, 9)
y = np.sin(x)
xi = np.linspace(0, 10, 101)

# use fitpack2 method
ius = InterpolatedUnivariateSpline(x, y)
yi = ius(xi)

plt.subplot(2, 1, 1)
plt.plot(x, y, 'bo')
plt.plot(xi, yi, 'g')
plt.plot(xi, np.sin(xi), 'r')
plt.title('Interpolation using univariate spline')

# use RBF method
rbf = Rbf(x, y)
fi = rbf(xi)

plt.subplot(2, 1, 2)
plt.plot(x, y, 'bo')
plt.plot(xi, fi, 'g')
plt.plot(xi, np.sin(xi), 'r')
plt.title('Interpolation using RBF - multiquadrics')
plt.tight_layout()
plt.savefig('rbf1d.png')<jupyter_output><empty_output><jupyter_text>![](files/attachments/RadialBasisFunctions/rbf1dnew.png)2d example==========This example shows how to interpolate scattered 2d data.<jupyter_code>import numpy as np
from scipy.interpolate import Rbf

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import cm

# 2-d tests - setup scattered data
x = np.random.rand(100)*4.0-2.0
y = np.random.rand(100)*4.0-2.0
z = x*np.exp(-x**2-y**2)
ti = np.linspace(-2.0, 2.0, 100)
XI, YI = np.meshgrid(ti, ti)

# use RBF
rbf = Rbf(x, y, z, epsilon=2)
ZI = rbf(XI, YI)

# plot the result
n = plt.normalize(-2., 2.)
plt.subplot(1, 1, 1)
plt.pcolor(XI, YI, ZI, cmap=cm.jet)
plt.scatter(x, y, 100, z, cmap=cm.jet)
plt.title('RBF interpolation - multiquadrics')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.colorbar()
plt.savefig('rbf2d.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17297297297297298, 'fraction_numerical': 0.029484029484029485, 'mean_word_length': 5.342679127725857, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '99661', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 867, 'n_tokens_neox': 792, 'n_words': 203, 'path': 'ipython/RadialBasisFunctions.ipynb', 'repo_name': 'gvvynplaine/scipy-cookbook', 'size': '2035'}