Day14 多目标优化算法

import random
import lightgbm as lgb  # 核心新增:导入LightGBM库
from deap import base, creator, tools, algorithms
from sklearn.metrics import precision_score, recall_score  # 保留评估指标库

# 4.2 定义问题:个体与适应度(框架不变,无需修改)
# 定义适应度:最大化两个目标(精确率、召回率),权重都设为 1.0
creator.create("FitnessMulti", base.Fitness, weights=(1.0, 1.0))

# 定义个体:每个个体包含一组LightGBM的超参数
creator.create("Individual", list, fitness=creator.FitnessMulti)

# 4.3 创建工具箱 (Toolbox)
toolbox = base.Toolbox()

# ======================================
# 关键修改1:替换为LightGBM的核心超参数及生成方式
# ======================================
# LightGBM 整数型超参数生成
toolbox.register("attr_num_leaves", random.randint, 31, 127)  # 叶子节点数,需满足 num_leaves < 2^max_depth
toolbox.register("attr_max_depth", random.randint, 3, 20)     # 树的最大深度,无需过大
toolbox.register("attr_min_child_samples", random.randint, 5, 50)  # 叶子节点最小样本数,防止过拟合

# LightGBM 浮点型超参数生成(学习率)
def attr_learning_rate():
    return random.uniform(0.01, 0.3)  # 学习率范围:0.01 ~ 0.3
toolbox.register("attr_learning_rate", attr_learning_rate)

# ======================================
# 关键修改2:更新个体生成器,组合LightGBM的超参数
# ======================================
toolbox.register("individual", tools.initCycle, creator.Individual, 
               (toolbox.attr_num_leaves, toolbox.attr_max_depth, 
                toolbox.attr_learning_rate, toolbox.attr_min_child_samples), n=1)

# 定义种群(框架不变,无需修改)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

# ======================================
# 关键修改3:重写多目标评估函数,替换为LightGBM模型
# ======================================
def evaluate_lgb(individual):
    """
    评估函数:接收一个个体(LightGBM超参数组合),返回其在测试集上的(精确率, 召回率)
    """
    # 从个体中解析LightGBM超参数
    num_leaves, max_depth, learning_rate, min_child_samples = individual
    
    # 类型转换:整数型参数强转int,浮点型参数保持不变
    num_leaves = int(num_leaves)
    max_depth = int(max_depth)
    min_child_samples = int(min_child_samples)
    
    # 创建并训练LightGBM分类器
    lgb_model = lgb.LGBMClassifier(
        num_leaves=num_leaves,
        max_depth=max_depth,
        learning_rate=learning_rate,
        min_child_samples=min_child_samples,
        n_estimators=200,  # 固定迭代次数,也可加入多目标优化
        objective="binary",  # 适配二分类任务(Credit Default)
        random_state=42,
        n_jobs=-1,
        verbose=-1  # 关闭冗余训练输出,保持整洁
    )
    lgb_model.fit(X_train, y_train)
    
    # 在测试集上进行预测
    predictions = lgb_model.predict(X_test)
    
    # 计算精确率和召回率(保持多目标优化目标不变)
    precision = precision_score(y_test, predictions)
    recall = recall_score(y_test, predictions)
    
    # 返回评估结果(必须是元组,符合DEAP多目标要求)
    return (precision, recall)

# ======================================
# 关键修改4:注册评估函数和自定义变异算子(适配LightGBM参数)
# ======================================
# 注册LightGBM的评估函数
toolbox.register("evaluate", evaluate_lgb)

# 注册交叉算子(两点交叉,框架不变,无需修改)
toolbox.register("mate", tools.cxTwoPoint)

# 自定义变异算子:适配LightGBM的整数型+浮点型参数
def mutate_lgb_individual(individual):
    """
    自定义变异函数,分别处理LightGBM的整数型和浮点型超参数
    """
    # 1. 整数型参数变异(num_leaves, max_depth, min_child_samples)
    int_param_indices = [0, 1, 3]  # 对应个体中的整数型参数索引
    int_param_bounds = [
        (31, 127),   # num_leaves 边界
        (3, 20),     # max_depth 边界
        (5, 50)      # min_child_samples 边界
    ]
    
    for idx, (low, high) in zip(int_param_indices, int_param_bounds):
        if random.random() < 0.2:  # 对应原indpb=0.2的变异概率
            individual[idx] = random.randint(low, high)
    
    # 2. 浮点型参数变异(learning_rate,索引2)
    if random.random() < 0.2:
        individual[2] = random.uniform(0.01, 0.3)
    
    return individual,

# 注册自定义变异算子(替换原有的mutUniformInt)
toolbox.register("mutate", mutate_lgb_individual)

# 注册选择算子:NSGA-II 多目标选择算法(框架不变,无需修改)
toolbox.register("select", tools.selNSGA2)
import random
import lightgbm as lgb  # 核心新增:导入LightGBM库
from deap import base, creator, tools, algorithms
from sklearn.metrics import precision_score, recall_score  # 保留评估指标库

# 4.2 定义问题:个体与适应度(框架不变,无需修改)
# 定义适应度:最大化两个目标(精确率、召回率),权重都设为 1.0
creator.create("FitnessMulti", base.Fitness, weights=(1.0, 1.0))

# 定义个体:每个个体包含一组LightGBM的超参数
creator.create("Individual", list, fitness=creator.FitnessMulti)

# 4.3 创建工具箱 (Toolbox)
toolbox = base.Toolbox()

# ======================================
# 关键修改1:替换为LightGBM的核心超参数及生成方式
# ======================================
# LightGBM 整数型超参数生成
toolbox.register("attr_num_leaves", random.randint, 31, 127)  # 叶子节点数,需满足 num_leaves < 2^max_depth
toolbox.register("attr_max_depth", random.randint, 3, 20)     # 树的最大深度,无需过大
toolbox.register("attr_min_child_samples", random.randint, 5, 50)  # 叶子节点最小样本数,防止过拟合

# LightGBM 浮点型超参数生成(学习率)
def attr_learning_rate():
    return random.uniform(0.01, 0.3)  # 学习率范围:0.01 ~ 0.3
toolbox.register("attr_learning_rate", attr_learning_rate)

# ======================================
# 关键修改2:更新个体生成器,组合LightGBM的超参数
# ======================================
toolbox.register("individual", tools.initCycle, creator.Individual, 
               (toolbox.attr_num_leaves, toolbox.attr_max_depth, 
                toolbox.attr_learning_rate, toolbox.attr_min_child_samples), n=1)

# 定义种群(框架不变,无需修改)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

# ======================================
# 关键修改3:重写多目标评估函数,替换为LightGBM模型
# ======================================
def evaluate_lgb(individual):
    """
    评估函数:接收一个个体(LightGBM超参数组合),返回其在测试集上的(精确率, 召回率)
    """
    # 从个体中解析LightGBM超参数
    num_leaves, max_depth, learning_rate, min_child_samples = individual
    
    # 类型转换:整数型参数强转int,浮点型参数保持不变
    num_leaves = int(num_leaves)
    max_depth = int(max_depth)
    min_child_samples = int(min_child_samples)
    
    # 创建并训练LightGBM分类器
    lgb_model = lgb.LGBMClassifier(
        num_leaves=num_leaves,
        max_depth=max_depth,
        learning_rate=learning_rate,
        min_child_samples=min_child_samples,
        n_estimators=200,  # 固定迭代次数,也可加入多目标优化
        objective="binary",  # 适配二分类任务(Credit Default)
        random_state=42,
        n_jobs=-1,
        verbose=-1  # 关闭冗余训练输出,保持整洁
    )
    lgb_model.fit(X_train, y_train)
    
    # 在测试集上进行预测
    predictions = lgb_model.predict(X_test)
    
    # 计算精确率和召回率(保持多目标优化目标不变)
    precision = precision_score(y_test, predictions)
    recall = recall_score(y_test, predictions)
    
    # 返回评估结果(必须是元组,符合DEAP多目标要求)
    return (precision, recall)

# ======================================
# 关键修改4:注册评估函数和自定义变异算子(适配LightGBM参数)
# ======================================
# 注册LightGBM的评估函数
toolbox.register("evaluate", evaluate_lgb)

# 注册交叉算子(两点交叉,框架不变,无需修改)
toolbox.register("mate", tools.cxTwoPoint)

# 自定义变异算子:适配LightGBM的整数型+浮点型参数
def mutate_lgb_individual(individual):
    """
    自定义变异函数,分别处理LightGBM的整数型和浮点型超参数
    """
    # 1. 整数型参数变异(num_leaves, max_depth, min_child_samples)
    int_param_indices = [0, 1, 3]  # 对应个体中的整数型参数索引
    int_param_bounds = [
        (31, 127),   # num_leaves 边界
        (3, 20),     # max_depth 边界
        (5, 50)      # min_child_samples 边界
    ]
    
    for idx, (low, high) in zip(int_param_indices, int_param_bounds):
        if random.random() < 0.2:  # 对应原indpb=0.2的变异概率
            individual[idx] = random.randint(low, high)
    
    # 2. 浮点型参数变异(learning_rate,索引2)
    if random.random() < 0.2:
        individual[2] = random.uniform(0.01, 0.3)
    
    return individual,

# 注册自定义变异算子(替换原有的mutUniformInt)
toolbox.register("mutate", mutate_lgb_individual)

# 注册选择算子:NSGA-II 多目标选择算法(框架不变,无需修改)
toolbox.register("select", tools.selNSGA2)

@浙大疏锦行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值