简体   繁体   English

statsmodels.api和scipy.stats无法产生适当的拟合度

[英]statsmodels.api and scipy.stats not producing proper fit

I'm trying to plot a line of best fit through two sets of data with scipy.stats and statsmodels.api . 我试图通过scipy.statsstatsmodels.api通过两组数据绘制一条最合适的线。

import matplotlib.pyplot as plt
import numpy as np
import statsmodels.api as sm
from scipy import stats

# toy data
y1 =  np.array([1,2,3,4,5])
x1 =  np.array([2,4,6,8,10])
y2 = np.array([1,3.0,5.0,7.0,9.0])
x2 = np.array([1,2.9,5.3,7.4,8.9])

#  should produce straight lines through each data set
plt.scatter(x1, y1, label = 'LRIS')
plt.scatter(x2, y2, label = 'PFCam')
for x, y in zip([x1, x2], [y1, y2]):
    model = sm.OLS(y, sm.add_constant(x))
    results = model.fit()
    params = stats.linregress(x, y)
    plt.plot(params[0]*x + params[1])

plt.xlabel('log Integration time, t [s]')
plt.ylabel('V [mag]')
plt.legend()
plt.show()

produces 产生

在此处输入图片说明

I don't understand what's going on to produce the lines of 'best' fit like this. 我不明白发生了什么事才能产生这样的“最佳”适应。

You wanted to plot X vs Y: 您想绘制X与Y:

    plt.plot(x, x * params.slope + params.intercept)

LGTM. LGTM。

最佳拟合线性模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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