statsmodels 中的形状未与完全相同的形状对齐
Shapes not alligned for exaclty same shapes in statsmodels
我正在使用 pandas
数据帧和系列作为测试和训练数据。我正在检查我的火车数据框和测试数据框的形状,它们完全相同。但我仍然有 shapes not aligned
错误。这是我的 fit/predict 代码:
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)
您正在使用 model.predict
方法。你应该使用 results.predict(...)
.
模型预测需要 params
,因为只有结果具有估计参数。
您在 model.predict 中的 x_test
被解释为 params
并导致形状不匹配。
我正在使用 pandas
数据帧和系列作为测试和训练数据。我正在检查我的火车数据框和测试数据框的形状,它们完全相同。但我仍然有 shapes not aligned
错误。这是我的 fit/predict 代码:
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)
您正在使用 model.predict
方法。你应该使用 results.predict(...)
.
模型预测需要 params
,因为只有结果具有估计参数。
您在 model.predict 中的 x_test
被解释为 params
并导致形状不匹配。