【问题标题】:Scikit Learn : Logistic Regression ErrorScikit Learn:逻辑回归错误
【发布时间】:2016-02-21 01:00:14
【问题描述】:

我正在尝试使用 scikit learn 对我的数据(6 个分类,1 个整数)运行逻辑回归。我正在关注 scikit 学习文档,但是在尝试拟合我的数据时,我收到以下值错误。有人可以帮忙吗?

#Below are the variables of my data.
train_data.dtypes
    OUTPUT
    TripType                 category
    VisitNumber              category
    Weekday                  category
    Upc                      category
    ScanCount                   int64
    DepartmentDescription    category
    FinelineNumber           category
    dtype: object


X = train_data.loc[:, 'VisitNumber':'FinelineNumber']
Y = train_data.loc[:, 'TripType':'TripType']
logreg = linear_model.LogisticRegression()
logreg.fit(X, Y)

**ValueError: could not convert string to float: GROCERY DRY GOODS**

【问题讨论】:

  • 这个错误似乎很有启发性——“DepartmentDescription”是一个字符串吗?
  • @polka DeparmentDescription 是一系列字符串,我使用train_data.DepartmentDescription = train_data.DepartmentDescription.astype('category') 将其转换为分类值

标签: python scikit-learn logistic-regression


【解决方案1】:

Scikit-learn 只能处理数字特征。有关如何处理您的案例的一些想法,请参阅 scikit-learn 文档中的 Encoding Categorical Features

【讨论】:

    【解决方案2】:

    您不能将类别名称直接用作逻辑回归中的特征。您需要将它们转换为一些编码向量(或虚拟变量)。如果您有 6 个类别,则需要使用 5 个虚拟变量。

    您可以查看以下 sklearn 包链接中的编码分类特征部分: http://scikit-learn.org/stable/modules/preprocessing.html

    【讨论】:

      猜你喜欢
      • 2012-06-27
      • 2017-03-31
      • 2018-03-01
      • 2016-07-31
      • 2016-06-17
      • 2016-03-04
      • 2018-09-23
      • 2018-08-17
      • 2013-09-30
      相关资源
      最近更新 更多