Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set parameters to score function in sklearn SelectKBest ()

I'm using the SelectKBest() class for feature selection in sklearn. SelectKBest() can take a callable score function as an input. In this case I would like to use mutual_info_regression as the score function. mutual_info_regression can take a few parameters which I would like to set myself. For example, I would like to set the random_state = 0. The problem is I'm not sure how to pass parameters into the score function since the score function is itself a parameter in SelectKBest(). Obviously something like SelectKBest(score_func = mutual_info_classif(random_state=0)) won't work because the mutal_info_classif function would be called directly. I feel like this is probably a very basic question about python classes but I can't seem to find anything addressing what I'm looking for. Thanks in advance for your time!

like image 486
Skip Avatar asked Jun 03 '17 19:06

Skip


1 Answers

You can create another function which calls mutual_info_regression and pass it instead:

def my_score(X, y):
    return mutual_info_regression(X, y, random_state=0)

SelectKBest(score_func=my_score)

Python standard library provides an useful helper for creating such functions - it is called functools.partial. It allows to create functions with some parameters pre-set; instead of "manual" my_score definition you can write

from functools import partial
my_score = partial(mutual_info_regression, random_state=0)
SelectKBest(score_func=my_score)

Of course, you can also pass partial directly, which gets you very close to your example:

from functools import partial
SelectKBest(score_func=partial(mutual_info_classif, random_state=0))
like image 169
Mikhail Korobov Avatar answered Sep 23 '22 22:09

Mikhail Korobov