0

I had difficulty producing a continuous regression curve, with no splits as is currently the case.

from numpy import *
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.optimize import curve_fit
from sklearn.metrics import mean_squared_error
from matplotlib.pyplot import *
from scipy.interpolate import *

%matplotlib inline

x = array([28,47,43,40,42,37,64,48,47,45,58,38,45,38,50,42,59,40,49,38,52])
y = array([64,44,46,48,43,55,41,50,52,58,44,56,47,59,40,50,43,58,47,55,40])


p2 = polyfit(x,y,2)   # Polynoial curve, secend order


plot (x,y,'o')
plot (x,polyval(p2,x),'r-')

The printed plot is:

enter image description here

But all I need is just a normal curve, the red curve should be just one continuous curve, and not be split or divided as it is now. How can this be done?

(In the code snippet above this is printing a second order regression curve, but the problem also exists in a higher order (and in the first order))

I would appreciate any help.

Eli
  • 31
  • 1
  • 2
  • 1
    Please avoid `import *`. Many libraries have functions with similar or equal names. See e.g. [Why is import `*` bad?](https://stackoverflow.com/questions/2386714/why-is-import-bad) – JohanC Jun 10 '21 at 16:02
  • 2
    The back and forth lines are caused by `plot (x,polyval(p2,x),'r-')` where `x` isn't sorted. You need new x-values to be able to draw the curve also where there aren't input points. So, e.g. `xs = np.linspace(min(x), max(x), 500)` and then `plt.plot(xs, np.polyval(p2,xs))` – JohanC Jun 10 '21 at 16:05
  • See also the first answer of [What exactly does “import *” import?](https://stackoverflow.com/questions/2360724/what-exactly-does-import-import). – JohanC Jun 10 '21 at 16:13

0 Answers0