如何获得多项式逻辑回归的系数? [英] How to get coefficients of multinomial logistic regression?

查看:105
本文介绍了如何获得多项式逻辑回归的系数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用sklearn计算多元logistic回归的系数:

I need to calculate coefficients of a multiple logistic regression using sklearn:

X =

x1          x2          x3   x4         x5    x6
0.300000    0.100000    0.0  0.0000     0.5   0.0
0.000000    0.006000    0.0  0.0000     0.2   0.0
0.010000    0.678000    0.0  0.0000     2.0   0.0
0.000000    0.333000    1.0  12.3966    0.1   4.0
0.200000    0.005000    1.0  0.4050     1.0   0.0
0.000000    0.340000    1.0  15.7025    0.5   0.0
0.000000    0.440000    1.0  8.2645     0.0   4.0
0.500000    0.055000    1.0  18.1818    0.0   4.0

y 的值在[1;4].

y =

1
2
1
3
4
1
2
3

这就是我要做的:

import pandas as pd
from sklearn import linear_modelion
from sklearn.metrics import mean_squared_error, r2_score
import numpy as np

h = .02

logreg = linear_model.LogisticRegression(C=1e5)

logreg.fit(X, y)

# print the coefficients
print(logreg.intercept_)
print(logreg.coef_)

但是,我在 logreg.intercept _ 的输出中得到6列,而在 logreg.coef _ 的输出中得到6列.如何为每个特征获得1个系数,例如 a-f 值?

However, I get 6 columns in the output of logreg.intercept_ and 6 columns in the output of logreg.coef_ How can I get 1 coefficient per feature, e.g. a - f values?

y = a*x1 + b*x2 + c*x3 + d*x4 + e*x5 + f*x6

也可能是我做错了,因为 y_pred = logreg.predict(X)为我提供了所有行的 1 值.

Also, probably I am doing something wrong, because y_pred = logreg.predict(X) gives me the value of 1 for all rows.

推荐答案

检查在线文档:

coef _ :数组,形状(1,n_features)(n_classes,n_features)

决策函数中特征的系数.

Coefficient of the features in the decision function.

coef_的形状为(1,n_features) 当给定问题为二进制时.

coef_ is of shape (1, n_features) when the given problem is binary.

正如@Xochipilli在评论中已经提到的那样,您将具有(n_classes,n_features),或者在您的情况下具有(4,6)系数和4个截距(一个每堂课

As @Xochipilli has already mentioned in comments you are going to have (n_classes, n_features) or in your case (4,6) coefficients and 4 intercepts (one for each class)

可能我做错了,因为 y_pred =logreg.predict(X)给我所有行的 1 值.

是的,您不应尝试使用用于训练模型的数据进行预测.将您的数据分为训练和测试数据集,使用训练数据集训练模型,并使用测试数据集检查其准确性.

yes, you shouldn't try to use data that you've used for training your model for prediction. Split your data into training and test data sets, train your model using train data set and check it's accuracy using test data set.

这篇关于如何获得多项式逻辑回归的系数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆