为实现模型建立过程与调参过程的分离,将模型的超参数写如配置文件中,以实现自动化调参
以随机森林为例:
超参数写入variables.ini配置文件中,每一个section可以作为一个调参的方案,并且在进行调参时,不需要对程序作出修改,简便了调参的过程
[RandomForest0]
n_estimators = [10,18,2]
criterion = ['gini','entropy']
[RandomForest1]
n_estimators = [10,20,4]
max_depth = [6,12,2]
读取配置文件
import configparser
config=configparser.ConfigParser()
config.read("variables.ini")
遍历配置文件中的超参数
#遍历配置文件中所有的section
for section in config.sections():
#将每一个section中所有的参数保存在para字典中
para=config.items(section)
clf=RandomForestClassifier(random_state=10)
for key, value in para:
#遍历字典中所有的超参数
value = eval(value)
#判断参数列表长度如果为3,则采用range(start, end, step)的形式
if len(value)!=3:
param_test={key:value}
else:
start=value[0]