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!
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With