微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我在使用 statsmodels 预测测试值时会收到这个 numpy 错误?

如何解决为什么我在使用 statsmodels 预测测试值时会收到这个 numpy 错误?

我在尝试使用 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)

解决方法

您只是没有根据正确的对象进行预测。 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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。