根据scikitlearn中的最低分数选择功能

2024-05-28 18:30:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在管道中选择功能。 我的管道如下:

我想用

Univariate feature selector with configurable strategy.

根据文件:

class sklearn.feature_selection.GenericUnivariateSelect(score_func=<function f_classif>, mode=’percentile’, param=1e-05)

   score_func : callable

    Function taking two arrays X and y, and returning a pair of arrays (scores, pvalues). For modes ‘percentile’ or ‘kbest’ it can return a single array scores.

我有一个自定义的分数函数,可以满足这些要求。在

^{pr2}$

但如何添加其他模式? 可能,不必重写selectorxin之上的类


编辑

我的管道看起来像:

from  sklearn.feature_selection import GenericUnivariateSelect

custom_filter=GenericUnivariateSelect(my_score)   
MyProcessingPipeline=Pipeline(steps=[('filter_step', custom_filter)])

我的处理管道非常简单:

X=pd.DataFrame(data=np.random.rand(500,3))
MyProcessingPipeline.fit(X)
MyProcessingPipeline.transform(X)

我的分数是:

#Function taking two arrays X and y, and returning a pair of arrays (scores, pvalues). 
def my_score(X,y):
    return (np.random.rand(X.shape[1]),np.zeros((X.shape[1],1)))

在我的例子中,我希望转换保留my_score返回分数>0.6的所有特性。 如何获得? 我越来越确信我将不得不重写一些本机sklearn类,但是有人知道我应该重写哪个类来最小化要编写的代码量,同时能够执行这个非常简单的特性选择吗?在


Tags: and管道mynpsklearnfilter分数feature

热门问题