使用简单数据的边界和初始参数进行Scipy curve_fit混淆

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

尽管我非常适合其他数据集,但由于某些原因,以下代码不适用于相对简单的点集。我已经尝试了衰减指数和幂以及初始参数和界限。我相信这暴露了我更深的误解;我感谢任何建议。

    snr = [1e10, 5, 1, .5, .1, .05]
    tau = [1, 8, 10, 14, 35, 80]

    fig1, ax1 = plt.subplots()

    def fit(x, a, b, c): #c: asymptote
        #return a * np.exp(b * x) + 1.
        return np.power(x,a)*b + c

    xlist = np.arange(0,len(snr),1)
    p0 = [-1., 1., 1.]
    params = curve_fit(fit, xlist, tau, p0)#, bounds=([-np.inf, 0., 0.], [0., np.inf, np.inf]))

    a, b, c = params[0]
    print(a,b,c)
    ax1.plot(xlist, fit(xlist, a, b, c), c='b', label='Fit')

    #ax1.plot(snr, tau, zorder=-1, c='k', alpha=.25)
    ax1.scatter(snr, tau)
    ax1.set_xscale('log')        
    #ax1.set_xlim(.02, 15)
    plt.show()
python scipy curve-fitting
1个回答
0
投票

这对我有用

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

snr = [1e10, 5, 1, .5, .1, .05]
tau = [1, 8, 10, 14, 35, 80]

fig1, ax1 = plt.subplots()

def fit(x, a, b, c):
    return np.power(x, a)*b + c

xlist = np.arange(0,len(snr),1)
p0 = [-10, 10., 1.]
params = curve_fit(fit, snr, tau), p0)

print('Fitting parameters: {}'.format(params[0]))
ax1.plot(snr, fit(snr, *params[0]), c='b', label='Fit')
ax1.scatter(snr, tau)
ax1.set_xscale('log')        
plt.show()

enter image description here

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