【问题标题】:Logistic Regression sklearn with categorical Output具有分类输出的逻辑回归 sklearn
【发布时间】:2020-09-16 19:20:51
【问题描述】:

我必须在 sklearn 中使用逻辑回归训练模型。我到处都看到结果必须是二元的,但我的标签是好的、坏的或正常的。我有 12 个功能,但我不知道如何处理三个标签?非常感谢每一个回答

【问题讨论】:

  • @ShreyanshAgarwal scikit-learn 在标题、正文和标签中明确提及。
  • 是的,我知道,但我推荐他,因为他是新人,在标签中添加 Python 会使其更清晰,并且会增加覆盖面。

标签: machine-learning scikit-learn regression classification logistic-regression


【解决方案1】:

您可以使用 Multinomial Logistic Regression。 在 python 中,您可以将 Logistic 回归代码修改为:

LogisticRegression(multi_class='multinomial').fit(X_train,y_train)

您可以在 Scikit-Learn 中查看 Logistic Regression 文档以了解更多详细信息。

【讨论】:

    【解决方案2】:

    它被称为一对多分类或多类分类。

    来自sklearn.linear_model.LogisticRegression:

    在多类情况下,如果 'multi_class' 选项设置为 'ovr',则训练算法使用 one-vs-rest (OvR) 方案,如果设置了 'multi_class' 选项,则使用交叉熵损失到“多项式”。 (目前,“多项式”选项仅受“lbfgs”、“sag”、“saga”和“newton-cg”求解器支持。)

    代码示例:

    # Authors: Tom Dupre la Tour <tom.dupre-la-tour@m4x.org>
    # License: BSD 3 clause
    
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.datasets import make_blobs
    from sklearn.linear_model import LogisticRegression
    
    # make 3-class dataset for classification
    centers = [[-5, 0], [0, 1.5], [5, -1]]
    X, y = make_blobs(n_samples=1000, centers=centers, random_state=40)
    transformation = [[0.4, 0.2], [-0.4, 1.2]]
    X = np.dot(X, transformation)
    
    for multi_class in ('multinomial', 'ovr'):
        clf = LogisticRegression(solver='sag', max_iter=100, random_state=42,
                                 multi_class=multi_class).fit(X, y)
    
        # print the training scores
        print("training score : %.3f (%s)" % (clf.score(X, y), multi_class))
    

    查看完整代码示例:Plot multinomial and One-vs-Rest Logistic Regression

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 2015-12-27
      • 2020-07-04
      • 2021-03-11
      • 2020-09-23
      • 1970-01-01
      • 2018-02-06
      • 2015-05-04
      相关资源
      最近更新 更多