使用分类数据作为sklean LogisticRegression中的功能

Opt*_*esh 10 python regression data-modeling scikit-learn logistic-regression

我想了解如何使用分类数据为特征sklearn.linear_modelLogisticRegression.

我理解当然我需要编码它.

  1. 我不明白的是如何将编码特征传递给Logistic回归,以便将其作为分类特征处理,而不是解释编码作为标准可量化特征时获得的int值.

  2. (不太重要)有人可以解释两者的区别preprocessing.LabelEncoder(),DictVectorizer.vocabulary或者只是一个简单的字典编码自己分类数据?Alex A.的评论涉及这个主题,但不是很深刻.

尤其是第一个!

Mat*_*unn 5

您可以为不同类别创建指标变量。例如:

animal_names = {'mouse';'cat';'dog'}

Indicator_cat = strcmp(animal_names,'cat')
Indicator_dog = strcmp(animal_names,'dog')
Run Code Online (Sandbox Code Playgroud)

然后我们有:

                [0                         [0
Indicator_cat =  1        Indicator_dog =   0
                 0]                         1]
Run Code Online (Sandbox Code Playgroud)

您可以将它们连接到原始数据矩阵上:

X_with_indicator_vars = [X, Indicator_cat, Indicator_dog]
Run Code Online (Sandbox Code Playgroud)

但请记住,如果数据矩阵中包含常数项,则不要留下一个没有指示符的类别!否则,您的数据矩阵将不会是完整的列秩(或者用计量经济学术语来说,您具有多重共线性)。

[1  1  0  0         
 1  0  1  0         
 1  0  0  1]        
Run Code Online (Sandbox Code Playgroud)

请注意常数项、鼠标指示符、猫指示符和狗指示符如何导致不完整的列秩矩阵:第一列是最后三列的总和。


Ibr*_*iev 3

  1. 将分类特征转换为数值的标准方法 - OneHotEncoding
  2. 这是完全不同的类:

    [DictVectorizer][2].vocabulary_

    将特征名称映射到特征索引的字典。

    即,在fit() DictVectorizer拥有所有可能的特征名称之后,现在它知道将在哪个特定列中放置特征的特定值。因此DictVectorizer.vocabulary_包含特征指标,但不包含值。

    LabelEncoder相反,将每个可能的标签(标签可以是字符串或整数)映射到某个整数值,并返回这些整数值的一维向量。