我有两个数组,将导致df_intensity_01与df_time的图。
df_time
[[ 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170
180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350
360 370 380 390 400 410 420 430 440 450 460 470 480 490 500 510 520 530
540 550 560 570 580 590 600 610 620 630 640 650 660 670 680 690 700 710
720 730 740 750 760 770 780 790 800]]
df_intensity_01
[1. 0.98666909 0.935487 0.91008815 0.86347009 0.81356788
0.79591582 0.78624289 0.76503846 0.75105705 0.72333501 0.67815733
0.69481674 0.68321344 0.66108185 0.65859392 0.64047511 0.63100282
0.63605049 0.6248548 0.60341172 0.57538132 0.57952294 0.57901395
0.56353725 0.56164702 0.55901125 0.54833934 0.53271058 0.52880127
0.52268282 0.51111965 0.5067436 0.49988595 0.49689326 0.48888879
0.48247889 0.4790469 0.47320723 0.46156169 0.45921527 0.4592913
0.45104607 0.44445031 0.44618426 0.43893589 0.42988811 0.42887013
0.42842872 0.41952032 0.41286965 0.41392143 0.41175663 0.40432874
0.39645523 0.39813004 0.38932936 0.38264912 0.38094263 0.3855869
0.38378537 0.37570065 0.37573022 0.37550635 0.36941113 0.36502241
0.36607629 0.36624103 0.36163477 0.35550154 0.35627875 0.35421111
0.34858053 0.34767026 0.34967665 0.34818347 0.34007975 0.34139552
0.34017057 0.33732993 0.33320098]
我正在尝试将数据拟合到单个指数衰减函数,在此我提供了拟合的初始系数。
def func(x, a, b, c):
return a * np.exp(-b * x) + c
xdata = df_time
guess=[1,0.001,0]
ydata = df_intensity
plt.plot(xdata, ydata, 'b-', label='data')
popt, pcov = curve_fit(func, xdata, ydata,p0=guess)
popt
plt.plot(xdata, func(xdata, *popt), 'r-',
label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
我收到一个我真的不知道如何解决的错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ValueError: object too deep for desired array
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-62-97bcc77fc6c7> in <module>
5 ydata = df_intensity
6 plt.plot(xdata, ydata, 'b-', label='data')
----> 7 popt, pcov = curve_fit(func, xdata, ydata,p0=guess)
8 popt
9
/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
749 # Remove full_output from kwargs, otherwise we're passing it in twice.
750 return_full = kwargs.pop('full_output', False)
--> 751 res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
752 popt, pcov, infodict, errmsg, ier = res
753 cost = np.sum(infodict['fvec'] ** 2)
/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
392 with _MINPACK_LOCK:
393 retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
--> 394 gtol, maxfev, epsfcn, factor, diag)
395 else:
396 if col_deriv:
error: Result from function call is not a proper array of floats.
首先,您需要将两个输入都作为一维数组(只有一组括号:[ ]
)。当前,看来df_time
是2D数组,它似乎是您发布的错误的来源。
然后,在绘制数据时,请记住需要评估函数对于x
的每个值,以便x
和y
数组的长度相同。您可以通过列表理解来做到这一点,记住将x
值转换为float
,以便可以将它们传递给函数:
plt.plot(xdata, [func(float(x), *popt) for x in xdata], 'r-',
label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
整个工作代码如下:
df_time = ['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100',
'110', '120', '130', '140', '150', '160', '170', '180', '190', '200',
'210', '220', '230', '240', '250', '260', '270', '280', '290', '300',
'310', '320', '330', '340', '350', '360', '370', '380', '390', '400',
'410', '420', '430', '440', '450', '460', '470', '480', '490', '500',
'510', '520', '530', '540', '550', '560', '570', '580', '590', '600',
'610', '620', '630', '640', '650', '660', '670', '680', '690', '700',
'710', '720', '730', '740', '750', '760', '770', '780', '790', '800']
df_intensity = ['1.', '0.98666909', '0.935487', '0.91008815', '0.86347009', '0.81356788',
'0.79591582', '0.78624289', '0.76503846', '0.75105705', '0.72333501', '0.67815733',
'0.69481674', '0.68321344', '0.66108185', '0.65859392', '0.64047511', '0.63100282',
'0.63605049', '0.6248548', '0.60341172', '0.57538132', '0.57952294', '0.57901395',
'0.56353725', '0.56164702', '0.55901125', '0.54833934', '0.53271058', '0.52880127',
'0.52268282', '0.51111965', '0.5067436', '0.49988595', '0.49689326', '0.48888879',
'0.48247889', '0.4790469', '0.47320723', '0.46156169', '0.45921527', '0.4592913',
'0.45104607', '0.44445031', '0.44618426', '0.43893589', '0.42988811', '0.42887013',
'0.42842872', '0.41952032', '0.41286965', '0.41392143', '0.41175663', '0.40432874',
'0.39645523', '0.39813004', '0.38932936', '0.38264912', '0.38094263', '0.3855869',
'0.38378537', '0.37570065', '0.37573022', '0.37550635', '0.36941113', '0.36502241',
'0.36607629', '0.36624103', '0.36163477', '0.35550154', '0.35627875', '0.35421111',
'0.34858053', '0.34767026', '0.34967665', '0.34818347', '0.34007975', '0.34139552',
'0.34017057', '0.33732993', '0.33320098']
from scipy.optimize import curve_fit
def func(x, a, b, c):
return a * np.exp(-b * x) + c
xdata = [float(x) for x in df_time]
guess=[1,0.001,0]
ydata = df_intensity
plt.plot(xdata, ydata, 'b-', label='data')
popt, pcov = curve_fit(func, xdata, ydata,p0=guess)
fig = plt.figure() # created a 2nd figure for 2nd plot
plt.plot(xdata, [func(float(x), *popt) for x in xdata], 'r-',
label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y');