python - 3D 中的径向基函数(面向奇异矩阵)

标签 python python-3.x interpolation spyder matrix-inverse

我正在编写 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/

相关文章:

python - 如何在python中将对象列表转换为json格式

r - 如何根据另一个查找表/数据框自动为一个数据框插入值?

R na.大约错误: need at least two non-NA values to interpolate

python - 如何使用工作或学校帐户将 SharePoint Online (Office365) Excel 文件读入 Python,特别是 pandas?

python - 如何使用Python在plotly中标记子图a、b、c、d?

python - Celery 未运行,没有错误消息

python - 使用 loc 方法获取 DataFrame 的 View

Python 'list' 自动将十六进制值转换为十进制

Python:循环不等待用户的输入

c++ - 二维插值