我目前正在尝试在 Python 中实现 MLR,但不确定如何将我找到的系数应用于未来值。
import pandas as pd
import statsmodels.formula.api as sm
import statsmodels.api as sm2
TV = [230.1, 44.5, 17.2, 151.5, 180.8]
Radio = [37.8,39.3,45.9,41.3,10.8]
Newspaper = [69.2,45.1,69.3,58.5,58.4]
Sales = [22.1, 10.4, 9.3, 18.5,12.9]
df = pd.DataFrame({'TV': TV,
'Radio': Radio,
'Newspaper': Newspaper,
'Sales': Sales})
Y = df.Sales
X = df[['TV','Radio','Newspaper']]
X = sm2.add_constant(X)
model = sm.OLS(Y, X).fit()
>>> model.params
const -0.141990
TV 0.070544
Radio 0.239617
Newspaper -0.040178
dtype: float64
假设我想预测以下 DataFrame 的“销售额”:
EDIT
TV Radio Newspaper Sales
230.1 37,8 69.2 22.4
44.5 39.3 45.1 10.1
... ... ... ...
25 15 15
30 20 22
35 22 36
我一直在尝试在这里找到的方法,但我似乎无法让它发挥作用:使用 Pandas OLS 进行预测 https://stackoverflow.com/questions/9943848/forecasting-using-pandas-ols
谢谢你!