参数不会进入 scikit-learn GridSearchCV 中的自定义估计器 [英] Parameters are not going to custom estimator in scikit-learn GridSearchCV

查看:76
本文介绍了参数不会进入 scikit-learn GridSearchCV 中的自定义估计器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将参数传递给 scikit learn 中的自定义估算器,但未能成功.我希望参数 lr 在 gridsearch 期间更改.问题是 lr 参数没有改变...

此处复制和更新代码示例>

(原始代码对我不起作用)

任何带有自定义估算器的 GridSearchCV 完整工作示例,以及更改参数将不胜感激.

我在 ubuntu 18.10 使用 scikit-learn 0.20.2

from sklearn.model_selection import GridSearchCV从 sklearn.base 导入 BaseEstimator,ClassifierMixin将 numpy 导入为 np类 MyClassifier(BaseEstimator, ClassifierMixin):def __init__(self, lr=0.1):# 一些代码打印('lr:',lr)回归自我def fit(self, X, y):# 一些代码回归自我定义预测(自我,X):# 一些代码回报 X % 3参数 = {'lr': [0.1, 0.5, 0.7]}gs = GridSearchCV(MyClassifier(), param_grid=params, cv=4)x = np.arange(30)y = np.concatenate((np.zeros(10), np.ones(10), np.ones(10) * 2))gs.fit(x, y)

特维辛,马库斯

解决方案

由于您在构造函数中打印,因此您无法看到 lr 值的变化.

如果我们在.fit()函数里面打印,我们可以看到lr值的变化.这是因为 方式 创建估计量的不同副本.请参阅此处了解该过程用于创建多个副本.

from sklearn.model_selection import GridSearchCV从 sklearn.base 导入 BaseEstimator,ClassifierMixin将 numpy 导入为 np类 MyClassifier(BaseEstimator, ClassifierMixin):def __init__(self, lr=0):# 一些代码打印('lr:',lr)self.lr = lrdef fit(self, X, y):# 一些代码打印('lr:',self.lr)回归自我定义预测(自我,X):# 一些代码回报 X % 3参数 = {'lr': [0.1, 0.5, 0.7]}gs = GridSearchCV(MyClassifier(), param_grid=params, cv=4)x = np.arange(30)y = np.concatenate((np.zeros(10), np.ones(10), np.ones(10) * 2))gs.fit(x, y)gs.predict(x)

输出:

lr: 0长:0长:0LR:0.1长:0LR:0.1长:0LR:0.1长:0LR:0.1长:0LR:0.5长:0LR:0.5长:0LR:0.5长:0LR:0.5长:0LR:0.7长:0LR:0.7长:0LR:0.7长:0LR:0.7长:0LR:0.1

I'm trying and failing to pass parameters to a custom estimator in scikit learn. I'd like the parameter lr to change during the gridsearch. Problem is that the lr parameter is not changing...

The code sample is copied and updated from here

(the original code did neither work for me)

Any full working example of GridSearchCV with custom estimator, with changing parameters would be appreciated.

I'm in ubuntu 18.10 using scikit-learn 0.20.2

from sklearn.model_selection import GridSearchCV
from sklearn.base import BaseEstimator, ClassifierMixin
import numpy as np

class MyClassifier(BaseEstimator, ClassifierMixin):

     def __init__(self, lr=0.1):
         # Some code
         print('lr:', lr)
         return self

     def fit(self, X, y):
         # Some code
         return self

     def predict(self, X):
         # Some code
         return X % 3

params = {
    'lr': [0.1, 0.5, 0.7]
}
gs = GridSearchCV(MyClassifier(), param_grid=params, cv=4)

x = np.arange(30)
y = np.concatenate((np.zeros(10), np.ones(10), np.ones(10) * 2))
gs.fit(x, y)

Terveisin, Markus

解决方案

You were not able to see the change in lr value since you are printing inside constructor function.

If we print inside .fit() function, we can see the change of lr values. It happens because of the way the different copies of estimators are created. See here to understand the process for creating multiple copies.

from sklearn.model_selection import GridSearchCV
from sklearn.base import BaseEstimator, ClassifierMixin
import numpy as np

class MyClassifier(BaseEstimator, ClassifierMixin):

    def __init__(self, lr=0):
         # Some code
        print('lr:', lr)
        self.lr = lr

    def fit(self, X, y):
         # Some code
        print('lr:', self.lr)
        return self

    def predict(self, X):
         # Some code
         return X % 3

params = {
    'lr': [0.1, 0.5, 0.7]
}
gs = GridSearchCV(MyClassifier(), param_grid=params, cv=4)

x = np.arange(30)
y = np.concatenate((np.zeros(10), np.ones(10), np.ones(10) * 2))
gs.fit(x, y)
gs.predict(x)

Output:

lr: 0
lr: 0
lr: 0
lr: 0.1
lr: 0
lr: 0.1
lr: 0
lr: 0.1
lr: 0
lr: 0.1
lr: 0
lr: 0.5
lr: 0
lr: 0.5
lr: 0
lr: 0.5
lr: 0
lr: 0.5
lr: 0
lr: 0.7
lr: 0
lr: 0.7
lr: 0
lr: 0.7
lr: 0
lr: 0.7
lr: 0
lr: 0.1

这篇关于参数不会进入 scikit-learn GridSearchCV 中的自定义估计器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆