zeroinflatedpoisson model in python

a 夏天 提交于 2020-05-30 08:02:53

问题


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)

回答1:


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.




回答2:


I ran into the same problem, landing me on this thread. As noted by Josef, it seems like you need to provide exog_infl with a 1-D array of ones of correct length to work. However, the code Josef provided misses the 1-D array-part, so the full line required to generate the required array is actually

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


来源:https://stackoverflow.com/questions/50999793/zeroinflatedpoisson-model-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!