【问题标题】:Why do I receive this numpy error when using statsmodels to predict test values?为什么在使用 statsmodels 预测测试值时会收到此 numpy 错误?
【发布时间】:2026-02-16 22:15:01
【问题描述】:

我在尝试使用 statsmodels .predict 预测我的测试值时遇到错误。

代码:

X_train, X_test, y_train, y_test = train_test_split(X_new_np, y, test_size=0.2, random_state=42)
logit = sm.Logit(y_train, X_train)
reg = logit.fit_regularized(start_params=None, method='l1_cvxopt_cp', maxiter= 1000, full_output=1, disp=1, callback=None, alpha=.01, trim_mode='auto', auto_trim_tol=0.01, size_trim_tol=0.0001, qc_tol=0.03)
reg.summary()
y_pred_test = logit.predict(X_test)

错误:

ValueError: shapes (1000,61) and (251,61) not aligned: 61 (dim 1) != 251 (dim 0)

【问题讨论】:

  • 完整的回溯会有所帮助。但是当尺寸不正确时,np.dot 会引发这种错误。如错误消息所示,第二个参数应具有形状 (61,251)。必须从回溯中推断出如何回溯到您的代码。
  • 谢谢!是的,我确实理解这是一个线性代数问题,因为内部尺寸不匹配,所以矩阵不能相乘。我只是不知道为什么它们不匹配。

标签: python numpy statsmodels logits


【解决方案1】:

您根本无法根据正确的对象进行预测。 reg 是已安装的那个,然后您应该使用 reg.predict。下面的代码运行没有错误(我使用了你的 fit_regularized 参数)。

from sklearn.model_selection import train_test_split
import numpy as np
from statsmodels.api import Logit

x = np.random.randn(100,50)
y = np.random.randint(0,2,100).astype(bool)

print(x.shape, y.shape)

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=.2)

logit = Logit(y_train, X_train)
reg = logit.fit_regularized(start_params=None, method='l1_cvxopt_cp',
        maxiter= 1000, full_output=1, disp=1, callback=None,
        alpha=.01, trim_mode='auto', auto_trim_tol=0.01,
        size_trim_tol=0.0001, qc_tol=0.03)
print(reg.summary())
y_pred_test = reg.predict(X_test)

【讨论】:

  • 谢谢。就是这个!