PyGAD传递整数参数不符合文档要求的问题排查与解决
PyGAD超参数优化类型转换问题解决
问题背景
使用PyGAD优化随机森林模型超参数时,gene_space中通过np.linspace生成的整数数组,经PyGAD处理后传递给RandomForestClassifier时被转为numpy.float64类型,触发报错:
ValueError: n_estimators must be an integer, got <class 'numpy.float64'>
直接调用gene_space[0][0]作为参数可正常运行,确认问题源于PyGAD对基因值的类型处理。
相关代码
gene_space定义
import numpy as np gene_space = [ # n_estimators np.linspace(50,200,25, dtype='int'), # min_samples_split np.linspace(2,10,5, dtype='int'), # min_samples_leaf np.linspace(1,10,5, dtype='int'), # min_impurity_decrease np.linspace(0,1,10, dtype='float') ]
适应度函数
from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.utils import sample_without_replacement def fitness_function_factory(data=data, y_name='y', sample_size=100): def fitness_function(solution, solution_idx): model = RandomForestClassifier( n_estimators=solution[0], min_samples_split=solution[1], min_samples_leaf=solution[2], min_impurity_decrease=solution[3] ) X = data.drop(columns=[y_name]) y = data[y_name] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5) train_idx = sample_without_replacement(n_population=len(X_train), n_samples=sample_size) test_idx = sample_without_replacement(n_population=len(X_test), n_samples=sample_size) model.fit(X_train.iloc[train_idx], y_train.iloc[train_idx]) fitness = model.score(X_test.iloc[test_idx], y_test.iloc[test_idx]) return fitness return fitness_function
PyGAD实例化
import pygad cross_validate = pygad.GA(gene_space=gene_space, fitness_func=fitness_function_factory(), num_generations=100, num_parents_mating=2, sol_per_pop=8, num_genes=len(gene_space), parent_selection_type='sss', keep_parents=2, crossover_type="single_point", mutation_type="random", mutation_percent_genes=25) cross_validate.best_solution()
解决方法
1. 适应度函数内强制类型转换
在创建RandomForestClassifier时,将需要整数的参数显式转为int类型,直接规避PyGAD的类型转换问题:
def fitness_function(solution, solution_idx): model = RandomForestClassifier( n_estimators=int(solution[0]), min_samples_split=int(solution[1]), min_samples_leaf=int(solution[2]), min_impurity_decrease=solution[3] ) # 后续代码不变
2. 将numpy数组转为Python原生列表
PyGAD对原生Python整数的处理更稳定,把gene_space中的numpy数组转为列表:
gene_space = [ np.linspace(50,200,25, dtype='int').tolist(), np.linspace(2,10,5, dtype='int').tolist(), np.linspace(1,10,5, dtype='int').tolist(), np.linspace(0,1,10, dtype='float').tolist() ]
3. 统一处理参数类型
在适应度函数开头对所有参数做类型映射,让代码更整洁:
def fitness_function(solution, solution_idx): # 前3个参数转整数,第4个保留浮点 processed_sol = [int(x) for x in solution[:3]] + [solution[3]] model = RandomForestClassifier( n_estimators=processed_sol[0], min_samples_split=processed_sol[1], min_samples_leaf=processed_sol[2], min_impurity_decrease=processed_sol[3] ) # 后续代码不变
内容的提问来源于stack exchange,提问作者jbuddy_13
相关产品推荐
相关产品推荐

