【发布时间】:2018-07-08 13:17:36
【问题描述】:
我需要使用 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 值?
y = a*x1 + b*x2 + c*x3 + d*x4 + e*x5 + f*x6
另外,可能我做错了什么,因为y_pred = logreg.predict(X) 为我提供了所有行的1 值。
【问题讨论】:
-
如果您正在执行多类逻辑回归,您将拥有每个类/变量组合的系数。因此,每个特征不会有 1 个系数,每个特征将有
n系数,其中n是您的类数。 -
@Xochipilli:那么,如果我想得到我在问题中提到的公式(我指的是系数
a-f),我该怎么办?我不能使用多元线性回归,因为y是分类的。 -
“因为 y_pred = logreg.predict(X) 给了我所有行的值 1”:-- 也许数据不足以适应和区分类。
标签: python pandas scikit-learn logistic-regression