首页 > 解决方案 > 在 scikit-learn 中查看腌制训练模型的系数/权重

问题描述

几个月前,我在 Scikit-learn 中训练了一个 SVM:

# Create standardizer
standardizer = StandardScaler()

# Create logistic regression
lsvc = SVC(C=0.1, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

# Create a pipeline that standardizes, then runs Support Vector Machine
svc_pipeline = make_pipeline(standardizer,lsvc)

我已经像这样腌制模型:

# Save Trained Model
with open('WF_SVC_Final.pkl', 'wb') as fid:
    pickle.dump(svc_pipeline, fid)

现在,我已经像这样加载了腌制模型:

WF_SVC_Final = pickle.load(open('WF_SVC_Final.pkl', 'rb'))

我可以使用 pickle 模型通过调用以下方法对新数据进行分类:

WF_SVC_Final.predict(x)

但我试图通过 .coef_ 属性查看/检查腌制模型的系数,但由于某种原因这不起作用:

WF_SVC_Final.coef_

我收到以下错误:

AttributeError:“管道”对象没有属性“coef_”

有谁知道如何解决这个问题?谢谢

标签: pythonmachine-learningscikit-learnpickle

解决方案


你快到了,只是你需要调用named_steps管道内部并调用coef它。我已将您的代码修改如下:

import pandas as pd 
import numpy as np
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
import pickle 

X, y = make_classification(n_samples=1000, n_classes=2,
                       n_informative=4, weights=[0.7, 0.3],
                       random_state=0)

standardizer = StandardScaler()

# Create support vector classifier
lsvc = SVC(C=0.1, cache_size=200, class_weight=None, coef0=0.0,
           decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
           max_iter=-1, probability=False, random_state=None, shrinking=True,
           tol=0.001, verbose=False)

# Create a pipeline that standardizes, then runs Support Vector Machine  
svc_pipeline = make_pipeline(standardizer,lsvc)

x_train, x_test, y_train, y_test = train_test_split(X,y, test_size=0.33, random_state=42)
svc_pipeline.fit(x_train,y_train)

with open('WF_SVC_Final.pkl', 'wb') as fid:
    pickle.dump(svc_pipeline, fid)

WF_SVC_Final = pickle.load(open('WF_SVC_Final.pkl', 'rb'))

coefficients = WF_SVC_Final.named_steps["svc"].coef_   #since svc is the name of the estimator we call it here

现在当我们打印时,coefficients我们得到

array([[ 0.02914615,  0.02835727, -0.0476559 , -0.03579271,  0.07187892,
    -0.10166647,  0.25455972, -0.02468286,  0.07035736, -0.0427572 ,
    -0.06497132, -0.1014921 , -0.01929861, -0.00833354, -0.04557688,
     0.06657225, -0.05579179,  0.24851723,  0.29399611,  0.04916833]])   

希望这可以帮助!


推荐阅读