【发布时间】:2019-08-26 16:17:49
【问题描述】:
我想使用 Scipy interpolate2d 选项来预测“训练”点之外的点中的值。我有 value=f(b_1,b_2) 函数,我想对其进行近似/插值。 matplotlib 能够使用给定的数据进行插值并绘制它。
我尝试使用scipy.interpolate.interp2d,但没有成功,执行时出现错误
raise TypeError('m >= (kx+1)(ky+1) must hold')
TypeError: m >= (kx+1)(ky+1) must hold
这里是代码:
from scipy import interpolate
import scipy
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
calibration = {'b_1_probe1': [-2,1.0,2.0,-2.0,1.5,0.0,1.0,2.0,2.0,-2.0,-0.8,-0.6],
'b_2_probe1': [-2,-2.0,0.4,2,1.0,0.0,2.0,2.0,-2.0,0.0,0.6,-0.7],
'value': [5.0,6.0,3.0,4.0,-2.0,3.0,5.0,-3.0,-4.0,1.0,-2.0,3.0],
'value_A_t': [2.0,3.0,4.0,5.0,1.0,2.0,3.0,-4.0,-2.0,2.0,-3.0,1.0]}
calibration = pd.DataFrame(calibration,columns= ['b_1_probe1','b_2_probe1','value','second_value'])
x = calibration['b_1_probe1']
y = calibration['b_2_probe1']
z = calibration['value']
f, ax = plt.subplots(1,2, sharex=True, sharey=True)
ax[0].tripcolor(x,y,z,shading='gouraud')
ax[1].tricontourf(x,y,z, 20) # choose 20 contour levels, just to show how good its interpolation is
ax[1].plot(x,y, 'ko ')
ax[0].plot(x,y, 'ko ')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.show()
# Interpolation goes here
f=scipy.interpolate.interp2d(x, y, z, kind='cubic', copy=True, bounds_error=False, fill_value=None)
test = {'b_1': [-1.8,-0.5,0.4,2,1.0,0.0,1.4,0.6,-1.0,0.0,0.6,-0.7],
'b_2': [1.8,1.0,2.0,-1.4,1.5,0.0,1.0,2.0,2.0,-2.0,-0.8,-0.6]}
test = pd.DataFrame(test,columns= ['b_1','b_2'])
xnew = test['b_1']
ynew = test['b_2']
znew = f(xnew, ynew)
plt.plot(x, z[0, :], 'ro-', xnew, znew[0, :], 'b-')
plt.show()
我希望 scipy.interpolate 能够从给定数据进行插值并将其显示在绘图中,因此我可以比较插值前后的颜色轮廓。
【问题讨论】: