如何在多个数据集中使用带有共享Fit参数的Scipy.Optimize中的CURVE_FIT? [英] How to use curve_fit from scipy.optimize with a shared fit parameter across multiple datasets?

查看:12
本文介绍了如何在多个数据集中使用带有共享Fit参数的Scipy.Optimize中的CURVE_FIT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有多个参数的Fit函数f,例如ab。现在,我希望将多个数据集适合此函数,并对所有数据集使用相同的a(共享参数),而b可以单独用于每个拟合项。

示例:

import numpy as np

# Fit function
def f(x, a, b):
    return a * x + b

# Datasets
x = np.arange(4)
y = np.array([x + a + np.random.normal(0, 0.5, len(x)) for a in range(3)])

所以我们有4个x值和3个数据集,每个数据集有4个y值。

推荐答案

执行此操作的一种方法是连接数据集并使用调整后的FIT函数。

在下面的示例中,这发生在新的FIT函数gwithnp.concatenate中。还对每个数据集进行了单独的拟合,以便我们可以将它们的图形与带有共享参数的串联拟合进行比较。

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


# Create example datasets

x = np.arange(4)
y = np.array([x + a + np.random.normal(0, 0.5, len(x)) for a in range(3)])
print("x =", x)
print("y =", y)


# Individual fits to each dataset

def f(x, a, b):
    return a * x + b

for y_i in y:
    (a, b), _ = curve_fit(f, x, y_i)
    plt.plot(x, f(x, a, b), label=f"{a:.1f}x{b:+.1f}")
    plt.plot(x, y_i, linestyle="", marker="x", color=plt.gca().lines[-1].get_color())

plt.legend()
plt.show()


# Fit to concatenated dataset with shared parameter

def g(x, a, b_1, b_2, b_3):
    return np.concatenate((f(x, a, b_1), f(x, a, b_2), f(x, a, b_3)))

(a, *b), _ = curve_fit(g, x, y.ravel())
for b_i, y_i in zip(b, y):
    plt.plot(x, f(x, a, b_i), label=f"{a:.1f}x{b_i:+.1f}")
    plt.plot(x, y_i, linestyle="", marker="x", color=plt.gca().lines[-1].get_color())

plt.legend()
plt.show()

输出:

x = [0 1 2 3]
y = [[0.40162683 0.65320576 1.92549698 2.9759299 ]
 [1.15804251 1.69973973 3.24986941 3.25735249]
 [1.97214167 2.60206217 3.93789235 6.04590999]]

个人与a的三个不同值匹配:

适合共享参数a

这篇关于如何在多个数据集中使用带有共享Fit参数的Scipy.Optimize中的CURVE_FIT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆