【问题标题】:How can I plot the results of Logit in statsmodel using matplotlib如何使用 matplotlib 在 statsmodel 中绘制 Logit 的结果
【发布时间】:2020-10-03 14:11:14
【问题描述】:

在这个数据集中,我有两个分类响应值(0 和 1),我想使用 statsmodels 拟合 Logit 模型。

X_incl_const = sm.add_constant(X)
model = sm.Logit(y, X_incl_const)
results = model.fit()
results.summary()

当我尝试使用以下代码绘制线和点时:

plt.scatter(X, y)
plt.plot(X, model.predict(X))

我收到以下错误:

    ValueError                                Traceback (most recent call last)
    <ipython-input-16-d69741b1f0ad> in <module>
          1 plt.scatter(X, y)
    ----> 2 plt.plot(X, model.predict(X))
    
    ~\Anaconda3\lib\site-packages\statsmodels\discrete\discrete_model.py in predict(self, params, exog, linear)
        461             exog = self.exog
        462         if not linear:
    --> 463             return self.cdf(np.dot(exog, params))
        464         else:
        465             return np.dot(exog, params)
    
    <__array_function__ internals> in dot(*args, **kwargs)
    
    ValueError: shapes (518,2) and (518,) not aligned: 2 (dim 1) != 518 (dim 0)

如何绘制此模型预测的预测线?

【问题讨论】:

    标签: python matplotlib data-visualization data-science statsmodels


    【解决方案1】:

    您的预测函数必须输入一个具有与拟合中使用的相同数量的列(或预测变量)的数组。此外,您应该在代码中使用拟合对象result,而不是model。使用示例数据集:

    from sklearn.datasets import load_breast_cancer
    import statsmodels.api as sm
    
    dat = load_breast_cancer()
    df = pd.DataFrame(dat.data,columns=dat.feature_names)
    df['target'] = dat.target
    X = df['mean radius']
    y = df['target']
    
    X_incl_const = sm.add_constant(X)
    model = sm.Logit(y, X_incl_const)
    results = model.fit()
    results.summary()
    

    合身一切都很好。现在,如果我们只是进行预测,就会出现与您看到的相同的错误:

    model.predict(X)
    
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-180-2558e7096c7c> in <module>
    ----> 1 model.predict(X)
          2 
          3 
    
    ~/anaconda2/lib/python3.7/site-packages/statsmodels/discrete/discrete_model.py in predict(self, params, exog, linear)
        482             exog = self.exog
        483         if not linear:
    --> 484             return self.cdf(np.dot(exog, params))
        485         else:
        486             return np.dot(exog, params)
    
    <__array_function__ internals> in dot(*args, **kwargs)
    
    ValueError: shapes (569,2) and (569,) not aligned: 2 (dim 1) != 569 (dim 0)
    

    我们添加常量截距,然后就可以了:

    plt.scatter(X,results.predict(sm.add_constant(X)))
    

    或者,如果您只绘制拟合值,请执行以下操作:

    plt.scatter(X,results.predict())
    

    【讨论】:

    • 比“相同维度的数组”更精确的是“具有相同列数的数组”。 (“维度”不明确)
    • 感谢您指出!是的,你是对的,'它应该是列
    【解决方案2】:

    查看您收到的错误:ValueError: shapes (518,2) and (518,) not aligned: 2 (dim 1) != 518 (dim 0)。它确切地说你的 X 是 518x2,意味着它有两个“列”(又名特征是二维的)。您不能将 1x1 维的散点图用于具有两个特征的数据。您一次只能绘制一个特征。

    提示:这就是为什么在 StackOverflow 上最好提供数据示例。因为现在,很难告诉你哪里错了:你的数据真的是二维的,还是只是你的代码上的一个错误?

    【讨论】:

    • 同意您关于提供数据的观点。错误来自运行model.predict()。 OP 提供了一个带有截距的数组,因此 (518,2) 用于拟合,并使用 (518,1) 进行预测,这是错误的。如果你做正确的预测,情节将起作用
    猜你喜欢
    • 2019-02-11
    • 2017-12-13
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 2023-04-08
    • 2016-01-27
    • 2013-09-04
    • 2023-03-25
    相关资源
    最近更新 更多