RBF interpolation: LinAlgError: singular matrix

2024/9/20 19:37:46

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.

https://en.xdnf.cn/q/72134.html

Related Q&A

What does `\x1b(B` do?

Im a Blessed user, and recently, when I tried to find out the contents of the term.bold() function, I got this output: \x1b[1m\x1b(B\x1b[mI understand what \x1b[1m and \x1b[m do, but what does \x1b(B d…

Not clicking all tabs and not looping once issues

I am trying to click the tabs on the webpage as seen below. Unfortunately, it only seems to click some of the tabs despite correct correct xpath in inspect Chrome. I can only assume it’s not clickin…

Opencv stream from a camera connected to a remote machine

I am developing a wx application in python for streaming and displaying video from two different webcams. This works fine, but now I need to do this in a different scenario in which the two cameras are…

Is there a callable equivalent to f-string syntax?

Everybody loves Python 3.6s new f-strings:In [33]: foo = {blah: bang}In [34]: bar = blahIn [35]: f{foo[bar]} Out[35]: bangHowever, while functionally very similar, they dont have the exact same semanti…

Python: list comprehension based on previous value? [duplicate]

This question already has answers here:Python list comprehension - access last created element(9 answers)Closed 10 months ago.Say I want to create a list using list comprehension like:l = [100., 50., 2…

How to run a coroutine inside a context?

In the Python docs about Context Vars a Context::run method is described to enable executing a callable inside a context so changes that the callable perform to the context are contained inside the cop…

Random Forest interpretation in scikit-learn

I am using scikit-learns Random Forest Regressor to fit a random forest regressor on a dataset. Is it possible to interpret the output in a format where I can then implement the model fit without using…

Why are three apostrophes needed for print in Python?

Im making this Pythagoras Theorem Calculator in Python 3.3.2.I made print over several lines so that I could make a diagram:print("Welcome to the Pythagoras Theorem Calculator, powered by Python!&…

Downloading files from public Google Drive in python: scoping issues?

Using my answer to my question on how to download files from a public Google drive I managed in the past to download images using their IDs from a python script and Google API v3 from a public drive us…

Change locale for django-admin-tools

In my settings.py file I have:LANGUAGE_CODE = ru-RUalso, I have installed and working django-admin-tools. But admin language still english. What Im doing wrong?PS.$ cat settings.py | grep USE | grep -…