python - 在 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)
解决方案
您正在使用该model.predict
方法。你应该使用results.predict(...)
.
模型 predict 需要params
,因为只有结果具有估计的参数。
您x_test
in model.predict 被解释为params
并导致形状不匹配。
推荐阅读
- apache-spark - 为什么我没有表格以在选择中获得排序结果?
- java - java.lang.ClassNotFoundException : com.mysql.jdbc.Driver 错误在执行以下代码时发生
- react-native - 有没有办法在使用 createBottomTabNavigator 和 createStackNavigator 并且同时使用 navigationOptions 时隐藏选项卡项?
- web - 如何在您有一个包含符号链接的 docroot 路径的地方添加一个 vhost 条目
- json - 有没有办法将 json 对象分类为正确的组
- c++ - 如何使用通过引用传递的值来更新以删除双向链表中的出现
- python - 如何使用 pyopenssl 生成带密码的 pem 密钥和证书?
- c++ - 如果我没有重载,为什么自定义类会出现“'operator='不匹配”?
- mysql - golang sql query timeout
- angular - 如何将输入搜索栏更改为自动完成搜索?