You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何强制LightGBM模型将指定特征纳入分裂(特征重要性)

强制LightGBM模型使用指定特征的方法

我需要训练LightGBM模型时,强制让部分特征必须参与树分裂(进入特征重要性列表),确保模型预测受这些变量影响。比如下面的示例中,useless_feature_1是一个常量特征(实际场景中可能是业务角度重要但未被模型选中的特征),当前训练后它的特征重要性为0,需要让它被模型使用。

原建模代码:

from lightgbm import LGBMRegressor
import pandas as pd
import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# 生成回归数据集
X, y = make_regression(n_samples=1000, n_features=10, noise=0.9, random_state=42)
feature_names = [f"feature_{i}" for i in range(X.shape[1])]

# 转为DataFrame提升可读性
X = pd.DataFrame(X, columns=feature_names)

# 添加无意义常量特征(模拟业务重要但未被选中的特征)
X["useless_feature_1"] = 1

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 定义LGBM回归模型
model = LGBMRegressor(
    objective="regression",
    metric="rmse",
    random_state=1,
    n_estimators=100
)

# 训练模型
model.fit(X_train, y_train, eval_set=[(X_test, y_test)])

# 预测与评估
y_pred = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print(f"Test RMSE: {rmse:.4f}")

# 输出特征重要性
importance = pd.DataFrame({
    "feature": X.columns,
    "importance": model.feature_importances_
}).sort_values(by="importance", ascending=False)

print("\nFeature Importance:")
print(importance)

解决方案

方法1:使用feature_weights参数(优先推荐)

LightGBM原生支持通过feature_weights参数为指定特征设置权重,权重越高,特征被选中作为分裂节点的概率越大。这是最直接的参数化方案,无需修改数据或底层逻辑。

修改后的代码:

from lightgbm import LGBMRegressor
import pandas as pd
import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# 生成回归数据集
X, y = make_regression(n_samples=1000, n_features=10, noise=0.9, random_state=42)
feature_names = [f"feature_{i}" for i in range(X.shape[1])]

# 转为DataFrame
X = pd.DataFrame(X, columns=feature_names)

# 添加需要强制使用的特征(模拟业务重要特征)
X["must_use_feature"] = 1

# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 定义特征权重:给目标特征设置远高于其他特征的权重
feature_weights = {col: 1 for col in X.columns}
feature_weights["must_use_feature"] = 100  # 权重值可根据需求调整

# 初始化模型,传入feature_weights参数
model = LGBMRegressor(
    objective="regression",
    metric="rmse",
    random_state=1,
    n_estimators=100,
    feature_weights=feature_weights  # 关键参数
)

# 训练模型
model.fit(X_train, y_train, eval_set=[(X_test, y_test)])

# 预测与评估
y_pred = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print(f"Test RMSE: {rmse:.4f}")

# 查看特征重要性
importance = pd.DataFrame({
    "feature": X.columns,
    "importance": model.feature_importances_
}).sort_values(by="importance", ascending=False)

print("\nFeature Importance:")
print(importance)

方法2:给特征添加微小噪声+权重(针对常量/低区分度特征)

如果目标特征是常量或区分度极低的特征,模型无法找到有效分裂点,此时可以给特征添加微小噪声,同时配合feature_weights参数,让模型能捕捉到分裂信号。

示例代码:

# 给必须使用的特征添加微小高斯噪声,保留业务含义的同时提供分裂依据
X_train["must_use_feature"] = X_train["must_use_feature"] + np.random.normal(0, 1e-3, size=len(X_train))
X_test["must_use_feature"] = X_test["must_use_feature"] + np.random.normal(0, 1e-3, size=len(X_test))

# 设置特征权重
feature_weights = {col: 1 for col in X.columns}
feature_weights["must_use_feature"] = 50

# 初始化并训练模型
model = LGBMRegressor(
    objective="regression",
    metric="rmse",
    random_state=1,
    n_estimators=100,
    feature_weights=feature_weights
)

model.fit(X_train, y_train, eval_set=[(X_test, y_test)])

# 查看特征重要性
importance = pd.DataFrame({
    "feature": X.columns,
    "importance": model.feature_importances_
}).sort_values(by="importance", ascending=False)
print("\nFeature Importance:")
print(importance)

方法3:自定义分裂增益(高级方案)

如果需要更严格的强制逻辑,可以通过修改LightGBM的分裂增益计算函数,让目标特征的分裂增益被人为放大。这种方法需要对LightGBM源码有一定了解,适合深度定制场景。

内容的提问来源于stack exchange,提问作者PeCaDe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:53:23