【发布时间】:2022-08-03 11:09:50
【问题描述】:
def fit_linear_regression(X, y):
X = sm.add_constant(X)
est = sm.OLS(y, X)
est = est.fit()
return est
print(X_train.shape) // outputs (604, 41)
print(X_test.shape) // outputs (95, 41)
model = fit_linear_regression(X_train.iloc[:, [0, 1, 2]], y_train)
model.predict(X_test.iloc[:, [0, 1, 2]])
当我运行此脚本时,出现以下错误
ValueError: shapes (95,3) and (4,) not aligned: 3 (dim 1) != 4 (dim 0)
当我不选择任何列而只包含整个数据框时,它与 shapes(95, 41) and (42,) 未对齐时相同。这到底是怎么回事?
X_train、y_train 和 y_test 是熊猫数据帧。
-
问题是在将
X_test数据传递给predict()函数之前,您没有向其添加常量。请参阅文档中的 this 示例,展示如何正确执行此操作。
标签: pandas statsmodels