【问题标题】:zeroinflatedpoisson model in pythonpython中的zeroinflatedpoisson模型
【发布时间】:2018-06-23 09:26:31
【问题描述】:

我想使用 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)

【问题讨论】:

  • 显示您尝试过的实际代码。模型名称为 ZeroInflatedPoissonfitpredict 的工作方式与 statsmodels 中的其他模型相同,但添加了零通胀部分的选项。
  • train_x,test_x,train_y,test_y = train_test_split(data_x,data['y'],test_size = 0.3) clf = ZeroInflatedPoisson(endog = train_x, exog = train_y).fit() clf.predict (test_x) ValueError: 操作数不能与形状 (140,) (60,) 一起广播

标签: python-3.x data-analysis statsmodels


【解决方案1】:

这对我来说似乎是一个错误。

据我所知:

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

作为在 predict 中指定正确长度的一维数组的工作应该有效。

试试:

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

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

【讨论】:

  • 我试过了。它显示“ValueError:形状(60,)和(1,)未对齐:60(dim 0)!=(dim 0)”如果我在训练中为exog_infl对齐数组。每次做预测都要改吗?
  • 编辑您的问题并添加完整的回溯或至少显示形状不匹配的最后一部分。信息性回溯是 Python 的一大特色。
  • ZeroInflated 模型通常需要两个 exog,显然 exog_infl 为 None 的情况尚未正确处理。因此,在 statsmodels 修复此问题之前,将需要一个解决方法和完整的规范。 (ZeroInflated 是新的,仍然存在可用性错误,直到有足够的反馈来修复这些错误。)
  • 我更新了我的代码。我试过clf.predict(x_test,exog_infl = len(x_test)) 它有效。但我不知道 exog_infl 是否有意义。
  • 关于为什么 ZeroInflated 模型通常需要两个 exog 的任何解释?
【解决方案2】:

我遇到了同样的问题,让我进入了这个线程。正如 Josef 所指出的,您似乎需要为 exog_infl 提供一个长度正确的一维数组才能工作。 但是,Josef 提供的代码缺少一维数组部分,因此生成所需数组所需的整行实际上是

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

【讨论】:

    猜你喜欢
    • 2019-02-18
    • 1970-01-01
    • 2014-06-04
    • 2013-02-03
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多