curve_fit的快速Python指数函数:Numpy Express给出较慢的结果



我正在用python实时拟合指数数据。数以百万计的对我的指数衰减函数的调用,因此加速它可以极大地提高性能。我最近偶然发现了numexpr模块,但它似乎是为更大的数组量身定制的。在我的健身计划中使用它的效果更差。两个拟合函数的Timeit结果:

import numexpr as ne
t=np.arange(0,1400)
a0,a1,a2=[-0.034913174979286636, 0.634038654906443, 0.010961607601301245]
%timeit (a0 + a1*np.exp(-t*a2))
25.5 µs ± 225 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit ne.evaluate('(a0 + a1*exp(-t*a2))')
33.1 µs ± 570 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
import numba as nb
@nb.njit(error_model="numpy",parallel=True)
def fexp_nb(t,a0,a1,a2):
return a0 + a1*np.exp(-t*a2)
%timeit fexp_nb(t,a0,a1,a2)
26.4 µs ± 974 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

这可能是由于numpy计算编译开销。有办法预编译吗?或者我可以做的另一个优化(将我的chunksize更改为我的fit数组的长度)?我在文档中没有看到任何这样的选项。

我也运行过cpython和numba,它们是否适合加速scipy适配?(编辑:Numba似乎也没有帮助,见上面的结果)这里是相关的适合代码,其中y是原始数据:

from scipy.optimize import curve_fit
...
x=np.arange(0,leny-syfd)
(M,pcov)=curve_fit(fexp, x, y[syfd:], p0=(M,), sigma=None, method='lm', ftol=0.001, xtol=0.001)

也是fexp函数的格式:

def fexp(t,a0,a1,a2):
return a0 + a1*np.exp(-t*a2)
def fexp_ne(t,a0,a1,a2):
return ne.evaluate('a0 + a1*exp(-t*a2)')

这已经在一个多进程例程中运行了。目前的限制是fexp计算的速度。任何帮助或洞察力优化指数调用在python将不胜感激。

编辑:这里是一个完整的例子:

array([ 6.77582760e-01,  6.71805572e-01,  6.62855412e-01,  6.54604329e-01,
6.47518077e-01,  6.40244555e-01,  6.33497217e-01,  6.25381033e-01,
6.17461716e-01,  6.11452674e-01,  6.03328389e-01,  5.97276341e-01,
5.90335893e-01,  5.83810521e-01,  5.77519704e-01,  5.70318901e-01,
5.64502798e-01,  5.58176217e-01,  5.51840170e-01,  5.45846183e-01,
5.40182764e-01,  5.31834927e-01,  5.26967196e-01,  5.21053537e-01,
5.13871378e-01,  5.08438374e-01,  5.03092689e-01,  4.97433669e-01,
4.91322442e-01,  4.85540282e-01,  4.78926889e-01,  4.73335312e-01,
4.69494226e-01,  4.63500965e-01,  4.57355641e-01,  4.51666783e-01,
4.47087242e-01,  4.41797146e-01,  4.36845384e-01,  4.31697062e-01,
4.26889273e-01,  4.22507944e-01,  4.16283962e-01,  4.12060334e-01,
4.06914605e-01,  4.02757403e-01,  3.97040053e-01,  3.92698393e-01,
3.87453958e-01,  3.82990676e-01,  3.79885919e-01,  3.74404426e-01,
3.70689325e-01,  3.66252384e-01,  3.61609911e-01,  3.57343305e-01,
3.53040276e-01,  3.48222287e-01,  3.43435412e-01,  3.40570855e-01,
3.36410098e-01,  3.32147335e-01,  3.28278134e-01,  3.23822881e-01,
3.20550978e-01,  3.15701573e-01,  3.12430630e-01,  3.08407969e-01,
3.05027449e-01,  3.02299415e-01,  2.97209405e-01,  2.94273878e-01,
2.90333001e-01,  2.86942177e-01,  2.83282482e-01,  2.79976710e-01,
2.76577305e-01,  2.73129084e-01,  2.71373570e-01,  2.66898124e-01,
2.63082368e-01,  2.59528578e-01,  2.56441631e-01,  2.53613598e-01,
2.51009741e-01,  2.46518043e-01,  2.44198633e-01,  2.40293215e-01,
2.38216907e-01,  2.34451158e-01,  2.32501154e-01,  2.29227875e-01,
2.26828373e-01,  2.23271660e-01,  2.20187713e-01,  2.17651154e-01,
2.14815004e-01,  2.12273672e-01,  2.09486921e-01,  2.07808467e-01,
2.04258261e-01,  2.01695808e-01,  1.98213932e-01,  1.96140969e-01,
1.94807019e-01,  1.89744628e-01,  1.89274492e-01,  1.87326534e-01,
1.83796672e-01,  1.82229243e-01,  1.79545829e-01,  1.78007550e-01,
1.75067117e-01,  1.71509604e-01,  1.71647877e-01,  1.68529915e-01,
1.64850192e-01,  1.62491681e-01,  1.62054741e-01,  1.59605217e-01,
1.56905657e-01,  1.56494100e-01,  1.53703593e-01,  1.50716588e-01,
1.48780532e-01,  1.47442009e-01,  1.45447195e-01,  1.42603381e-01,
1.41557222e-01,  1.39193149e-01,  1.37259584e-01,  1.35013117e-01,
1.34160615e-01,  1.31822213e-01,  1.29757188e-01,  1.27075812e-01,
1.25988605e-01,  1.25003127e-01,  1.23063061e-01,  1.21201713e-01,
1.19385533e-01,  1.17488818e-01,  1.14957141e-01,  1.13375794e-01,
1.13162993e-01,  1.10977052e-01,  1.09198020e-01,  1.08012072e-01,
1.06087446e-01,  1.05099185e-01,  1.03226144e-01,  1.01090762e-01,
9.89268290e-02,  9.84298054e-02,  9.74088178e-02,  9.57077008e-02,
9.34094478e-02,  9.34770285e-02,  9.16919224e-02,  9.02382531e-02,
8.96615820e-02,  8.71530839e-02,  8.61682304e-02,  8.42724668e-02,
8.46299322e-02,  8.21125377e-02,  7.98889165e-02,  8.04244477e-02,
7.84594084e-02,  7.76000882e-02,  7.54094009e-02,  7.45808875e-02,
7.40301258e-02,  7.27424577e-02,  7.12559804e-02,  7.04680289e-02,
6.85277010e-02,  6.87051292e-02,  6.76578279e-02,  6.42697271e-02,
6.40199091e-02,  6.36143478e-02,  6.15051025e-02,  6.13128604e-02,
6.05643017e-02,  5.82606683e-02,  5.74001989e-02,  5.71317747e-02,
5.60278909e-02,  5.51050378e-02,  5.39380398e-02,  5.37979092e-02,
5.20580658e-02,  5.04491614e-02,  5.05715122e-02,  4.92034685e-02,
4.75611572e-02,  4.69520119e-02,  4.64852536e-02,  4.59729697e-02,
4.55283250e-02,  4.42763236e-02,  4.28236355e-02,  4.27546272e-02,
4.19050488e-02,  4.04688277e-02,  3.94272902e-02,  3.95748321e-02,
3.89298743e-02,  3.73866731e-02,  3.72141633e-02,  3.65430784e-02,
3.44737817e-02,  3.42898759e-02,  3.31612552e-02,  3.26311197e-02,
3.18868672e-02,  3.04010275e-02,  3.06206431e-02,  2.94735156e-02,
2.96741315e-02,  2.88437650e-02,  2.76153159e-02,  2.77555857e-02,
2.58538605e-02,  2.54368711e-02,  2.44673277e-02,  2.56250910e-02,
2.33827364e-02,  2.26483672e-02,  2.27238410e-02,  2.03568150e-02,
2.14757502e-02,  2.01854003e-02,  1.97837673e-02,  1.97186331e-02,
1.77891223e-02,  1.91265448e-02,  1.73643535e-02,  1.63559444e-02,
1.64184309e-02,  1.60763341e-02,  1.56667306e-02,  1.53286118e-02,
1.40528680e-02,  1.40642010e-02,  1.50253718e-02,  1.23722700e-02,
1.18349807e-02,  1.16952932e-02,  1.10814560e-02,  1.12464417e-02,
9.02985284e-03,  9.90810088e-03,  9.90613947e-03,  8.33110496e-03,
8.03159218e-03,  7.55101564e-03,  7.87897600e-03,  6.63023548e-03,
6.02989494e-03,  6.19476245e-03,  5.14076580e-03,  5.09559047e-03,
3.78939236e-03,  3.91231009e-03,  3.87641581e-03,  2.88336640e-03,
2.62317124e-03,  2.07934571e-03,  2.31619406e-03,  1.60184113e-03,
8.19957252e-04,  1.55867472e-03,  9.11270374e-04,  7.60252051e-04,
-2.49981482e-04,  1.61518200e-04, -1.50757208e-03, -1.40549609e-03,
-2.15401175e-03, -2.55539057e-03, -2.26817884e-03, -3.42271219e-03,
-3.61592119e-03, -3.78268491e-03, -3.83210682e-03, -5.32324729e-03,
-5.99929258e-03, -4.99760968e-03, -5.18682072e-03, -7.09107309e-03,
-6.17065428e-03, -6.49019882e-03, -7.28126650e-03, -8.18286576e-03,
-8.10584493e-03, -8.38154570e-03, -7.94357643e-03, -8.73734918e-03,
-8.66833752e-03, -8.81083174e-03, -9.63921586e-03, -9.36494609e-03,
-1.05607430e-02, -9.98560128e-03, -9.90366976e-03, -1.11094682e-02,
-1.06379207e-02, -1.06525755e-02, -1.21732000e-02, -1.13729375e-02,
-1.16419153e-02, -1.27108551e-02, -1.25469346e-02, -1.27152863e-02,
-1.17560316e-02, -1.33894917e-02, -1.36998304e-02, -1.35083165e-02,
-1.38339452e-02, -1.46794008e-02, -1.46427131e-02, -1.48451722e-02,
-1.41840269e-02, -1.45224640e-02, -1.54875774e-02, -1.60601588e-02,
-1.60807530e-02, -1.58817803e-02, -1.61808488e-02, -1.69161374e-02,
-1.63841256e-02, -1.67614781e-02, -1.73994434e-02, -1.73168742e-02,
-1.74870049e-02, -1.79067005e-02, -1.75728560e-02, -1.81948350e-02,
-1.80174598e-02, -1.93596162e-02, -1.88724969e-02, -1.83835134e-02,
-1.92801688e-02, -1.83701659e-02, -1.99196601e-02, -1.91348148e-02,
-1.99374233e-02, -2.01917585e-02, -2.05305591e-02, -1.95394645e-02,
-2.00409723e-02, -1.94469646e-02, -2.04755182e-02, -2.06248027e-02,
-2.05010916e-02, -2.07693578e-02, -2.11682006e-02, -2.04152019e-02,
-2.01176837e-02, -2.21404326e-02, -2.22886409e-02, -2.22694136e-02,
-2.21583693e-02, -2.23670971e-02, -2.31068168e-02, -2.23772147e-02,
-2.25358675e-02, -2.34127542e-02, -2.28211864e-02, -2.25208901e-02,
-2.32599413e-02, -2.36529662e-02, -2.34243801e-02, -2.35504858e-02,
-2.35490835e-02, -2.36486644e-02, -2.42875634e-02, -2.44091021e-02,
-2.42907779e-02, -2.42465971e-02, -2.32332165e-02, -2.35861171e-02,
-2.43288435e-02, -2.47434837e-02, -2.38644776e-02, -2.48019800e-02,
-2.57744739e-02, -2.51345549e-02, -2.47581495e-02, -2.52083800e-02,
-2.52254769e-02, -2.59064627e-02, -2.67025427e-02, -2.53231836e-02,
-2.67526497e-02, -2.61746427e-02, -2.55226625e-02, -2.71449442e-02,
-2.65484337e-02, -2.64726648e-02, -2.64737643e-02, -2.70614712e-02,
-2.61358754e-02, -2.69886395e-02, -2.64278420e-02, -2.66606957e-02,
-2.75539499e-02, -2.70457799e-02, -2.71564872e-02, -2.68975728e-02,
-2.77275194e-02, -2.71034376e-02, -2.81987851e-02, -2.69518921e-02,
-2.68113748e-02, -2.80321816e-02, -2.82383723e-02, -2.85239729e-02,
-2.79248766e-02, -2.79176487e-02, -2.75685659e-02, -2.75524150e-02,
-2.86517306e-02, -2.82334042e-02, -2.82021609e-02, -2.87045516e-02,
-2.79073930e-02, -2.80293837e-02, -2.90965831e-02, -2.91120301e-02,
-2.86658119e-02, -2.96395014e-02, -2.81576011e-02, -2.85221055e-02,
-2.93785647e-02, -2.90209844e-02, -2.93115642e-02, -2.95479562e-02,
-2.91992171e-02, -3.00063362e-02, -2.93740143e-02, -2.94714359e-02,
-2.96185127e-02, -2.92964155e-02, -3.03497746e-02, -2.87453930e-02,
-2.96188685e-02, -3.01844065e-02, -2.94560033e-02, -2.93963522e-02,
-3.00035737e-02, -2.97532861e-02, -3.01599580e-02, -3.06006544e-02,
-2.97609362e-02, -3.07181808e-02, -3.07292531e-02, -3.06362095e-02,
-2.98901790e-02, -3.04443508e-02, -3.02572028e-02, -3.09409910e-02,
-3.08647315e-02, -3.06634902e-02, -3.15067909e-02, -3.03233359e-02,
-2.99905665e-02, -3.02081986e-02, -3.11139285e-02, -3.13340137e-02,
-3.09951026e-02, -3.09899665e-02, -3.11665638e-02, -3.01946180e-02,
-3.08299765e-02, -3.08378929e-02, -3.15699150e-02, -3.15054284e-02,
-3.10488971e-02, -3.11838352e-02, -3.12699404e-02, -3.17345207e-02,
-3.16880447e-02, -3.12629832e-02, -3.27701718e-02, -3.14923815e-02,
-3.18409559e-02, -3.17549392e-02, -3.18248006e-02, -3.18833433e-02,
-3.18507044e-02, -3.28423670e-02, -3.25059588e-02, -3.22541783e-02,
-3.24280562e-02, -3.11278196e-02, -3.23799083e-02, -3.18008029e-02,
-3.15655292e-02, -3.22360527e-02, -3.27990780e-02, -3.31176766e-02,
-3.19895222e-02, -3.21722148e-02, -3.20069616e-02, -3.25053035e-02,
-3.21758613e-02, -3.29895686e-02, -3.23257713e-02, -3.22494555e-02,
-3.34180414e-02, -3.28823631e-02, -3.17963022e-02, -3.31061402e-02,
-3.18056882e-02, -3.28681891e-02, -3.25210312e-02, -3.20252718e-02,
-3.26430672e-02, -3.16463911e-02, -3.29117511e-02, -3.35422067e-02,
-3.26000323e-02, -3.27540496e-02, -3.28575831e-02, -3.22863797e-02,
-3.21990766e-02, -3.29656052e-02, -3.24849060e-02, -3.24672224e-02,
-3.34997607e-02, -3.23476219e-02, -3.30003359e-02, -3.31903249e-02,
-3.23320069e-02, -3.21583965e-02, -3.33494120e-02, -3.25694200e-02,
-3.29155545e-02, -3.29860160e-02, -3.29130749e-02, -3.38232579e-02,
-3.22732056e-02, -3.39209757e-02, -3.25092142e-02, -3.37595780e-02,
-3.33669686e-02, -3.35825564e-02, -3.33722726e-02, -3.27740007e-02,
-3.28696731e-02, -3.27377683e-02, -3.27916633e-02, -3.37191211e-02,
-3.33418515e-02, -3.27649672e-02, -3.31879414e-02, -3.36483182e-02,
-3.38895037e-02, -3.36076148e-02, -3.35543298e-02, -3.36461247e-02,
-3.34425827e-02, -3.28934586e-02, -3.37521003e-02, -3.43962283e-02,
-3.35585952e-02, -3.29186022e-02, -3.38042560e-02, -3.38795420e-02,
-3.37015369e-02, -3.37722326e-02, -3.39482442e-02, -3.35983382e-02,
-3.38301731e-02, -3.37419695e-02, -3.33614888e-02, -3.42261595e-02,
-3.33288687e-02, -3.28740788e-02, -3.39862016e-02, -3.39686340e-02,
-3.36117973e-02, -3.40604864e-02, -3.36467247e-02, -3.42279772e-02,
-3.38968587e-02, -3.37241014e-02, -3.33639541e-02, -3.40628743e-02,
-3.34929792e-02, -3.25138232e-02, -3.33583295e-02, -3.40803016e-02,
-3.36138505e-02, -3.33820101e-02, -3.37644909e-02, -3.44127361e-02,
-3.39649377e-02, -3.27922368e-02, -3.39639708e-02, -3.42206377e-02,
-3.38836283e-02, -3.44770250e-02, -3.41299533e-02, -3.39258577e-02,
-3.46702638e-02, -3.33523470e-02, -3.36042512e-02, -3.40460383e-02,
-3.49591397e-02, -3.41034329e-02, -3.39737027e-02, -3.41011223e-02,
-3.30045869e-02, -3.36814333e-02, -3.47013877e-02, -3.37575989e-02,
-3.36095144e-02, -3.33079927e-02, -3.39511869e-02, -3.34799268e-02,
-3.35149171e-02, -3.46605639e-02, -3.32439569e-02, -3.39443446e-02,
-3.42861055e-02, -3.35662419e-02, -3.46708870e-02, -3.50600333e-02,
-3.44727983e-02, -3.33925530e-02, -3.43963266e-02, -3.42983944e-02,
-3.35456002e-02, -3.46927984e-02, -3.45836514e-02, -3.41312429e-02,
-3.35098882e-02, -3.37101306e-02, -3.42577243e-02, -3.38490866e-02,
-3.51506072e-02, -3.45444034e-02, -3.56960615e-02, -3.40305316e-02,
-3.51487629e-02, -3.40921794e-02, -3.32115512e-02, -3.47733277e-02,
-3.36974616e-02, -3.41679051e-02, -3.39379587e-02, -3.42782666e-02,
-3.49266687e-02, -3.35467516e-02, -3.39475116e-02, -3.38363944e-02,
-3.41696444e-02, -3.37882222e-02, -3.35646142e-02, -3.43454582e-02,
-3.48884429e-02, -3.43949884e-02, -3.41012770e-02, -3.41740303e-02,
-3.52294612e-02, -3.48467251e-02, -3.35748776e-02, -3.46945377e-02,
-3.41908751e-02, -3.47079427e-02, -3.47498891e-02, -3.37765466e-02,
-3.38721483e-02, -3.43011504e-02, -3.51082773e-02, -3.41377945e-02,
-3.47690137e-02, -3.46573450e-02, -3.39032546e-02, -3.51362022e-02,
-3.40017028e-02, -3.50607637e-02, -3.44254118e-02, -3.32999216e-02,
-3.40899627e-02, -3.36552731e-02, -3.51097480e-02, -3.38504148e-02,
-3.53445609e-02, -3.48091391e-02, -3.34585502e-02, -3.48543033e-02,
-3.48724930e-02, -3.45203008e-02, -3.45642097e-02, -3.38434300e-02,
-3.47620255e-02, -3.50947994e-02, -3.45384252e-02, -3.43135199e-02,
-3.37178912e-02, -3.38296935e-02, -3.45168420e-02, -3.41297621e-02,
-3.51864141e-02, -3.51024019e-02, -3.43297582e-02, -3.47088709e-02,
-3.46309274e-02, -3.46781194e-02, -3.50010209e-02, -3.46962427e-02,
-3.44095095e-02, -3.53311350e-02, -3.39931809e-02, -3.46238409e-02,
-3.52434994e-02, -3.40879825e-02, -3.46586257e-02, -3.54748016e-02,
-3.44133748e-02, -3.39382339e-02, -3.57839292e-02, -3.55845475e-02,
-3.33617264e-02, -3.47458503e-02, -3.42885498e-02, -3.48665094e-02,
-3.42821275e-02, -3.36819682e-02, -3.46807692e-02, -3.41946941e-02,
-3.48663270e-02, -3.49485227e-02, -3.42731017e-02, -3.37184470e-02,
-3.50249766e-02, -3.46173434e-02, -3.47982270e-02, -3.44577258e-02,
-3.34760879e-02, -3.50841823e-02, -3.48028106e-02, -3.47610885e-02,
-3.48627567e-02, -3.46453136e-02, -3.53597660e-02, -3.48366916e-02,
-3.41159649e-02, -3.48510093e-02, -3.38929624e-02, -3.41668454e-02,
-3.51546471e-02, -3.48239087e-02, -3.47974259e-02, -3.45125944e-02,
-3.35662054e-02, -3.46594125e-02, -3.43376767e-02, -3.54654090e-02,
-3.44872972e-02, -3.50279292e-02, -3.51916011e-02, -3.51733827e-02,
-3.49335033e-02, -3.43695134e-02, -3.50273204e-02, -3.45770412e-02,
-3.47294761e-02, -3.42238455e-02, -3.46603175e-02, -3.53509888e-02,
-3.44939229e-02, -3.47054498e-02, -3.43152802e-02, -3.53069042e-02,
-3.48990588e-02, -3.48061202e-02, -3.46125598e-02, -3.43076313e-02,
-3.56307417e-02, -3.45607201e-02, -3.48562659e-02, -3.48183361e-02,
-3.42800213e-02, -3.48008216e-02, -3.46413477e-02, -3.33991997e-02,
-3.34592928e-02, -3.51194744e-02, -3.56346115e-02, -3.49305882e-02,
-3.39160893e-02, -3.44744768e-02, -3.37401949e-02, -3.44527422e-02,
-3.56275449e-02, -3.40924192e-02, -3.48064583e-02, -3.52221836e-02,
-3.41056418e-02, -3.36949311e-02, -3.40409519e-02, -3.50560851e-02,
-3.45227075e-02, -3.47602652e-02, -3.44943538e-02, -3.51834814e-02,
-3.48978101e-02, -3.46459700e-02, -3.48302294e-02, -3.40745323e-02,
-3.44707363e-02, -3.46792487e-02, -3.41969107e-02, -3.53778739e-02,
-3.38056240e-02, -3.45850758e-02, -3.52381986e-02, -3.34324939e-02,
-3.50983387e-02, -3.34525378e-02, -3.44871303e-02, -3.50951220e-02,
-3.50662501e-02, -3.44533721e-02, -3.43439344e-02, -3.44846993e-02,
-3.47457564e-02, -3.48807088e-02, -3.44570175e-02, -3.34232593e-02,
-3.37898079e-02, -3.44684047e-02, -3.41998567e-02, -3.51761452e-02,
-3.36589506e-02, -3.43113375e-02, -3.41142145e-02, -3.52910604e-02,
-3.42636637e-02, -3.44097305e-02, -3.52569121e-02, -3.47346609e-02,
-3.52394760e-02, -3.51207529e-02, -3.59074049e-02, -3.43782032e-02,
-3.45249451e-02, -3.55029542e-02, -3.44689495e-02, -3.48015929e-02,
-3.42877078e-02, -3.42021596e-02, -3.56332832e-02, -3.41231387e-02,
-3.34710413e-02, -3.43916856e-02, -3.46014289e-02, -3.43719466e-02,
-3.45558978e-02, -3.44913195e-02, -3.39216332e-02, -3.41125714e-02,
-3.44156379e-02, -3.35212434e-02, -3.37740482e-02, -3.56893883e-02,
-3.42789395e-02, -3.31686223e-02, -3.41410333e-02, -3.40329969e-02,
-3.40408281e-02, -3.40798109e-02, -3.49933344e-02, -3.53360545e-02,
-3.40809115e-02, -3.42890593e-02, -3.44813047e-02, -3.48948829e-02,
-3.41661404e-02, -3.41507265e-02, -3.42771229e-02, -3.45525076e-02,
-3.36386559e-02, -3.47478316e-02, -3.47615404e-02, -3.53777435e-02,
-3.48854549e-02, -3.46911188e-02, -3.40262419e-02, -3.52597000e-02,
-3.51428234e-02, -3.41261487e-02, -3.44094045e-02, -3.50469709e-02,
-3.40509976e-02, -3.61236414e-02, -3.54544395e-02, -3.45446896e-02,
-3.43770087e-02, -3.48358683e-02, -3.48614926e-02, -3.46836842e-02,
-3.48124762e-02, -3.45195361e-02, -3.53525381e-02, -3.48897379e-02,
-3.48570559e-02, -3.40205499e-02, -3.44614663e-02, -3.50886444e-02,
-3.47593216e-02, -3.45004546e-02, -3.47610443e-02, -3.51304240e-02,
-3.50419176e-02, -3.55487284e-02, -3.48448775e-02, -3.45270005e-02,
-3.48444731e-02, -3.56791348e-02, -3.41868794e-02, -3.45829663e-02,
-3.42041597e-02, -3.41088884e-02, -3.37886267e-02, -3.50681751e-02,
-3.48749108e-02, -3.48883655e-02, -3.53833780e-02, -3.50574166e-02,
-3.48498214e-02, -3.40616643e-02, -3.44808605e-02, -3.43902313e-02,
-3.54206094e-02, -3.54653968e-02, -3.43590665e-02, -3.44195629e-02,
-3.46913088e-02, -3.44187496e-02, -3.44500106e-02, -3.42162651e-02,
-3.41504437e-02, -3.50227445e-02, -3.51200634e-02, -3.56780851e-02,
-3.51531719e-02, -3.46342579e-02, -3.35805430e-02, -3.46176141e-02,
-3.46284146e-02, -3.52208962e-02, -3.48296515e-02, -3.42411877e-02,
-3.47367980e-02, -3.49304037e-02, -3.53047306e-02, -3.47298861e-02,
-3.45596703e-02, -3.35770379e-02, -3.47974391e-02, -3.57307270e-02,
-3.44462867e-02, -3.43336445e-02, -3.54116576e-02, -3.42430972e-02,
-3.42990431e-02, -3.36653012e-02, -3.53732881e-02, -3.45496478e-02,
-3.45974553e-02, -3.50473587e-02, -3.37887692e-02, -3.51737484e-02,
-3.47449674e-02, -3.49421666e-02, -3.40494031e-02, -3.42924008e-02,
-3.44493023e-02, -3.44581822e-02, -3.46422660e-02, -3.43513060e-02,
-3.51855268e-02, -3.54661305e-02, -3.33121022e-02, -3.44833136e-02,
-3.50119164e-02])

有几个选项:

你可以用一小部分数据进行第一次优化,然后使用结果作为使用全部数据进行最后优化的种子。

您还可以提供雅可比矩阵以避免其数值计算,这涉及到多次调用fexp。在这种情况下,雅可比矩阵将是:

def jacobian(t, _, a1, a2):
j = np.empty((len(t), 3))
j[:, 0] = 1.
j[:, 1] = np.exp(-t * a2)
j[:, 2] = -a1 * t * j[:, 1]
return j

如果你想并行化fexp和Numba,你可以使用prange:

@nb.njit(parallel=True)
def fexp_nb(t, a0, a1, a2):
result = np.empty_like(t)
for i in nb.prange(len(t)):
result[i] = a0 + a1 * np.exp(-t[i] * a2)
return result

你也可以用同样的方法并行化雅可比矩阵。

相关内容

  • 没有找到相关文章

最新更新