LoginSignup
1
1

More than 1 year has passed since last update.

LightGBM

Last updated at Posted at 2023-04-02

LightGBM

https://lightgbm.readthedocs.io/en/latest/index.html
分類モデル

.py
from lightgbm import LGBMClassifier
model=LGBMClassifier(
    objective='binary',    #---------2値分類:'binary', 3値以上分類:'multiclass'
    n_estimators=100,      #---------繰り返し数(作成する決定木の数,デフォルト:100)
    max_depth=3            #---------作成する木の深さの最大値(大きいほど過学習傾向)
    learning_rate=0.1      #---------繰り返しごとに前のモデルを修正する係数(デフォルト:0.1)
    colsample_bytree=1,    #---------各決定木においてランダムに抽出される列の割合(大きいほど過学習寄り,デフォルト:1)
    num_leaves=31,         #---------1本の木の最大葉枚数(大きいほど過学習寄り,デフォルト:31)
    min_child_samples=20,  #---------1枚の葉に含まれる最小データ数(小さいほど過学習寄り,デフォルト:20)
    class_weight='balanced'#---------'balanced'に設定すると各クラスのサンプル数に応じて重みを付ける(各クラスのサンプル数に偏りがあるときに指定)
    subsample=1,           #---------各決定木においてランダムに抽出される標本の割合(大きいほど過学習寄り,デフォルト:1)
    subsample_freq=1       #---------サンプルを復元抽出(バギング)する頻度(小さいほど予測精度改善,ただし0では復元抽出しない,デフォルト0)

回帰モデル

.py
from lightgbm import LGBMRegressor
model=LGBMRegressor(
        n_estimators=100,      #---------繰り返し数(作成する決定木の数,デフォルト:100)
        max_depth=3            #---------作成する木の深さの最大値(大きいほど過学習傾向)
        learning_rate=0.1      #---------繰り返しごとに前のモデルを修正する係数(デフォルト:0.1)
        colsample_bytree=1,    #---------各決定木においてランダムに抽出される列の割合(大きいほど過学習寄り,デフォルト:1)
        num_leaves=31,         #---------1本の木の最大葉枚数(大きいほど過学習寄り,デフォルト:31)
        min_child_samples=20,  #---------1枚の葉に含まれる最小データ数(小さいほど過学習寄り,デフォルト:20)
        subsample=1,           #---------各決定木においてランダムに抽出される標本の割合(大きいほど過学習寄り,デフォルト:1)
        subsample_freq=1       #---------サンプルを復元抽出(バギング)する頻度(小さいほど予測精度改善,ただし0では復元抽出しない,デフォルト0)   
    )

特徴率の寄与率出力

.py
model.feature_importances_

ツリーを表示

.py
from lightgbm import plot_tree as l_plot_tree
import matplotlib.pyplot as plt

fig,ax=plt.subplots(figsize=(20,10))
l_plot_tree(model,ax=ax,tree_index=4)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1