首页 > 解决方案 > 在 statsmodels 中,形状未对齐到完全相同的形状

问题描述

我正在使用pandas数据框和系列作为测试和训练数据。我正在检查我的火车数据框和测试数据框的形状,它们完全一样。但我仍然有shapes not aligned错误。这是我的拟合/预测代码:

train_df = df.loc[:50]
X_train = train_df[["Value", "Momentum", "Quality", "MinimumVolatility"]]
y_train = train_df["P1ExRe"]

X_train = sm.add_constant(X_train)

model = sm.OLS(y_train, X_train)
results = model.fit()
test_df = df.loc[51:100]
x_test = test_df[["Value", "Momentum", "Quality", "MinimumVolatility"]]
y_test = test_df["P1ExRe"]

print(x_test.shape==X_train.shape)
model.predict(x_test)

这是错误:

    ValueError                                Traceback (most recent call last)
<ipython-input-108-832ad1f6bc61> in <module>
      4 
      5 print(x_test.shape==X_train.shape)
----> 6 model.predict(x_test)

~/projects/courserads/venv/lib/python3.6/site-packages/statsmodels/regression/linear_model.py in predict(self, params, exog)
    378             exog = self.exog
    379 
--> 380         return np.dot(exog, params)
    381 
    382     def get_distribution(self, params, scale, exog=None, dist_class=None):

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (50,5) and (50,5) not aligned: 5 (dim 1) != 50 (dim 0)

标签: pythonpandasstatsmodels

解决方案


您正在使用该model.predict方法。你应该使用results.predict(...).

模型 predict 需要params,因为只有结果具有估计的参数。

x_testin model.predict 被解释为params并导致形状不匹配。


推荐阅读