属性错误:“LinearSVC”对象没有属性“coef_”

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

我使用 LinearSVC 来解决多标签分类问题。由于

LinearSVC
没有提供
predict_proba
方法,因此我决定使用
CalibratedClassifierCV
将决策函数缩放为 [0, 1] 概率。

from sklearn.svm import LinearSVC
from sklearn.calibration import CalibratedClassifierCV

classifier = CalibratedClassifierCV(LinearSVC(class_weight = 'balanced', max_iter = 100000)
classifier.fit(X_train, y_train)

但是,我还需要访问权重

coef_
,但是
classifier.base_estimator.coef_
引发以下错误:

AttributeError: 'LinearSVC' object has no attribute 'coef_'

我认为

classifier.base_estimator
返回了校准的分类器并允许访问其所有属性。预先感谢您向我解释我误解的内容。

python python-3.x machine-learning svm multilabel-classification
1个回答
0
投票

我偶然发现了这个问题,结果发现你需要查看底层的校准模型(这并不存储在

classifier.base_estimator
中,这看起来很直观):

classifier = CalibratedClassifierCV(LinearSVC(
    class_weight = 'balanced', 
    max_iter = 100000
)
classifier.fit(X_train, y_train)

# access the weights of each trained classifier here
trained_classifiers = classifier.calibrated_classifiers_

# look at the first one for example
first_trained_classifier = trained_classifiers[0]

print(first_trained_classifier.base_estimator.coef_)

这将输出类似以下内容:

array([[-2.92536427e-01, -4.09833721e-02, -1.05421453e-01, ...,
        -1.10948730e-01, -9.39921540e-03, -4.10724956e-03],
       [-4.02743557e-01, -3.56716135e-01,  2.30114807e-01, ...,
        -4.56128723e-02,  0.00000000e+00,  1.08420217e-17],
       [ 1.90550610e-01,  3.70755554e-02, -9.41194493e-02, ...,
        -1.38777878e-17,  3.87705771e-02,  9.26296583e-02],
       ...,
       [-5.93576666e-02,  1.03790828e-01, -7.95228024e-02, ...,
        -1.50562796e-01,  4.56042004e-02,  0.00000000e+00],
       [ 2.72370524e-01,  8.71504993e-02,  1.30601404e-03, ...,
        -1.55804453e-01, -3.92344382e-02,  6.05113193e-02],
       [-3.91555037e-01, -1.31277960e-01, -4.62863392e-02, ...,
        -1.67542946e-01,  0.00000000e+00,  0.00000000e+00]])
© www.soinside.com 2019 - 2024. All rights reserved.