具有分类输出的逻辑回归 sklearn

Logistic Regression sklearn with categorical Output

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

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

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

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

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

来自sklearn.linear_model.LogisticRegression

In the multiclass case, the training algorithm uses the one-vs-rest (OvR) scheme if the ‘multi_class’ option is set to ‘ovr’, and uses the cross-entropy loss if the ‘multi_class’ option is set to ‘multinomial’. (Currently the ‘multinomial’ option is supported only by the ‘lbfgs’, ‘sag’, ‘saga’ and ‘newton-cg’ solvers.)

代码示例:

# 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