Aizzaac Aizzaac - 1 month ago 21
Python Question

How does sp_randint work?

I am doing hyperparameter optimisation of Random Forest classifier. I am planning to use RandomSearchCV.

So by checking the available code in Scikit learn: What does sp_randint do?
Does it randomly take a value from 1 to 11? Can it be replaced by other function?

from scipy.stats import randint as sp_randint

param_dist = {"n_estimators": sp_randint (1, 11),
"max_depth": [3, None],
"max_features": sp_randint(1, 11),
"min_samples_split": sp_randint(1, 11),
"min_samples_leaf": sp_randint(1, 11),
}


Thank you.

Answer

sklearn.grid_search.RandomizedSearchCV can get a param_distributions parameter, mapping parameters to random distributions supporting the rvs method.

In your example, this object will return random integers in the range $[1, 11)$:

In [8]: g = sp_randint(1, 11)

In [9]: g.rvs(20)
Out[9]: 
array([ 5,  2,  9, 10,  6,  9,  9,  8,  1,  5,  1,  8,  1,  5,  5,  4,  6,
        5,  8,  4])

You can change it to any other object meaningfully supporting the rvs method, or even a list. For example:

param_dist = {"n_estimators": [1, 3, 4], 
              "max_depth": [3, None],
              "max_features": [1, 3, 4],
              "min_samples_split": [1, 3, 4],
              "min_samples_leaf": [1, 3, 4],
             }

will work as well.

Comments