簡體   English   中英

得分Statsmodels Logit

[英]Score Statsmodels Logit

我似乎無法弄清楚邏輯回歸模型得分的語法。

logit = sm.Logit(data[response],sm.add_constant(data[features]))
model = logit.fit()
preds = model.predict(data[features])

這是我得到的追溯(抱歉丑陋的格式,不知道如何解決它......)


  2     logit = sm.Logit(data[response],sm.add_constant(data[features]))
  3     model = logit.fit()

----> 4 preds = model.predict(data [features])

878             exog = dmatrix(self.model.data.orig_exog.design_info.builder,
879                     exog)

- > 880返回self.model.predict(self.params,exog,* args,** kwargs)881 882

376             exog = self.exog
377         if not linear:

- > 378返回self.cdf(np.dot(exog,params))379 else:380返回np.dot(exog,params)

ValueError:矩陣未對齊

您將常量包含在估算中但不包括在預測中。

用於預測的解釋變量需要相同數量的變量,包括在估算中使用的常數:

preds = model.predict(sm.add_constant(data[features]))

向數據框添加常量列通常很有用,因此我們有一組一致的變量,包括常量。

相關:如果已在模型中使用公式接口,則還會在預測調用中執行一些自動轉換。

看起來您還需要將常量添加到predict方法中。 假設你正在使用熊貓,它可能會更容易

data['constant'] = 1

並將其添加到您的功能列表中。 或者,您可以使用statsmodels.formula.api.logit的公式界面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM