【问题标题】:Curve fitting and smoothing using python for 3D data使用 python 对 3D 数据进行曲线拟合和平滑处理
【发布时间】:2020-11-24 17:43:56
【问题描述】:

我目前正在使用多项式定义函数来创建 3d 曲线拟合,但无济于事。 image 1 scatter, image 2 curve fitting 代码如下:

#import excel data 
"""
how can I improve this polynomial function, 
is there any better methods instead of polynomial? 
"""

def func(data, a, b, c, d):
    x = data[0]
    y = data[1]
    z = data[2]
    return a + b * x + c * y + d * x**2 
# using curve fitting to pass the function 
fittedParameters, pcov = scipy.optimize.curve_fit(
    func, [xData, yData, zData], 
    zData, p0 = None, method= 'lm', maxfev=5000000
) #, p0 = None, maxfev=5000

# making mesh grid 
# making meshgrid
xModel = numpy.linspace( min(x_data), max(x_data), 80) #min(x_data)
yModel = numpy.linspace( min(y_data), max(y_data), 80)
X, Y = numpy.meshgrid( xModel, yModel )

#popt = fittedparameters
a = fittedParameters[0]
b = fittedParameters[1]
c = fittedParameters[2]
d = fittedParameters[3]
x = X
y = Y
Z = a + b * x + c * y + d * x**2
axes.plot_surface(
    X, Y, Z,
    rstride=1, cstride=1, cmap=cm.coolwarm, 
    linewidth=1, antialiased=True
)
axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

# add a title for surface plot
axes.set_title('Surface plot of LN(PoF) and length & depth') 


axes.set_xlabel('Depth (mm)')
axes.set_ylabel('Length (mm)')
axes.set_zlabel('LN(PoF)') # Z axis data label

plt.show()

enter image description here

【问题讨论】:

  • return a*x**3 + b*x**2 +c*x + d*x**2 这是一个错字吗?请参阅Z = a + b*x + c*y + d*x**2 等式不同。您只在第一个中使用 x,在第二个中使用 x, y。 func 中的 y 和 z 在哪里?为什么你会有b*x**2d*x**2 这是同一系数上的另一个参数...?
  • @dzang 道歉,我在应对时一定有错误。我在玩 33,44,66 多项式,看起来有些东西可能搞混了。真诚的歉意。我会改正的。
  • 在不知道数据的情况下,很难说,但是有没有具体的原因为什么它在x 中是二次的,而在y 中却是线性的?我不得不说,如果你的合身需要maxfev=5000000 出了点问题......这可能是你问的原因......

标签: python curve-fitting smoothing


【解决方案1】:

内置模块


#%% splprep and splev for the 2D smoothing of x and y value 
def splprep_2d(x,y):
    tck, u = interpolate.splprep([x,y], s = 2,
                 task = 0,full_output=0,quiet = 0,
                 k = 5, t=None)
    fittedParameters = interpolate.splev(u,tck)
    xnew = np.array(fittedParameters[0])
    ynew = np.array(fittedParameters[1])
    return xnew, ynew
xnew, ynew = splprep_2d(x,y)
splprep_2d(x,y)

s = 2 是平滑因子,较低会导致绘图准确,使用较高的平滑因子会导致曲线平滑。

K = 曲线的抛物线性质,最多可以使用第 5 条抛物线。

这些是您的平滑参数: xnew = np.array(fittedParameters[0]) ynew = np.array(fittedParameters[1])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-29
    • 2017-01-02
    • 2021-11-14
    • 2021-10-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多