문제

I would like to create a simple linear regression chart just like in excel. With the shortest way possible.

Which is the easiest way to to plot a stock returns chart with a regression line using the pandas .plot ?

도움이 되었습니까?

해결책

It would be pretty simple with statsmodels

import statsmodels.api as sm
mod = sm.OLS.from_formula('y ~ x', data=df)  # y and x are column names in the DataFrame
res = mod.fit()
fig, ax = plt.subplots()
sm.graphics.abline_plot(model_results=res, ax=ax)
df.plot(kind='scatter', x='x', y='y', ax=ax)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top