使用Python scipy.optimize曲线拟合的难度:未找到最佳参数:函数调用次数已达到maxfev = 1000

时间:2018-05-30 20:39:20

标签: python

我希望通过查找幂律的参数,这是一个容易纠正的问题。我在使用curve_fit时遇到了常见的错误,但是没有成功通过建议的解决方案来避开它。

错误是:

Optimal parameters not found: Number of calls to function has reached maxfev = 1000.

以下是我正在使用的数据和powerlaw函数。我希望有人知道指向我的方向。

import matplotlib.pyplot as plt

import numpy as np

from scipy.optimize import curve_fit


def powerlaw(x, amp, ex, x0, y0):
    return (amp * np.power((x-x0),ex) + y0)


x = np.array([  2.5       ,   3.51778656,   4.53557312,   4.55335968,
         5.57114625,   5.58893281,   5.60671937,   5.62450593,
         5.64229249,   8.66007905,   8.67786561,   9.69565217,
         9.71343874,   9.7312253 ,  10.74901186,  10.76679842,
        10.78458498,  11.80237154,  11.8201581 ,  11.83794466,
        11.85573123,  11.87351779,  12.89130435,   3.5       ,
         3.48221344,   4.46442688,   4.44664032,   5.42885375,
         5.41106719,   6.39328063,   6.37549407,   6.35770751,
         6.33992095,   7.32213439,   8.30434783,   9.28656126,
        11.2687747 ,  11.25098814,  11.23320158,  11.21541502,
        11.19762846,  11.1798419 ,  12.16205534,  12.14426877,
        12.12648221,  13.10869565])

y = np.array([52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
       69, 70, 71, 72, 73, 74, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
       63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74])

print(curve_fit(powerlaw, x, y, maxfev=1000))

1 个答案:

答案 0 :(得分:0)

为什么不将maxfev增加到1e6?

print(curve_fit(powerlaw, x, y, maxfev=1000000))

给出:

(array([ 7.56848833e-80,  3.07781530e+01, -4.06201617e+02,  3.43443918e+01]),
 array([[ 7.35597960e-150, -1.37675497e-071,  1.91624708e-070, 4.08767916e-073],
        [-1.37675497e-071,  2.57675332e+007, -3.58647084e+008, -7.65034124e+005],
        [ 1.91624707e-070, -3.58647083e+008,  4.99188464e+009, 1.06502882e+007],
        [ 4.08767868e-073, -7.65034033e+005,  1.06502870e+007, 2.28543208e+004]]))