在没有范围技巧的情况下将额外的参数传递给GenericUnivariateSelect [英] passing an extra argument to GenericUnivariateSelect without scope tricks
问题描述
如果我应用答案中建议的make_scorer
解决方法,这是完整的追溯...
here is the complete traceback if I apply the make_scorer
workaround suggested in the answers...
`File "________python/anaconda-2.7.11-64/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File ""________python/anaconda-2.7.11-64/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File ""________/main_"________.py", line 43, in <module>
"_________index.fit(X,Y ,g=g,L=L)
File ""________/Core.py", line 95, in fit
X_preprocessed=self.preprocessing.fit_transform(X,y)
File ""________python/anaconda-2.7.11-64/lib/python2.7/site-packages/sklearn/pipeline.py", line 303, in fit_transform
return last_step.fit_transform(Xt, y, **fit_params)
File ""________/python/anaconda-2.7.11-64/lib/python2.7/site-packages/sklearn/base.py", line 497, in fit_transform
return self.fit(X, y, **fit_params).transform(X)
File "Base/Base.py", "________
score_func_ret = self.score_func(X, y)
TypeError: __call__() takes at least 4 arguments (3 given)`
我正在研究sklearn管道.
I am working on a sklearn pipeline.
custom_filter=GenericUnivariateSelect(Custom_Score,mode='MinScore',param=0.9)
custom_filter._selection_modes.update({'MinScore': SelectMinScore})
MyProcessingPipeline=Pipeline(steps=[...
('filter_step', None),
....])
ProcessingParams.update({'filter_step':custom_filter})
MyProcessingPipeline.set_params(**ProcessingParams)
其中SelectMinScore
是自定义BaseFilter
.
我需要基于Custom_Score
执行单变量特征选择,必须必须接收一个额外的参数,在这里称为XX
I need to perform univariate feature selection based on a Custom_Score
, which must receive an extra argument, called XX in here
def Custom_Score(X,Y,XX=_XX ):
# do stuff
return my_score
不幸的是,Akleik sklearn API不允许将额外的参数传递给管道步骤的参数.
Unfortunately, AFAIK the sklearn API does not allow extra arguments to be passed a parameter of a parameter of a step of a pipeline.
我尝试过
MyProcessingPipeline({'filter_step':custom_filter(XX=_XX)})
但这打破了传递级联的论点(我相信).
but that breaks argument passing cascade (I believe).
到目前为止,我已经通过编写包装器解决了这个问题,其中_XX是我需要的数据,不幸的是,在定义时,该数据必须在函数的范围内.
因此,我最终在main
函数中定义了该函数,以便_XX存在并且可以传递它.
So far, I have solved this by writing a wrapper, where _XX is the data I need which unfortunately need to be in the scope of the function at definition time.
So I have ended up defining the function within my main
function so that _XX exists and it can be passed.
def Custom_Score_Wrapped(X,Y):
return Custom_Score(X,Y,XX=_XX )
我认为这是一个非常肮脏的解决方法.
I think this is a really dirty workaround.
正确的方法是什么?
推荐答案
您可以在调用make_scorer()函数时简单地传递额外的争论.
例如,您选中此选项链接.在示例中,它使用了fbeta_score.
现在,fbeta需要一个附加参数beta
,该参数是在像这样调用make_scorer()函数时设置的:
You can simply pass the extra arguement while calling the make_scorer() function.
For example, you check this link. In the example it makes use of fbeta_score.
Now fbeta requires an additional parameter, beta
which is set while calling the make_scorer() function like this :
ftwo_scorer = make_scorer(fbeta_score, beta=2)
因此,在您的情况下,这应该可行:
So in your case, this should work:
def Custom_Score(X,Y,XX):
# do stuff
return my_score
my_scorer = make_scorer(Custom_Score,XX=_XX)
这篇关于在没有范围技巧的情况下将额外的参数传递给GenericUnivariateSelect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!