LinearSVC coef_属性

问题描述 投票:0回答:1

我使用Python中的sklearn库训练了一个LinearSVC模型。目标变量y有4个级别(0,1,2,3)。

model = LinearSVC()
X_train, X_test, y_train, y_test = tts(X, y, test_size = 0.25,
                                       random_state = 4)
model.fit(X_train, y_train)
model.coef_

我得到这样的NumPy ndarray:

array([[ -1.64280582,  -0.49711136,   0.        , ...,   0.        ,
         -0.50203059,   0.        ],
       [  0.        ,   0.        ,  -2.67396495, ...,   2.35298657,
          0.        ,   0.        ],
       [  1.11471827,   3.76220356,   0.        , ..., -11.09758616,
          0.        ,   0.        ],
       [  0.        ,  -2.7305259 ,   0.09663903, ...,   0.        ,
          0.        ,   0.        ]])

我需要知道哪个数组匹配每个目标变量级别。我怎么知道?

python-3.x scikit-learn linear coefficients svc
1个回答
0
投票

此数组的顺序与输入数据的顺序相同。数组从[0]开始,这对应于输入数据中的feature_1。该数组的结构将是n_classes * n_features。


0
投票

首先,这是一个一对一的实施。

从文档:

coef_:array,shape = [n_features] if n_classes == 2 else [n_classes,n_features]赋予特征的权重(原始问题中的系数)。这仅适用于线性内核。

coef_是源自​​raw_coef_的readonly属性,它遵循liblinear的内部存储器布局。

在您的情况下,形状是[n_classes,n_features]。

  • model.coef_[0,:]为您提供0 vs all类的特征权重。
  • model.coef_[1,:]为您提供1 vs all类的特征权重。
  • 等等。
© www.soinside.com 2019 - 2024. All rights reserved.