u011459315
玉璋
采纳率0%
2020-09-10 14:31

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_jsom如下,想请教如何把下面模型结果通过json迭代解析成sql

{'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}}}}],
 'feature_importances': {'petal_length': 2},
 'pandas_categorical': []}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

相关推荐