如何解释sklearn决策树tree_中的children_left属性

https://stackoverflow.com/questions/42075630/how-to-interpret-the-children-left-attributes-in-sklearn-decision-tree-tree

指出下列代码哪些是叶子节点import pandas as pd import numpy as np from sklearn.datasets import make_classification def decision_tree_binning(x_value: np.ndarray, y_value: np.ndarray, max_bin=10) -> list: '''利用决策树获得最优分箱的边界值列表''' from sklearn.tree import DecisionTreeClassifier clf = DecisionTreeClassifier( criterion='gini', # 选择“信息熵”或基尼系数 max_leaf_nodes=max_bin, # 最大叶子节点数 min_samples_leaf=0.05) # 叶子节点样本数量最小占比 clf.fit(x_value.reshape(-1, 1), y_value) # 训练决策树 # 绘图 import matplotlib.pyplot as plt from sklearn.tree import plot_tree plt.figure(figsize=(14, 12)) # 指定图片大小 plot_tree(clf) plt.show() # 根据决策树进行分箱 n_nodes = clf.tree_.node_count # 决策树节点 children_left = clf.tree_.children_left children_right = clf.tree_.children_right threshold = clf.tree_.threshold # 开始分箱 boundary = [] for i in range(n_nodes): if children_left[i] != children_right[i]: # 获得决策树节点上的划分边界值 boundary.append(threshold[i]) boundary.sort() min_x = x_value.min() max_x = x_value.max() # max_x = x_value.max() + 0.1 # +0.1是为了考虑后续groupby操作时,能包含特征最大值的样本 boundary = [min_x] + boundary + [max_x] return boundary if __name__ == '__main__': data_x, data_y = make_classification(n_samples=100, n_classes=2, n_features=20, n_informative=2, random_state=None) bin_result = decision_tree_binning(data_x[:, 0], data_y, max_bin=20) bin_value = pd.cut(data_x[:, 0], bin_result).codes # 分箱的结果
最新发布
05-31
import pandas as pd import numpy as np from sklearn.datasets import make_classification def decision_tree_binning(x_value: np.ndarray, y_value: np.ndarray, max_bin=10) -> list: '''利用决策树获得最优分箱的边界值列表''' from sklearn.tree import DecisionTreeClassifier clf = DecisionTreeClassifier( criterion='', # 选择“信息熵”或基尼系数 max_leaf_nodes=max_bin, # 最大叶子节点数 min_samples_leaf=0.05) # 叶子节点样本数量最小占比 clf.fit(x_value.reshape(-1, 1), y_value) # 训练决策树 # 绘图 import matplotlib.pyplot as plt from sklearn.tree import plot_tree plt.figure(figsize=(14, 12)) # 指定图片大小 plot_tree(clf) plt.show() # 根据决策树进行分箱 n_nodes = clf.tree_.node_count # 决策树节点 children_left = clf.tree_.children_left children_right = clf.tree_.children_right threshold = clf.tree_.threshold # 开始分箱 boundary = [] for i in range(n_nodes): if children_left[i] != children_right[i]: # 获得决策树节点上的划分边界值 boundary.append(threshold[i]) boundary.sort() min_x = x_value.min() max_x = x_value.max() # max_x = x_value.max() + 0.1 # +0.1是为了考虑后续groupby操作时,能包含特征最大值的样本 boundary = [min_x] + boundary + [max_x] return boundary if __name__ == '__main__': data_x, data_y = make_classification(n_samples=, n_classes=, n_features=, n_informative=, random_state=) bin_result = decision_tree_binning(data_x[:, 0], data_y, max_bin=) bin_value = pd.cut(data_x[:, 0], bin_result).codes # 分箱的结果这个代码错在哪
04-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值