ValueError:endog 和 exog 矩阵的大小不同 - 如何仅删除特定列中的数据?

我正在尝试运行多变量回归并收到错误:


“ValueError:endog 和 exog 矩阵的大小不同”


我的代码片段如下:


df_raw = pd.DataFrame(data=df_raw)


y = (df_raw['daily pct return']).astype(float)

x1 = (df_raw['Excess daily return']).astype(float)

x2 = (df_raw['Excess weekly return']).astype(float)

x3 = (df_raw['Excess monthly return']).astype(float)

x4 = (df_raw['Trading vol / mkt cap']).astype(float)

x5 = (df_raw['Std dev']).astype(float)

x6 = (df_raw['Residual risk']).astype(float)


y = y.replace([np.inf, -np.inf],np.nan).dropna()


print(y.shape)

print(x1.shape)

print(x2.shape)

print(x3.shape)

print(x4.shape)

print(x5.shape)

print(x6.shape)



df_raw.to_csv('Raw_final.csv', header=True)


result = smf.OLS(exog=y, endog=[x1, x2, x3, x4, x5, x6]).fit()

print(result.params)

print(result.summary())

正如您从我的代码中看到的,我正在检查每个变量的“形状”。我得到以下输出,表明错误的原因是 y 变量只有 48392 个值,而所有其他变量都有 48393:


(48392,) (48393,) (48393,) (48393,) (48393,) (48393,) (48393,)


有没有人有一个优雅的解决方案来对齐矩阵的大小,所以我不再收到这个错误?我想我需要从 y 变量('daily pct return')中删除第一行值 APART 但我不确定如何实现这一点?


倚天杖
浏览 164回答 2
2回答

素胚勾勒不出你

我假设您想丢弃与无穷大的 y 值相关的所有数据。df_raw = pd.DataFrame(data=df_raw)df_raw['daily pct return']) = df_raw['daily pct return']).astype(float).replace([np.inf, -np.inf],np.nan)df_raw = df_raw.dropna()然后按照您的意愿进行回归。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python