[英]Viewing the coefficients/weights of a pickled trained model in 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'))
我可以通過以下方法使用腌制模型對新數據進行分類:
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]])
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.