gpt4 book ai didi

python-3.x - python 中的零膨胀泊松模型

转载 作者:行者123 更新时间:2023-12-02 20:22:30 27 4
gpt4 key购买 nike

我想使用 python3 构建一个 zeroinflatedpoisson 模型。我在库 statsmodel 中找到函数 statsmodels.discrete.count_model.ZeroInflatePoisson
我只是想知道如何使用它。看来我应该这样做:
ZIFP(Y_train,X_train).fit().
但是当我想使用 X_test 进行预测时。
它告诉我 X_test 的长度不适合 X_train。或者是否有其他包装适合此型号?这是我使用的代码:

X1 = [random.randint(0,1) for i in range(200)]
X2 = [random.randint(1,2) for i in range(200)]
y = np.random.poisson(lam = 2,size = 100).tolist()
for i in range(100):y.append(0)
df['x1'] = x1
df['x2'] = x2
df['y'] = y
df_x = df.iloc[:,:-1]
x_train,x_test,y_train,y_test = train_test_split(df_x,df['y'],test_size = 0.3)
clf = ZeroInflatedPoisson(endog = y_train,exog = x_train).fit()
clf.predict(x_test)

ValueError:operands could not be broadcat together with shapes (140,)(60,)

也尝试过:

clf.predict(x_test,exog = np.ones(len(x_test)))

ValueError: shapes(60,) and (1,) not aligned: 60 (dim 0) != 1 (dim 0)

最佳答案

这对我来说像是一个错误。

据我所知:

如果没有为通货膨胀模型指定的解释变量 exog_infl,则使用一组 1 来模拟恒定的通货膨胀概率。但是,如果 predict 中的 exog_infl 为 None,则它使用 model.exog_infl,这是一个长度等于训练样本的数组。

因为在 predict 中指定一个正确长度的一维数组应该可行。

尝试:

clf.predict(test_x, exog_infl=np.ones(len(test_x))

我猜如果在模型中使用了 exposure,但没有在 predict 中明确指定,也会出现同样的问题。

关于python-3.x - python 中的零膨胀泊松模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50999793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com