几个月前,我已经在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'))
我可以通过以下方法使用腌制的模型对新数据进行分类:
WF_SVC_Final.predict(x)
但是我试图通过.coef_属性查看/检查腌制模型的系数,但是由于某些原因,这是行不通的:
WF_SVC_Final.coef_
我遇到以下错误:
AttributeError:“管道”对象没有属性“ coef _”
有人知道如何解决这个问题吗?谢谢
答案 0 :(得分:0)
您快到了,只需要在管道内部调用 before_action :do_stuff, if: [:condition_1?, :condition_2?], only: :show
,并在其顶部调用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
希望这会有所帮助!