【问题标题】:Using Categorical Predictor Variables in sci-kit learn在 scikit learn 中使用分类预测变量
【发布时间】:2019-06-05 21:13:02
【问题描述】:

这里的基本问题:

我正在尝试实现一个简单的信用卡默认分类模型,我只在输入数据上使用model.fit、model.predict。但是,该输入数据既包含分类数据(例如年龄、已婚或未婚、教育程度等人口统计信息),也包含连续数据(例如学分余额)。

data.info()

<div class="output"><div class="output_area"><div class="run_this_cell"></div><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>&lt;class 'pandas.core.frame.DataFrame'&gt;
Int64Index: 30000 entries, 1 to 30000
Data columns (total 24 columns):
LIMIT_BAL    30000 non-null float64
SEX          30000 non-null int64
EDUCATION    30000 non-null int64
MARRIAGE     30000 non-null int64
AGE          30000 non-null int64
PAY_1        30000 non-null int64
PAY_2        30000 non-null int64
PAY_3        30000 non-null int64
PAY_4        30000 non-null int64
PAY_5        30000 non-null int64
PAY_6        30000 non-null int64
BILL_AMT1    30000 non-null float64
BILL_AMT2    30000 non-null float64
BILL_AMT3    30000 non-null float64
BILL_AMT4    30000 non-null float64
BILL_AMT5    30000 non-null float64
BILL_AMT6    30000 non-null float64
PAY_AMT1     30000 non-null float64
PAY_AMT2     30000 non-null float64
PAY_AMT3     30000 non-null float64
PAY_AMT4     30000 non-null float64
PAY_AMT5     30000 non-null float64
PAY_AMT6     30000 non-null float64
default      30000 non-null int64
dtypes: float64(13), int64(11)
memory usage: 5.7 MB
</pre></div></div></div>

据我了解,scikit-learn 要求所有数据都是数字和连续的,或者专门编码为分类变量。数字部分不是问题,因为我的所有数据都是用数字编码的(比如 0 表示已婚,1 表示未婚),但我的 3 个变量(SEX、EDUCATION 和 MARRIAGE)是名义/有序的,需要编码为分类变量而不是 int64 的。

如何使用 scikit-learn 的预处理模块对这 3 个变量进行编码,以将这些特征正确地输入到逻辑回归等模型中?

提前致谢,请原谅格式(随时编辑或推荐我如何正确地将 Jupyter Notebook 输出包含到 Stack Overflow 帖子中)。

【问题讨论】:

  • 您在哪里看到数据连续的要求?您是在谈论分类问题与回归问题之间的区别吗?一旦它被数字编码,它就可以被输入到模型中。您是否尝试过,遇到的问题或错误是什么?
  • 嗯,我想问题出在编码上。现在,我所有的特征都编码为 float64 或 int64,但我认为我需要将所有 int64 编码为 sklearn-preprocessing 的类别。我从 Datacamp 的监督学习课程中得到了数据必须是连续的要求。但我还在scikit-learn's documentation 上发现分类变量的整数表示不起作用。
  • @G.Anderson,感谢您的反馈。为了清楚起见,我将编辑问题。
  • 这更有意义!由于分类变量已经是数值型的,只需使用内置的One-Hot encoder 将变量转换为 one-hot 列

标签: python machine-learning scikit-learn classification categorical-data


【解决方案1】:

分类功能需要更多关注特征工程,因为特征等年龄,日期等难以编码。通过分析,域名知识和更多的方式,有许多方法来编码这些功能。

有一个库 category_encoders ,它通过使用统计来编码这些功能的许多功能。您可以在这里找到http://contrib.scikit-learn.org/categorical-encoding/

在这里,另一个好resource,它将通过一个例子显示使用编码方法。

【讨论】:

  • gideon,如果解决问题,请接受它。 span>
猜你喜欢
  • 2015-01-12
  • 2021-08-30
  • 2015-04-08
  • 2015-03-01
  • 2022-09-27
  • 2015-03-05
  • 2017-12-22
  • 2017-09-19
  • 2020-06-21
相关资源
最近更新 更多