Python中的zeroinflatedpoisson模型 [英] zeroinflatedpoisson model in python

查看:525
本文介绍了Python中的zeroinflatedpoisson模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 python3 构建零膨胀泊松模型。我在库 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 = [i在范围(200)中的random.randint(1,2)]
y = np.random.poisson(lam = 2,size = 100).tolist() i在范围(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:操作数不能与形状(140,)一起使用60,)

也尝试过:

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

ValueError:shapes(60,)和(1,)不对齐: 60(dim 0)!= 1(dim 0)


解决方案



据我所知:



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



围绕在预测中指定长度正确的一维数组的工作应该起作用。



尝试:

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

我猜如果模型中使用了曝光量,但是在预测中未明确指定,则会发生相同的问题。


I want to use python3 to build a zeroinflatedpoisson model. I found in library statsmodel the function statsmodels.discrete.count_model.ZeroInflatePoisson.
I just wonder how to use it. It seems I should do:
ZIFP(Y_train,X_train).fit().
But when I wanted to do prediction using X_test.
It told me the length of X_test doesn't fit X_train. Or is there another package to fit this model? Here is the code I used:

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,)

also tried:

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

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

解决方案

This looks like a bug to me.

As far as I can see:

If there are no explanatory variables, exog_infl, specified for the inflation model, then a array of ones is used to model a constant inflation probability. However, if exog_infl in predict is None, then it uses the model.exog_infl which is an array of ones with the length equal to the training sample.

As work around specifying a 1-D array of ones of correct length in predict should work.

Try:

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

I guess the same problem will occur if exposure was used in the model, but is not explicitly specified in predict.

这篇关于Python中的zeroinflatedpoisson模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆