python 使用statsmodels调整线到点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用statsmodels调整线到点相关的知识,希望对你有一定的参考价值。

import statsmodels.api as sm
def fit_line2(x, y):
    """Return slope, intercept of best fit line."""
    X = sm.add_constant(x)
    model = sm.OLS(y, X, missing='drop') # ignores entires where x or y is NaN
    fit = model.fit()
    return fit.params[1], fit.params[0] # could also return stderr in each via fit.bse

x = diabetes_X_test[:,0]
y = diabetes_y_test
m, c = fit_line2(x, y)
yy = [m * x[i] + c for i in range(len(x))]
plt.plot(x,y,'bo',x,yy)

以上是关于python 使用statsmodels调整线到点的主要内容,如果未能解决你的问题,请参考以下文章