Вы почти у цели, просто вам нужно вызвать 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]])
Надеюсь, это поможет!