<jupyter_start><jupyter_text>データの取得<jupyter_code># ライブラリの読み込み
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_breast_cancer
from pathlib import Path
from sklearn.model_selection import train_test_split
import lightgbm as lgb
# サンプルデータセットの読み込み
data = load_breast_cancer()
# pandas Dataframe形式で説明変数Xを取得
df_X = pd.DataFrame(data.data, columns=data.feature_names)
# pandas Series形式で目的変数yを取得
s_y = pd.Series(data.target)
# pandas Dataframe形式で目的変数と説明変数を結合したXyを取得
df_Xy = pd.concat([df_X, s_y], axis=1, join='inner')
# colum最終列にある目的変数に名前がついていないので"target'という名前に変更
df_Xy = df_Xy.rename(columns={0:"target"})<jupyter_output><empty_output><jupyter_text>特徴量重要度の可視化用関数<jupyter_code># 特徴量重要度を棒グラフでプロットする関数 
def plot_feature_importance(df): 
    n_features = len(df)                              # 特徴量数(説明変数の個数) 
    df_plot = df.sort_values('importance')            # df_importanceをプロット用に特徴量重要度を昇順ソート 
    f_importance_plot = df_plot['importance'].values  # 特徴量重要度の取得 
    plt.barh(range(n_features), f_importance_plot, align='center') 
    cols_plot = df_plot['feature'].values             # 特徴量の取得 
    plt.yticks(np.arange(n_features), cols_plot)      # x軸,y軸の値の設定
    plt.xlabel('Feature importance')                  # x軸のタイトル
    plt.ylabel('Feature')                             # y軸のタイトル<jupyter_output><empty_output><jupyter_text>データ分割<jupyter_code># 全体の20%をテストデータとして分割
X_train, X_test, y_train, y_test = train_test_split(df_X, s_y,test_size=0.20, random_state=2)<jupyter_output><empty_output><jupyter_text>モデルの学習<jupyter_code># 学習に使用するデータを設定
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train) 

# LightGBM parameters
params = {
        'task': 'train',
        'boosting_type': 'gbdt',
        'objective': 'binary',        # 目的 : 2クラス分類  
        'metric': {'binary_error'},   # 評価指標 : 誤り率(= 1-正答率) 
}

# モデルの学習
model = lgb.train(params,
                  train_set=lgb_train, # トレーニングデータの指定
                  valid_sets=lgb_eval, # 検証データの指定
                  )<jupyter_output>[LightGBM] [Warning] Find whitespaces in feature_names, replace with underlines
[LightGBM] [Info] Number of positive: 288, number of negative: 167
[LightGBM] [Warning] Auto-choosing col-wise multi-threading, the overhead of testing was 0.000460 seconds.
You can set `force_col_wise=true` to remove the overhead.
[LightGBM] [Info] Total Bins 4542
[LightGBM] [Info] Number of data points in the train set: 455, number of used features: 30
[LightGBM] [Warning] Find whitespaces in feature_names, replace with underlines
[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.632967 -> initscore=0.544967
[LightGBM] [Info] Start training from score 0.544967
[LightGBM] [Warning] No further splits with positive gain, best gain: -inf
[1]	valid_0's binary_error: 0.394737
[LightGBM] [Warning] No further splits with positive gain, best gain: -inf
[2]	valid_0's binary_error: 0.394737
[LightGBM] [Warning] No further splits with positive gain, best gain: -inf
[3]	valid_0's binary_error: 0.0701754
[LightGBM] [Wa[...]<jupyter_text>特徴量重要度の算出<jupyter_code># 特徴量重要度の算出 (データフレームで取得)
cols = list(df_X.columns)     # 特徴量名のリスト(目的変数target以外)
# 特徴量重要度の算出方法 'gain'(推奨) : トレーニングデータの損失の減少量を評価
f_importance = np.array(model.feature_importance(importance_type='gain')) # 特徴量重要度の算出 //
f_importance = f_importance / np.sum(f_importance) # 正規化(必要ない場合はコメントアウト)
df_importance = pd.DataFrame({'feature':cols, 'importance':f_importance})
df_importance = df_importance.sort_values('importance', ascending=False) # 降順ソート
display(df_importance)<jupyter_output><empty_output><jupyter_text>特徴量重要度の可視化<jupyter_code># 特徴量重要度の可視化
n_display = 10 # 上位10個に制限
plot_feature_importance(df_importance.iloc[:n_display,:])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10877561744613767, 'fraction_numerical': 0.02233315817130846, 'mean_word_length': 5.062101910828026, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '535655', 'license': 'MIT', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1689, 'n_words': 277, 'path': 'feature_extraction/feature_extraction_2class.ipynb', 'repo_name': 'AtsushiMatsumoto/notebooks', 'size': '3806'}