python - 具有 2 个类别的虚拟变量。它应该在单列还是多列中?

标签 python machine-learning scikit-learn

我正在研究一个分类问题,使用 Python 中的逻辑回归模型 (scikit learn)。我的特点之一是性别。在原始数据集中,该变量是一个字符串(male、female)。我使用 pandas 的 get_dummies 方法,它创建了 2 列,值分别为 0,1。每类一个。

我的问题是,我应该使用 2 个不同的列,还是像male (0,1) 这样的单个列?

最佳答案

我个人喜欢对具有 n 类别的字段使用 n - 1 列。当使用get_dummies时方法这意味着将 drop_first 设置为 True。

至于为什么我喜欢这样做;我的一位前任讲师在对 one hot encoding vs dummy encoding in sckikit learn 的回答中对此进行了很好的解释。 。基本上可以归结为消除共线性。

关于python - 具有 2 个类别的虚拟变量。它应该在单列还是多列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46836848/

相关文章:

python - 如何修复 SimpleImputer : "ValueError: Input contains NaN, infinity or a value too large for dtype"

python - 使用 sympy 分解出特定数字

python - 如何使用 requests python 模块登录 fidelity.com

python - 请求例如 : Recurrent neural network for predicting next value in a sequence

python - 在 scikit-learn 中使用 DecisionTreeClassifier 修复 100% 的准确率

python - Python 中带有文本数据的 MultiOutputClassifier

python - pandas str.split给我一个意想不到的语法错误

python - 在 Enthought Canopy IDE 中执行脚本时 matplotlib.figure() 不起作用

lua - torch 调整张量大小

javascript - 拟合时的 TensorflowJS,损失为 NaN