lightgbm json模型结果能否迭代解析转成sql,求教!

#库包加载
import lightgbm as lgb
from sklearn.metrics import mean_squared_error
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
from sklearn import tree
from sklearn import metrics

# 加载数据
iris = load_iris()
# 加载数据
iris = load_iris()
feature_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
data = pd.DataFrame(iris.data, columns=feature_names)
data['target'] = iris.target

# 划分训练集和测试集

X_train, X_test, y_train, y_test = train_test_split(
                   data[feature_names], data['target'], test_size=0.2, random_state=42)
print("Train data length:", len(X_train))
print("Test data length:", len(X_test))

# 转换为Dataset数据格式
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)

# 参数
params = {
    'task': 'train',
    'boosting_type': 'gbdt',  # 设置提升类型
    'objective': 'regression',  # 目标函数
    'metric': {'l2', 'auc'},  # 评估函数
    'num_leaves': 31,  # 叶子节点数
    'learning_rate': 0.05,  # 学习速率
    'feature_fraction': 0.9,  # 建树的特征选择比例
    'bagging_fraction': 0.8,  # 建树的样本采样比例
    'bagging_freq': 5,  # k 意味着每 k 次迭代执行bagging
    'verbose': 1  # <0 显示致命的, =0 显示错误 (警告), >0 显示信息
}

# 模型训练
gbm = lgb.train(params, lgb_train, num_boost_round=2, valid_sets=lgb_eval)
#模型结果保存
lgbm_json = gbm.dump_model()
lgbm_json

#模型lgbm_json结果
{‘name’: ‘tree’,
‘version’: ‘v3’,
‘num_class’: 1,
‘num_tree_per_iteration’: 1,
‘label_index’: 0,
‘max_feature_idx’: 3,
‘objective’: ‘regression’,
‘average_output’: False,
‘feature_names’: [‘sepal_length’,
‘sepal_width’,
‘petal_length’,
‘petal_width’],
‘monotone_constraints’: [],
‘feature_infos’: {‘sepal_length’: {‘min_value’: 4.3,
‘max_value’: 7.7,
‘values’: []},
‘sepal_width’: {‘min_value’: 2, ‘max_value’: 4.4, ‘values’: []},
‘petal_length’: {‘min_value’: 1, ‘max_value’: 6.7, ‘values’: []},
‘petal_width’: {‘min_value’: 0.1, ‘max_value’: 2.5, ‘values’: []}},
‘tree_info’: [{‘tree_index’: 0,
‘num_leaves’: 3,
‘num_cat’: 0,
‘shrinkage’: 1,
‘tree_structure’: {‘split_index’: 0,
‘split_feature’: 2,
‘split_gain’: 49.12009811401367,
‘threshold’: 3.1500000000000004,
‘decision_type’: ‘<=’,
‘default_left’: True,
‘missing_type’: ‘None’,
‘internal_value’: 0.991667,
‘internal_weight’: 0,
‘internal_count’: 99,
‘left_child’: {‘leaf_index’: 0,
‘leaf_value’: 0.9434722218364995,
‘leaf_weight’: 36,
‘leaf_count’: 36},
‘right_child’: {‘split_index’: 1,
‘split_feature’: 2,
‘split_gain’: 12.203200340270996,
‘threshold’: 4.750000000000001,
‘decision_type’: ‘<=’,
‘default_left’: True,
‘missing_type’: ‘None’,
‘internal_value’: 1.01669,
‘internal_weight’: 63,
‘internal_count’: 63,
‘left_child’: {‘leaf_index’: 1,
‘leaf_value’: 0.9920833333550643,
‘leaf_weight’: 28,
‘leaf_count’: 28},
‘right_child’: {‘leaf_index’: 2,
‘leaf_value’: 1.03636904726958,
‘leaf_weight’: 35,
‘leaf_count’: 35}}}},
{‘tree_index’: 1,
‘num_leaves’: 3,
‘num_cat’: 0,
‘shrinkage’: 0.05,
‘tree_structure’: {‘split_index’: 0,
‘split_feature’: 2,
‘split_gain’: 44.33089828491211,
‘threshold’: 3.1500000000000004,
‘decision_type’: ‘<=’,
‘default_left’: True,
‘missing_type’: ‘None’,
‘internal_value’: 0,
‘internal_weight’: 0,
‘internal_count’: 99,
‘left_child’: {‘leaf_index’: 0,
‘leaf_value’: -0.04578472146143516,
‘leaf_weight’: 36,
‘leaf_count’: 36},
‘right_child’: {‘split_index’: 1,
‘split_feature’: 2,
‘split_gain’: 11.013400077819824,
‘threshold’: 4.750000000000001,
‘decision_type’: ‘<=’,
‘default_left’: True,
‘missing_type’: ‘None’,
‘internal_value’: 0.0237688,
‘internal_weight’: 63,
‘internal_count’: 63,
‘left_child’: {‘leaf_index’: 1,
‘leaf_value’: 0.00039583332836627965,
‘leaf_weight’: 28,
‘leaf_count’: 28},
‘right_child’: {‘leaf_index’: 2,
‘leaf_value’: 0.04246726287262781,
‘leaf_weight’: 35,
‘leaf_count’: 35}}}}],
‘feature_importances’: {‘petal_length’: 4},
‘pandas_categorical’: []

目的是希望能lgbm_json 通过josn递归解析把上述模型结果解析成sql case when 语句。谢谢!

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!