我在进行随机森林分类器的超参数优化。我计划使用RandomSearchCV。
通过查看Scikit learn中的可用代码:sp_randint 做什么用?它是从1到11中随机取一个值吗?它可以被其他函数替代吗?
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), }
谢谢你。
回答:
sklearn.grid_search.RandomizedSearchCV
可以接受一个 param_distributions
参数,该参数将参数映射到支持 rvs
方法的随机分布上。
在你的例子中,这个对象将返回范围为 $[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])
你可以将其更改为任何其他支持 rvs
方法的对象,甚至是一个列表。例如:
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], }
也是可以工作的。