تناسب بد؟(Bad Fit?)

اجازه دهید مثالی ایجاد کنیم که در آن رگرسیون خطی بهترین روش نباشد.
برای پیش بینی مقادیر آینده.




مثال


These values for the x- and y-axis should result in a very bad fit for linear
regression:



import matplotlib.pyplot as plt
from scipy import stats


x = [89,43,36,36,95,10,66,34,38,20,26,29,48,64,6,5,36,66,72,40]
y =
[21,46,3,35,67,95,53,72,58,10,26,34,90,33,38,20,56,2,47,15]

slope,
intercept, r, p, std_err = stats.linregress(x, y)

def
myfunc(x):
  return slope * x + intercept

mymodel = list(map(myfunc,
x))

plt.scatter(x, y)
plt.plot(x, mymodel)
plt.show()

Result:







و مقدار r-squared؟




مثال


You should get a very low r-squared value.



import numpy
from scipy import stats

x =
[89,43,36,36,95,10,66,34,38,20,26,29,48,64,6,5,36,66,72,40]
y =
[21,46,3,35,67,95,53,72,58,10,26,34,90,33,38,20,56,2,47,15]

slope, intercept, r,
p, std_err = stats.linregress(x, y)

print(r)






نتیجه: 0.013 یک رابطه بسیار بد را نشان می دهد و به ما می گوید که این مجموعه داده برای رگرسیون خطی مناسب نیست.