1

I'm trying to fit some linear lines to curves, they behave linearly near 0 and then do odd things. But I need their behavior around zero. Now I've written a little script, and it does not work at all for some curves, while it does others perfectly. And I'm not sure why, could you help?

def linfit(mode): #linfit a line trough 3 dots around 0 x = [-2,-1,0,1,2] #Grab a few points around 0 and fit a line y = [1,2,3,4,5] #put the middle 5 values from input into y y[0] = mode[8] y[1] = mode[9] y[2] = mode[10] y[3] = mode[11] y[4] = mode[12] print(x,y) fit = np.polyfit(x,y,1) #make a fit xfit = np.arange(-10,11)/10.0 #save the fit line = fit[0]*xfit + fit[1] #plot the line plt.plot(mode) #compare to imput plt.plot(line) plt.show() return fit #output parameters 

This is the array I put in:

2.863301071014881134e+06 2.460245261639075354e+06 2.093026012088939082e+06 1.760246916946319165e+06 1.459359399934661807e+06 1.186759278075916693e+06 9.379136078178760363e+05 7.075090289232722716e+05 4.896215309857819229e+05 2.779015818236746127e+05 6.577018238002381986e+04 -1.533787985483088996e+05 -3.859754316150106606e+05 -6.380747491954336874e+05 -9.151669400838417932e+05 -1.222001861839911900e+06 -1.562432032544140937e+06 -1.939277663402933627e+06 -2.354219490472638980e+06 -2.807717568478100933e+06 -3.298964879534429871e+06 

http://i.imgur.com/ObE5hfj.png

But some curves it fits great..and Idk why. enter image description here

Here is the full dataset of 12 fits, and only a few of them fit perfectly, while about 5 don't fit at all.

2.863301071014881134e+06,2.460245261639075354e+06,2.093026012088939082e+06,1.760246916946319165e+06,1.459359399934661807e+06,1.186759278075916693e+06,9.379136078178760363e+05,7.075090289232722716e+05,4.896215309857819229e+05,2.779015818236746127e+05,6.577018238002381986e+04,-1.533787985483088996e+05,-3.859754316150106606e+05,-6.380747491954336874e+05,-9.151669400838417932e+05,-1.222001861839911900e+06,-1.562432032544140937e+06,-1.939277663402933627e+06,-2.354219490472638980e+06,-2.807717568478100933e+06,-3.298964879534429871e+06 1.823024837501582457e+06,1.407404888380297925e+06,1.045542366034140810e+06,7.369900890351813287e+05,4.796441426606248133e+05,2.698286365673191613e+05,1.024250849728439935e+05,-2.896099883565749042e+04,-1.317982424403309124e+05,-2.143990311263155891e+05,-2.856664656426848378e+05,-3.548254775850749575e+05,-4.311454552569908556e+05,-5.236618570110450964e+05,-6.409042855782526312e+05,-7.906385211718138307e+05,-9.796293207744800020e+05,-1.213430110346697038e+06,-1.496206498148957733e+06,-1.830595814738985617e+06,-2.217610022593144793e+06 4.798691891424674541e+06,4.335460195711855777e+06,3.862336577153687831e+06,3.381052381723834202e+06,2.893223182883592322e+06,2.400343388215905987e+06,1.903789665149280569e+06,1.404825854079108685e+06,9.046142340220035985e+05,4.042298400829117745e+05,-9.532238749372726306e+04,-5.930878206153763458e+05,-1.088141074660916813e+06,-1.579564367707776371e+06,-2.066426035502505023e+06,-2.547760585996538866e+06,-3.022550547782572918e+06,-3.489710436714207288e+06,-3.948076268974626437e+06,-4.396395672050636262e+06,-4.833326583355414681e+06 3.793703552110599820e+06,3.391412198827212211e+06,2.989782693296195008e+06,2.589578770337372553e+06,2.191246189844559878e+06,1.794929647279789671e+06,1.400503089901715051e+06,1.007606002048938535e+06,6.156892334155119024e+05,2.240665530861769803e+05,-1.680294492848222144e+05,-5.613882603051033802e+05,-9.567615461140368134e+05,-1.354804212866113055e+06,-1.756018122938997578e+06,-2.160700877420521341e+06,-2.568900574604218360e+06,-2.980377833163299598e+06,-3.394578734188971575e+06,-3.810614864194200840e+06,-4.227257391775649041e+06 -2.688736622001025826e+05,-2.891307096734186634e+05,-3.210182462179567665e+05,-3.637685228908676654e+05,-4.161451769185513258e+05,-4.764690694206450135e+05,-5.426589879127629101e+05,-6.122883688792278990e+05,-6.826550662752185017e+05,-7.508627579625872895e+05,-8.139109218074735254e+05,-8.687908509623678401e+05,-9.125844099162584171e+05,-9.425623182375114411e+05,-9.562787566806860268e+05,-9.516588113849731162e+05,-9.270761900481916964e+05,-8.814180572800580412e+05,-8.141346932859588414e+05,-7.252721974752144888e+05,-6.154868653471516445e+05 9.290761216964293271e+04,9.716293103619851172e+04,1.066760295451972634e+05,1.210595604704506695e+05,1.398309263398554176e+05,1.624331190057452768e+05,1.882570907542696223e+05,2.166650927380658686e+05,2.470136447085617110e+05,2.786755592228779569e+05,3.110599570438275114e+05,3.436296166989142075e+05,3.759150219994084910e+05,4.075246579653825611e+05,4.381512495859954506e+05,4.675738988737529144e+05,4.956561688274089247e+05,5.223402258499907330e+05,5.476378741131518036e+05,5.716185480071883649e+05,5.943952403778489679e+05 -1.526902880737665109e+05,-1.735365197676238604e+05,-1.890024515842027031e+05,-1.990524921999722719e+05,-2.037516828224882483e+05,-2.032633730402099900e+05,-1.978446369726238772e+05,-1.878397246057786979e+05,-1.736716232176218182e+05,-1.558319302106322721e+05,-1.348693471926613711e+05,-1.113770128403031267e+05,-8.597902396752825007e+04,-5.931646757646976039e+04,-3.203331006132997572e+04,-4.762504500862210989e+03,2.188737932290835306e+04,4.734455955763859674e+04,7.108546047641383484e+04,9.264432113033020869e+04,1.116216124380915426e+05 2.079714810232995078e+05,2.360356890453444794e+05,2.643386878711422905e+05,2.926172451146300882e+05,3.205867588270269334e+05,3.479488338805735111e+05,3.743995507517913356e+05,3.996394133776463568e+05,4.233835349200218916e+05,4.453720293387128040e+05,4.653802901815772057e+05,4.832283492548773065e+05,4.987890926566505805e+05,5.119948616758547723e+05,5.228420208745803684e+05,5.313934730113642290e+05,5.377785636607399210e+05,5.421904888370856643e+05,5.448816494497396052e+05,5.461558669662652537e+05,5.463592480976451188e+05 -6.004972233628863469e+05,-5.218144770968388766e+05,-4.323520639496566728e+05,-3.328308516886550933e+05,-2.241492617535209283e+05,-1.073722272343654186e+05,1.628341686249244958e+04,1.454689736557258293e+05,2.787269490527026355e+05,4.145172438386715949e+05,5.512458119609234855e+05,6.872943554893778637e+05,8.210504976007230580e+05,9.509374077493352816e+05,1.075441897909167223e+06,1.193140607877797447e+06,1.302723037819108926e+06,1.403011151727671735e+06,1.492975922453127801e+06,1.571748814718301408e+06,1.638630423184413929e+06 -8.224905728125246242e+05,-8.272410185064692050e+05,-8.326893845516629517e+05,-8.388871982868439518e+05,-8.458762596825547516e+05,-8.536853897637501359e+05,-8.623273033962328918e+05,-8.717957186641427688e+05,-8.820629007600517944e+05,-8.930777311472347938e+05,-9.047644510141359642e+05,-9.170221686316537671e+05,-9.297252067997995764e+05,-9.427243347298488952e+05,-9.558488991128550842e+05,-9.689098272587950341e+05,-9.817034558001877740e+05,-9.940160934147448279e+05,-1.005629199419648852e+06,-1.016325006415236276e+06,-1.025892532105713151e+06 -4.424117788771889172e+05,-5.102346394100966863e+05,-5.751149122867453843e+05,-6.363799527015131898e+05,-6.934574586782292463e+05,-7.458898594100442715e+05,-7.933441185955684632e+05,-8.356173786789262667e+05,-8.726374820720185526e+05,-9.044589545214013197e+05,-9.312544687950452790e+05,-9.533022778828768060e+05,-9.709702647911612876e+05,-9.846973581635965966e+05,-9.949731923205442727e+05,-1.002316828812014312e+06,-1.007255601753221359e+06,-1.010304709861830808e+06,-1.011948455100183841e+06,-1.012623895610535983e+06,-1.012707068021281622e+06 3.477817421297905967e+05,3.736243431702046655e+05,3.920268554006759077e+05,4.033072721675941721e+05,4.079090453006611206e+05,4.063751606468805112e+05,3.993197351276362315e+05,3.873984844536441378e+05,3.712793758027623408e+05,3.516147341198949143e+05,3.290160663752942346e+05,3.040326176762795076e+05,2.771345338446269743e+05,2.487012466697013006e+05,2.190154871324780397e+05,1.882628948148782365e+05,1.565372117093461566e+05,1.238505384397571906e+05,9.014778573495894670e+04,5.532475400785356760e+04,1.924863420119788498e+04 
4
  • so you want to fit x = [-2,-1,0,1,2] and y = [1,2,3,4,5] ??? your question is not clear. Commented Jun 11, 2014 at 19:55
  • y gets filled with yvalues from the input. the input goes from -10 to 10 and I want to fit the linear part around zero (so the middle 5 values of the array) So if you input the values which I provided, it will pick the middle 5 and put them into y and try to fit. (like in the curve) Commented Jun 11, 2014 at 20:13
  • when you say y gets filled with the array you mention, how come you have negative values? your input array seems to be positive Commented Jun 11, 2014 at 20:30
  • Oops, Had wrong values in there. I've put in the right ones now. (The ones that give the blue curve in the first pic) Commented Jun 11, 2014 at 20:34

2 Answers 2

1

I think you simply get carried away, why do you need that /10 for (in xfit = np.arange(-10,11)/10.0)? it is perfect without that /10, for the dataset you posted:

x = [-2,-1,0,1,2] #Grab a few points around 0 and fit a line y = mode[8:13] print(x,y) fit = np.polyfit(x,y,1) #make a fit xfit = np.arange(-10,11) #save the fit line = fit[0]*xfit + fit[1] #plot the line plt.plot(mode) #compare to imput plt.plot(line) 

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Well you can do this with curve_fit from scipy.

I have taken only the 5 points in your input array and performed the fit

import matplotlib.pyplot as plt import numpy as np from scipy.optimize import curve_fit x = np.linspace(0,15,5) # you can use your own x values y = np.array([4.896215309857819229e+05,2.779015818236746127e+05,6.577018238002381986e+04,-1.533787985483088996e+05,-3.859754316150106606e+05]) def fitfunc(x,p1,p2): return p1*x+p2 #since you asked for a linear fit, I used a basic fit function popt, pcov = curve_fit(fitfunc,x,y,p0=(1.0,2.0)) # Here p0 is the input parameters, which you can vary if needed p1=popt[0] #This is the first best fit parameter, i.e p1 p2=popt[1] #This is the second best fit parameter, i.e p2 fit_curve = fitfunc(x,p1,p2) #This is the red curve that you see in the plot, that fits your data plt.scatter(x,y) plt.plot(x,fit_curve,'r') plt.show() 

This is what I get, for your input values. enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.