我正在编写 2D RBF 插值的代码,但面临奇点问题:
`raise LinAlgError('Matrix is singular.')
LinAlgError: Matrix is singular.`
我有一个QR分解的想法来解决这个问题,但我不知道应该在代码的哪个位置编写QR分解的代码?我也删除了重复的点!
import numpy as np
from scipy import interpolate
import pandas as pd
import xlsxwriter
#Read the Dataset from Excel File
dataset=pd.read_excel(r'C:\Users\Windows 10\.spyder-py3\Messwerte_FIBRE.xlsx')
dataset=dataset.drop([0])
dataset=dataset.drop_duplicates(subset=['T Heizstation','T GWK','P Presse','Bauteilverzug'])
index1=[1]
index2=[4]
index3=[5]
index4=[9]
x1=dataset.iloc[:, index1]
x2=dataset.iloc[:, index2]
x3=dataset.iloc[:, index3]
y=dataset.iloc[:, index4]
#converting string into array
x1np=np.array(x1,dtype=float)
x2np=np.array(x2,dtype=float)
x3np=np.array(x3,dtype=float)
ynp=np.array(y,dtype=float)
newfunc = interpolate.Rbf(x1np,x2np,x3np,ynp,function='linear')
estimation= newfunc(x1np,x2np,x3np)
estimation=np.array(estimation)
# Write the estimation output in Another Excel file
workbook = xlsxwriter.Workbook(r'C:\Users\Windows 10\.spyder-py3\RBF_Reg.xlsx')
worksheet = workbook.add_worksheet('est_output')
row=2
for outputverzug in enumerate(estimation):
worksheet.write(row,0,outputverzug[1])
row+=1
#for next column
row=2
for outputverzug in enumerate(ynp):
worksheet.write_column(row,1,outputverzug[1])
row+=1
worksheet.write(0,0,"Predicted Angle Values")
worksheet.write(1,0,"°")
worksheet.write(0,1,"Original Angle Values")
worksheet.write(1,1,"°")
worksheet.conditional_format('A2:A202', {'type': '3_color_scale',
'min_color': "#FF0000",
'max_color': "#00FF00"})
worksheet.conditional_format('B2:B202', {'type':'3_color_scale',
'min_color': "#00FF00",
'max_color': "#FF0000"})
workbook.close()
最佳答案
对于具有重复项(或其他形式的冗余)的经验数据,使用平滑(而不是强制对所有点进行插值)是合适的。
如果没有平滑,比重复项更糟糕的是“接近重复项不兼容”(在 X、Y 平面上接近,但在 Z 平面上明显不同)。 “几乎奇异”的点会破坏附近的结果。
Rbf
有一个 smooth
关键字参数,float:
Values greater than zero increase the smoothness of the approximation. 0 is for interpolation (default), the function will always go through the nodal points in this case.
我设计了一个具有近奇点的示例。尝试使用不同的 smooth
参数值。
import numpy as np
from scipy.interpolate import Rbf
import matplotlib.pyplot as plt
np.random.seed(99)
x, y = np.random.rand(2, 100)
x[0] = 0.
for i in range(1,len(x)): # Generate clumpy distribution.
x[i] = x[i-1] + np.exp(-6.*x[i])
y += 2.*np.sin(x)
fig, ax = plt.subplots(figsize=(15,4))
ax.plot(x ,y ,'ko')
xi = np.linspace(x[0], x[-1], 1000)
for smooth in [.01, 1, 100]:
rbfi = Rbf(x, y, smooth=smooth)
yi = rbfi(xi)
ax.plot(xi,yi,'k-')
fig.show()
关于python - 3D 中的径向基函数(面向奇异矩阵),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50435630/