如何根据构造函数参数创建具有类属性的子类,以便在 GridSearchCV 估计器中使用?

我想子类化sklearn.svm.LinearSVC并将其用作 的估计器sklearn.model_selection.GridSearchCV。我之前在子类化方面遇到了一些问题,我认为我根据我之前的帖子和所选答案修复了它。

然而,现在我的目标是创建一个sklearn.kernel_approximation.RBFSampler对象作为我的新类的属性。现在这是一个例子,我有一个更广泛的问题:

问题: 最终期望将我的新估计器类与 一起使用GridSearchCV,如何根据传递到构造函数的参数值(或缺少参数值)创建属性?

然而,正如我在这里了解到的,GridSearchCV 首先使用默认值启动估计器对象,并且与feature_importances_中的属性具有类似的实现sklearn.tree.DecisionTreeClassifier

另外,我从上面的代码中得到的错误是:

---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-6-a11420cc931e> in <module>

     66                       'sampler_n': [10, 200]}

     67 

---> 68     gs_model = GridSearchCV(estimator=LinearSVCSub(), verbose=1, param_grid=param_grid,

     69                             scoring='roc_auc', n_jobs=-1, cv=2)

     70     gs_model.fit(X, y)


<ipython-input-6-a11420cc931e> in __init__(self, penalty, loss, sampler_gamma, sampler_n, dual, tol, C, multi_class, fit_intercept, intercept_scaling, class_weight, verbose, random_state, max_iter)

     21         self.sampler_n = sampler_n

     22 

---> 23         self.sampler = create_sampler()

     24 

     25 


NameError: name 'create_sampler' is not defined


白衣染霜花
浏览 99回答 1
1回答

阿波罗的战车

使用__init__构造函数作为存储属性的容器。在方法中执行所有相应的逻辑from sklearn.datasets import make_classificationfrom sklearn.svm import LinearSVCfrom sklearn.model_selection import GridSearchCVfrom sklearn.kernel_approximation import RBFSamplerfrom sklearn.datasets import load_breast_cancerRANDOM_STATE = 123class LinearSVCSub(LinearSVC):&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def __init__(self, penalty='l2', loss='squared_hinge', sampler_gamma=None, sampler_n=None,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dual=True, tol=0.0001, C=1.0, multi_class='ovr', fit_intercept=True, intercept_scaling=1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;class_weight=None, verbose=0, random_state=None, max_iter=1000, sampler=None):&nbsp; &nbsp; &nbsp; &nbsp; super(LinearSVCSub, self).__init__(penalty=penalty, loss=loss, dual=dual, tol=tol,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C=C, multi_class=multi_class, fit_intercept=fit_intercept,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;intercept_scaling=intercept_scaling, class_weight=class_weight,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;verbose=verbose, random_state=random_state, max_iter=max_iter)&nbsp; &nbsp; &nbsp; &nbsp; self.sampler_gamma = sampler_gamma&nbsp; &nbsp; &nbsp; &nbsp; self.sampler_n = sampler_n&nbsp; &nbsp; &nbsp; &nbsp; self.sampler = sampler&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; def fit(self, X, y, sample_weight=None):&nbsp; &nbsp; &nbsp; &nbsp; X = self.transform_this(X)&nbsp; &nbsp; &nbsp; &nbsp; super(LinearSVCSub, self).fit(X, y, sample_weight)&nbsp; &nbsp; &nbsp; &nbsp; return self&nbsp; &nbsp; def predict(self, X):&nbsp; &nbsp; &nbsp; &nbsp; X = self.transform_this(X)&nbsp; &nbsp; &nbsp; &nbsp; return super(LinearSVCSub, self).predict(X)&nbsp; &nbsp; def score(self, X, y, sample_weight=None):&nbsp; &nbsp; &nbsp; &nbsp; X = self.transform_this(X)&nbsp; &nbsp; &nbsp; &nbsp; return super(LinearSVCSub, self).score(X, y, sample_weight)&nbsp; &nbsp; def decision_function(self, X):&nbsp; &nbsp; &nbsp; &nbsp; X = self.transform_this(X)&nbsp; &nbsp; &nbsp; &nbsp; return super(LinearSVCSub, self).decision_function(X)&nbsp; &nbsp; def transform_this(self, X):&nbsp; &nbsp; &nbsp; &nbsp; if self.sampler:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; X = RBFSampler(gamma=self.sampler_gamma, n_components=self.sampler_n).fit_transform(X)&nbsp; &nbsp; &nbsp; &nbsp; return Xdata = load_breast_cancer()X, y = data.data, data.target# Parameter tuning with custom LinearSVCparam_grid = {'C': [0.00001, 0.0005],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'dual': (True, False), 'random_state': [RANDOM_STATE],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sampler_gamma': [0.90, 0.60, 0.30],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sampler_n': [10, 200],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sampler':[0,1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}gs_model = GridSearchCV(estimator=LinearSVCSub(sampler=1), verbose=1, param_grid=param_grid,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scoring='roc_auc', n_jobs=-1, cv=2)gs_model.fit(X, y)gs_model.cv_results_
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python