I run the same code twice and get a different set of selected features. Why aren't the results reproducible?
Why this happens
The mutual information estimator adds a tiny amount of random noise to continuous features to break ties between identical values. It also uses a nearest-neighbor method that's sensitive to that noise. Without a fixed seed, the scores change slightly on each run. Features with similar scores can then swap places at the edge of the top k.
SelectKBest has no random_state parameter of its own, so you have to pass the seed to the score function.
Answer 1: Fix random_state with functools.partial
partial creates a version of the function with some arguments filled in. This is the recommended approach:
from functools import partial
from sklearn.feature_selection import mutual_info_classif
selector = SelectKBest(partial(mutual_info_classif,
random_state=42), k=10)
