Python:如何使用 SKlearn 使用多项 Logistic 回归

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36760000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:17:57  来源:igfitidea点击:

Python : How to use Multinomial Logistic Regression using SKlearn

pythonscikit-learnlogistic-regressiondata-analysis

提问by Sriram Chandramouli

I have a test dataset and train dataset as below. I have provided a sample data with min records, but my data has than 1000's of records. Here E is my target variable which I need to predict using an algorithm. It has only four categories like 1,2,3,4. It can take only any of these values.

我有一个测试数据集和训练数据集,如下所示。我提供了一个包含最少记录的示例数据,但我的数据有超过 1000 条记录。这里 E 是我需要使用算法预测的目标变量。它只有四个类别,如 1、2、3、4。它只能采用这些值中的任何一个。

Training Dataset:

训练数据集:

A    B    C    D    E
1    20   30   1    1
2    22   12   33   2
3    45   65   77   3
12   43   55   65   4
11   25   30   1    1
22   23   19   31   2
31   41   11   70   3
1    48   23   60   4

Test Dataset:

测试数据集:

A    B    C    D    E
11   21   12   11
1    2    3    4
5    6    7    8 
99   87   65   34 
11   21   24   12

Since E has only 4 categories, I thought of predicting this using Multinomial Logistic Regression (1 vs Rest Logic). I am trying to implement it using python.

由于 E 只有 4 个类别,因此我想到使用多项 Logistic 回归(1 与 Rest 逻辑)来预测这一点。我正在尝试使用 python 来实现它。

I know the logic that we need to set these targets in a variable and use an algorithm to predict any of these values:

我知道我们需要在变量中设置这些目标并使用算法来预测这些值中的任何一个的逻辑:

output = [1,2,3,4]

But I am stuck at a point on how to use it using python (sklearn) to loop through these values and what algorithm should I use to predict the output values? Any help would be greatly appreciated

但是我在如何使用 python (sklearn) 循环遍历这些值以及我应该使用什么算法来预测输出值方面遇到了问题?任何帮助将不胜感激

回答by dukebody

LogisticRegressioncan handle multiple classes out-of-the-box.

LogisticRegression可以开箱即用地处理多个类。

X = df[['A', 'B', 'C', 'D']]
y = df['E']
lr = LogisticRegression()
lr.fit(X, y)
preds = lr.predict(X)  # will output array with integer values.

回答by Daisy Qin

You could try

你可以试试

LogisticRegression(multi_class='multinomial',solver ='newton-cg').fit(X_train,y_train)