python - 在 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_”
有谁知道如何解决这个问题?谢谢
解决方案
你快到了,只是你需要调用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]])
希望这可以帮助!
推荐阅读
- javascript - 如何通过添加存储项目来设置我的状态
- spring-boot - Spring数据jpa投影无法提取ResultSet
- java - 如何在 Java Eclipse Enterprise IDE 中设置背景图像
- yocto - 在 Bitbake 中包含 systemd-journal-remote
- php - Laravel:无法访问 Facade 的受保护功能
- react-native - 崩溃中的 React Native 崩溃
- javascript - JavaScript,无法读取 null 的属性“映射””,嵌套 ForEach?
- javascript - 如何使用 jquery / javascript 有条件地更改自定义属性的属性
- python - Python:对列表中已有的项目进行分组并将它们反转
- javascript - Chart.js 中的同一个甜甜圈图是否可以有不同的厚度?