Python - 双对数图上的线性趋势线拟合得不好

问题描述 投票:0回答:1

我有多组四个数据点,我想在每个点之间的双对数图上为每个组创建一条线性趋势线(四个点意味着总共三个趋势线)。我使用

curve_fit
中的
scipy.optimize
来执行此操作。它适用于我拥有的所有套装,除了一套:

大多数时候,这与最初的猜测(

p0
)有关,但在尝试了不同的猜测之后,我仍然得到了这个结果。我在文献中看到,对于相似的值,这些图看起来很好,所以我一定错过了一些东西。

我在这里缺少什么才能让它发挥作用?我唯一能想到的就是猜测还是有问题。我有下面的测试代码可以复制粘贴。

代码:

from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import numpy as np

# Two lists with the information for the line
list_x = [3.139, 2.53, 0.821, 0.27]
list_y = [35.56, 26.82, 10.42, 4.66]

def func_exp(x, a, b):
    return (a * x)**b


# Points
point1_x = list_x[0]
point1_y = list_y[0]

point2_x = list_x[1]
point2_y = list_y[1]

point3_x = list_x[2]
point3_y = list_y[2]

point4_x = list_x[3]
point4_y = list_y[3]


# Lines between points
p0_12 = (point1_x, point2_x)
formula_12, pcov_12 = curve_fit(func_exp, [point1_x, point1_y], [point2_x, point2_y], maxfev=10000, p0=p0_12)

p0_23 = (point2_x, point3_x)
formula_23, pcov_23 = curve_fit(func_exp, [point2_x, point2_y], [point3_x, point3_y], maxfev=10000, p0=p0_23)

p0_34 = (point3_x, point4_x)
formula_34, pcov_34 = curve_fit(func_exp, [point3_x, point3_y], [point4_x, point4_y], maxfev=10000, p0=p0_34)


# Create plot
plot_x_12 = np.linspace(point1_x, point2_x, 1000)
plot_y_12 = (formula_12[0] * plot_x_12)**formula_12[1]

plot_x_23 = np.linspace(point2_x, point3_x, 1000)
plot_y_23 = (formula_23[0] * plot_x_23)**formula_23[1]

plot_x_34 = np.linspace(point3_x, point4_x, 1000)
plot_y_34 = (formula_34[0] * plot_x_34)**formula_34[1]

fig, ax1 = plt.subplots(1, 1, figsize=(10, 5))
ax1.scatter(list_x, list_y, color='black')
ax1.plot(plot_x_12, plot_y_12)
ax1.plot(plot_x_23, plot_y_23)
ax1.plot(plot_x_34, plot_y_34)

ax1.set_xscale('log', base=10)
ax1.set_yscale('log', base=10)
python scipy curve-fitting
1个回答
0
投票

对于您的问题,我认为最好检查一下曲线拟合的输出与实际 y 值一次。根据您当前的表述,它们根本不匹配:

func_exp(point1_x, formula_12[0], formula_12[1]), point1_y
# prints: (2.53, 35.56)

错误出现在 curve_fit 的函数调用上。第二个参数应该是 x,而不是第一个点,依此类推...

因此,如果您使用以下公式,您会得到一个看起来更清晰的图:

p0_12 = (point1_x, point2_x)
formula_12, pcov_12 = curve_fit(func_exp, [point1_x, point2_x], [point1_y, point2_y], maxfev=10000, p0=p0_12)

p0_23 = (point2_x, point3_x)
formula_23, pcov_23 = curve_fit(func_exp, [point2_x, point3_x], [point2_y, point3_y], maxfev=10000, p0=p0_23)

p0_34 = (point3_x, point4_x)
formula_34, pcov_34 = curve_fit(func_exp, [point3_x, point4_x], [point3_y, point4_y], maxfev=10000, p0=p0_34)

剧情:

© www.soinside.com 2019 - 2024. All rights reserved.