Josh Josh - 2 months ago 43
Python Question

RBF interpolation: LinAlgError: singular matrix

The following call:

rbf = Rbf(points[0], points[1], values,epsilon=2)


results in an error:

LinAlgError: singular matrix


with the following values:

In [3]: points
Out[3]:
(array([71, 50, 48, 84, 71, 74, 89, 76, 70, 77, 74, 79, 83, 71, 72, 78, 73,
84, 75, 65, 73, 82, 48, 86, 74, 86, 66, 74, 68, 74, 81, 74, 88, 66,
57, 50, 72, 86, 72, 92, 81, 67, 82, 78, 69, 70, 73, 71, 76, 72, 74,
75]),
array([32, 34, 4, 35, 1, 7, 47, 16, 37, 14, 65, 18, 32, 4, 3, 27, 25,
34, 18, 25, 6, 25, 34, 41, 16, 35, 44, 2, 32, 2, 37, 60, 45, 32,
33, 42, 54, 31, 18, 38, 24, 18, 45, 48, 9, 63, 56, 45, 9, 59, 5,
12]))

In [4]: values
Out[4]:
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])


What can I do to avoid it and still solve the interpolation problem?

Answer

I think what you're trying to do is kernel density estimation. You can use scipy.stats.gaussian_kde for this:

import numpy as np
from scipy.stats import gaussian_kde
from matplotlib import pyplot as pp

# kernel density estimate of the PDF
kde = gaussian_kde(points)

# evaluate the estimated PDF on a grid
x,y = np.mgrid[40:101,-20:101]
z = kde((x.ravel(),y.ravel())).reshape(*x.shape)

# plot
fig,ax = pp.subplots(1,1)
ax.hold(True)
pc = ax.pcolor(x,y,z)
cb = pp.colorbar(pc)
cb.ax.set_ylabel('Probability density')
ax.plot(points[0],points[1],'o',mfc='w',mec='k')

pp.show()

enter image description here

The statsmodels module also has some more elaborate tools for kernel density estimation.