简体   繁体   中英

Exponential Function Fit to Scatter Plot in Python

I'm not sure why this function won't fit to the data. If someone can point me in the right direction that would be super helpful. Thanks: Here's the code and plot:

from scipy.optimize import curve_fit
def func(x, a, b, c):
    return -a * np.exp(-b * x) + c

popt, pcov = curve_fit(func, phases, vels, bounds=([1, .1, -5000], [10, 2, 0]))
plt.scatter(phases, vels)
phase2 = np.linspace(90,400,num=1000)
plt.plot(phase2, func(phase2, *popt), 'r-')
plt.show()

Plot with data and fit:

phases:
[234.3692578, 347.2157865, 416.122074, 114.2758026, 267.3525965, 105.3502091, 118.3264626, 271.046984, 272.0451574, 338.9227713, 152.1191112, 146.76, 160.3954574, 226.5937143, 254.498976, 279.4143882, 202.9954395, 136.9392446, 151.8314991, 112.6287668, 185.2084578, 314.4599624, 126.3685106, 112.47, 132.38, 144.25, 144.089315, 152.8798147, 105.1975035, 114.4105431, 274.9182613, 282.8462458, 296.038016, 229.4133998, 229.9514097, 239.3765462, 239.9145561, 139.453362, 189.2786782, 207.2249829, 216.1383142, 234.97994, 242.0408139, 278.8905594, 327.3256417, 338.5709956, 256.1592914, 281.7220879, 181.5985583, 104.1277317, 131.6284923, 115.029687, 114.3586946, 118.3563139, 123.3151473, 141.1814304, 145.1026684, 153.9579407, 180.7707569, 120.6181777, 114.7435322, 121.5899368, 105.4976365, 105.7242335, 310.5930853, 118.3052086, 142.7487101, 171.5051916, 270.985632, 134.8910036, 212.1471114, 119.3923297, 119.5015514, 148.0779446, 383.1366457, 136.689375, 136.689375, 137.6830315, 137.6830315, 140.664001, 219.1628643, 219.1628643, 374.1732778, 199.51, 111.2741939, 143.0952324, 320.099759, 100.5024876, 135.19, 161.68, 104.7708707, 112.6455042, 112.8524217, 224.8250223, 317.2254066, 118.2161054, 127.1624473, 142.0545581, 125.649415, 124.6681977, 135.5244714, 186.1572569, 186.1771402, 193.1064651, 125.6316899, 150.4188055, 274.6080538, 275.2427281, 357.6717998, 101.9181194, 113.8877175, 116.8366048, 121.8521996, 124.832913, 158.6481465, 301.1893017, 100.7152056, 153.29, 124.3055297, 305.526556, 101.2265847, 109.5131962, 114.4660693, 117.4497278, 135.2949893, 190.7482717, 105.1506266, 140.5240717, 155.3998154, 199.4792183, 111.210032, 114.2076339, 116.2060352, 119.2036371, 124.1996403, 126.1980416, 134.1916467, 210.5305755, 241.5057954, 262.4890088, 266.4858114, 295.4626299, 325.4386491, 347.4210631, 350.4186651, 383.3922862, 100.5917689, 115.5103137, 124.4614405, 150.3202514, 220.9346966, 259.7229129, 338.2939152, 365.1472958, 110.1963363, 112.1885864, 124.3413123, 125.1382124, 129.1227126, 129.1227126, 129.1227126, 132.2107004, 160.998715, 179.9250914, 418.995109, 422.9796093, 422.9796093]

vels:
[-1523.215458, -292.3123381, 1783.735836, -1721.78392, -1032.351706, -4003.568985, -4146.412044, -1309.511074, -1287.168078, -1230.078559, -2677.982908, -2438.466234, -2363.137505, -2262.920989, -1781.094157, -1656.092363, -1946.78311, -1795.174691, -1393.875456, -3005.008036, -1994.70397, -2230.80794, -3041.47676, -2066.530586, -1791.857029, -1541.89944, -2522.390699, -2326.680197, -3681.674912, -3130.377473, -787.3757801, -528.7545665, -1179.597242, -1823.216385, -1823.14022, -1575.998188, -1576.449602, -2820.457868, -2068.014687, -1824.518119, -1638.409597, -1513.782304, -1531.012159, -1127.540354, -172.5201222, -647.0536519, -899.9553126, -660.8149826, -923.1653867, -3183.193728, -2835.654321, -2527.25102, -3063.284718, -3096.048325, -2929.295714, -2412.346897, -2078.085025, -1999.355947, -1731.245086, -3155.549269, -3274.887528, -2708.211645, -3106.20535, -3106.547572, -89.54104345, -3980.589927, -3717.284715, -3449.792978, -497.5478602, -2658.175173, -3340.360913, -3274.414439, -3315.526948, -2879.588904, 1355.440479, -4486.752833, -2551.101192, -4910.506907, -2846.489535, -2667.866316, -1362.762487, -1362.78039, 603.762725, -1316.73516, -3761.959519, -3359.648081, -779.4289079, -3067.894699, -2702.412496, -2125.841792, -3741.697885, -3737.924795, -3944.496736, -1720.10751, -924.6217128, -3973.864799, -3493.932426, -2120.476018, -4028.089266, -3301.521895, -3100.580801, -2379.490737, -2253.842659, -1911.066182, -1494.690862, -808.3964185, -2045.951539, -487.596456, -3233.869815, -3370.480653, -3000.864418, -2965.374848, -2754.722235, -2834.875036, -2109.795178, -922.096443, -4602.961557, -1507.978985, -4051.584501, -746.0584669, -4519.903561, -2607.773714, -2347.675403, -2422.259305, -2218.683696, -1484.162757, -3052.918961, -799.146157, -683.7247306, -1777.846881, -4578.501502, -4543.181516, -4459.674986, -4379.842989, -4205.490729, -4140.040102, -3860.742871, -2475.532748, -2127.699918, -1642.517106, -1764.746124, -1255.507245, -1011.976055, -932.7056409, -519.3680458, -583.2534339, -2737.339591, -2349.937591, -1932.12695, -1783.429796, -1246.94678, -871.250397, -888.834142, -976.6466722, -3005.743245, -3008.035753, -2879.531139, -2611.953796, -2753.222132, -3074.055596, -2753.22125, -2815.811302, -2821.650714, -1829.969079, 1652.067508, 2262.566418, 1672.47848]

Your doing everything fine, but the curve_fit is having trouble converging because it doesn't know where to look. I saw you tried to narrow the right parameters by forcing them to have some bounds. This is like finding a needle in a haystack. The problem is that the computer, floating point arithmetic, etc. simply don't know what values are important to your data and what is not important. Long story short, if we normalize your data, then that greatly helps it. You can run the normalized parameters backwards if you want the equation to work on the un-normalized data.

from scipy.optimize import curve_fit 

def func(x, a, b, c): 
    return -a * np.exp(-b * x) + c 
norm_phases = (phases - phases.mean())/phases.std() 
norm_vels   = (vels   - vels.mean())/vels.std() 

popt, pcov = curve_fit(func, norm_phases, norm_vels,bounds=(0,[3,1,5])) 
plt.scatter(norm_phases,norm_vels) 
phase2 = np.linspace(-1.2,3,num=10000) 
plt.plot(phase2, func(phase2, *popt), 'r-') 
plt.show() 

Most algorithms are optimized for normal looking data. Something with really large or small values is going to throw it off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM