Python案例如何用Scikit-learn做LightGBM

wen python案例 1

本文目录导读:

Python案例如何用Scikit-learn做LightGBM

  1. 安装LightGBM
  2. 基础分类案例
  3. 回归问题示例
  4. 特征重要性
  5. 超参数调优
  6. 早停法
  7. 完整的工作流程示例
  8. 关键参数说明
  9. 优势特点

我来帮你了解如何使用Scikit-learn接口来使用LightGBM,LightGBM有与Scikit-learn兼容的API接口。

安装LightGBM

pip install lightgbm scikit-learn pandas numpy

基础分类案例

二分类问题示例(使用鸢尾花数据集)

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import lightgbm as lgb
import matplotlib.pyplot as plt
# 加载数据
iris = load_iris()
X = iris.data
y = iris.target
# 只取两个类别做二分类
mask = y != 2
X = X[mask]
y = y[mask]
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
)
# 创建LGBM分类器
lgb_model = lgb.LGBMClassifier(
    n_estimators=100,  # 树的数量
    learning_rate=0.1,  # 学习率
    max_depth=5,  # 最大深度
    num_leaves=31,  # 叶子节点数
    random_state=42
)
# 训练模型
lgb_model.fit(X_train, y_train)
# 预测
y_pred = lgb_model.predict(X_test)
y_pred_proba = lgb_model.predict_proba(X_test)
# 评估
print("准确率:", accuracy_score(y_test, y_pred))
print("\n分类报告:")
print(classification_report(y_test, y_pred))
print("混淆矩阵:")
print(confusion_matrix(y_test, y_pred))

多分类问题示例

from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
# 加载数字数据集
digits = load_digits()
X = digits.data
y = digits.target
# 数据标准化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# 划分数据
X_train, X_test, y_train, y_test = train_test_split(
    X_scaled, y, test_size=0.2, random_state=42
)
# 多分类LGBM
lgb_multi = lgb.LGBMClassifier(
    n_estimators=200,
    learning_rate=0.05,
    max_depth=8,
    num_leaves=50,
    objective='multiclass',
    num_class=10,
    random_state=42,
    n_jobs=-1
)
# 训练
lgb_multi.fit(X_train, y_train)
# 预测
y_pred_multi = lgb_multi.predict(X_test)
# 评估
accuracy = accuracy_score(y_test, y_pred_multi)
print(f"多分类准确率: {accuracy:.4f}")
print("\n分类报告:")
print(classification_report(y_test, y_pred_multi))

回归问题示例

from sklearn.datasets import fetch_california_housing
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import cross_val_score
# 加载房价数据集
housing = fetch_california_housing()
X = housing.data
y = housing.target
# 划分数据
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)
# 创建LGBM回归器
lgb_reg = lgb.LGBMRegressor(
    n_estimators=200,
    learning_rate=0.1,
    max_depth=6,
    num_leaves=31,
    random_state=42,
    n_jobs=-1
)
# 训练模型
lgb_reg.fit(X_train, y_train)
# 预测
y_pred = lgb_reg.predict(X_test)
# 评估
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"均方误差 (MSE): {mse:.4f}")
print(f"R² 分数: {r2:.4f}")
# 交叉验证
cv_scores = cross_val_score(lgb_reg, X, y, cv=5, scoring='r2')
print(f"交叉验证 R² 分数: {cv_scores}")
print(f"平均 R² 分数: {cv_scores.mean():.4f} (+/- {cv_scores.std() * 2:.4f})")

特征重要性

# 特征重要性可视化
feature_importance = pd.DataFrame({
    'feature': housing.feature_names,
    'importance': lgb_reg.feature_importances_
})
feature_importance = feature_importance.sort_values('importance', ascending=False)
plt.figure(figsize=(10, 6))
plt.bar(feature_importance['feature'], feature_importance['importance'])
plt.xticks(rotation=45)'LightGBM Feature Importance')
plt.tight_layout()
plt.show()
print("特征重要性排名:")
print(feature_importance)

超参数调优

from sklearn.model_selection import GridSearchCV
# 定义参数网格
param_grid = {
    'n_estimators': [50, 100, 200],
    'learning_rate': [0.01, 0.05, 0.1],
    'max_depth': [3, 5, 7],
    'num_leaves': [15, 31, 50],
    'min_child_samples': [10, 20, 30]
}
# 创建基础模型
lgb_base = lgb.LGBMClassifier(random_state=42, n_jobs=-1)
# 网格搜索
grid_search = GridSearchCV(
    estimator=lgb_base,
    param_grid=param_grid,
    cv=3,
    scoring='accuracy',
    n_jobs=-1,
    verbose=1
)
# 运行网格搜索
grid_search.fit(X_train, y_train)
# 最佳参数
print("最佳参数:", grid_search.best_params_)
print("最佳分数:", grid_search.best_score_)
# 使用最佳参数重新训练
best_lgb = grid_search.best_estimator_
y_pred_best = best_lgb.predict(X_test)
print(f"优化后准确率: {accuracy_score(y_test, y_pred_best):.4f}")

早停法

# 使用早停法防止过拟合
X_train, X_val, y_train, y_val = train_test_split(
    X_train, y_train, test_size=0.2, random_state=42
)
lgb_early = lgb.LGBMClassifier(
    n_estimators=1000,  # 设置较大的数值
    learning_rate=0.1,
    random_state=42
)
# 使用早停法训练
lgb_early.fit(
    X_train, y_train,
    eval_set=[(X_val, y_val)],
    eval_metric='logloss',
    early_stopping_rounds=20,  # 20轮没有改善就停止
    verbose=False
)
print(f"早停法使用的迭代次数: {lgb_early.best_iteration_}")

完整的工作流程示例

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score
import lightgbm as lgb
import warnings
warnings.filterwarnings('ignore')
def lgbm_classification_pipeline(X, y, test_size=0.2, random_state=42):
    """完整的LGBM分类工作流程"""
    # 1. 数据划分
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=test_size, random_state=random_state
    )
    # 2. 数据标准化
    scaler = StandardScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)
    # 3. 基础模型
    base_model = lgb.LGBMClassifier(random_state=random_state)
    # 4. 交叉验证
    cv_scores = cross_val_score(base_model, X_train_scaled, y_train, cv=5)
    print(f"交叉验证分数: {cv_scores.mean():.4f} (+/- {cv_scores.std() * 2:.4f})")
    # 5. 训练模型
    base_model.fit(X_train_scaled, y_train)
    # 6. 预测
    y_pred = base_model.predict(X_test_scaled)
    y_pred_proba = base_model.predict_proba(X_test_scaled)
    # 7. 评估
    print("\n分类报告:")
    print(classification_report(y_test, y_pred))
    # ROC-AUC (二分类)
    if len(np.unique(y)) == 2:
        auc_score = roc_auc_score(y_test, y_pred_proba[:, 1])
        print(f"ROC-AUC Score: {auc_score:.4f}")
    # 8. 特征重要性
    feature_importance = pd.DataFrame({
        'feature': [f'feature_{i}' for i in range(X.shape[1])],
        'importance': base_model.feature_importances_
    }).sort_values('importance', ascending=False)
    print("\nTop 5 重要特征:")
    print(feature_importance.head())
    return base_model, feature_importance
# 使用示例
# model, importance = lgbm_classification_pipeline(X, y)

关键参数说明

  • n_estimators: 提升迭代次数/树的数量
  • learning_rate: 学习率,控制每棵树的贡献
  • max_depth: 树的最大深度
  • num_leaves: 每棵树的叶子节点数
  • min_child_samples: 叶子节点最小样本数
  • subsample: 训练每棵树时的样本采样比例
  • colsample_bytree: 特征采样比例
  • reg_alpha: L1正则化
  • reg_lambda: L2正则化

优势特点

  1. 速度快: 基于直方图的算法,训练速度快
  2. 内存效率高: 内存占用相对较少
  3. 高准确率: 通常能达到很好的精度
  4. 处理类别特征: 原生支持类别特征
  5. GPU支持: 支持GPU加速

这些案例涵盖了LightGBM在Scikit-learn框架下的主要应用场景,你可以根据实际需求调整参数和优化策略。

抱歉,评论功能暂时关闭!