【问题标题】:Error while converting continuous data to categorical data in Logistic Regression在 Logistic 回归中将连续数据转换为分类数据时出错
【发布时间】:2020-03-22 06:13:29
【问题描述】:

我正在对我的数据集使用逻辑回归,该数据集的目标变量为 0 和 1。我使用了 .replace() 函数并相应地替换了它们。

> data['target']=data['target'].replace({0:"No",1:"yes"})

代码运行良好。但是当我对数据进行建模时,

model_log=sm.Logit(data['target'],data.iloc[:,2:]).fit()

它显示以下错误:

ValueError: Pandas 数据转换为对象的 numpy dtype。检查输入 带有 np.asarray(data) 的数据。

【问题讨论】:

  • Logit 要求因(目标)变量是 {0, 1} 中的数值(整数或浮点数)或在区间 [0, 1] 中浮点数,以获得准最大似然。

标签: casting data-science logistic-regression modeling


【解决方案1】:

当您使用iloc 选择 X 数据时,它会返回一个 pandas 数据框。According to statsmodel documentation,logit 期望 X 和 y 类似于数组。您需要将数据框转换为所需的数据类型。您可以使用to_numpy方法将数据框转换为numpy数组。

model_log=sm.Logit(data['target'].astype(float),data.iloc[:,2:].to_numpy()).fit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 2019-04-28
    • 2021-10-24
    • 2015-07-30
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    相关资源
    最近更新 更多