简体   繁体   English

有没有办法手动修改从给定数据集学习到的决策树中设置的阈值?

[英]Is there any way to manually modify the thresholds set in the decision tree learnt from a given dataset?

I was trying to create a decision tree model using scikit-learn's module: tree .我试图使用 scikit-learn 的模块tree创建一个决策树模型。 Once I generated the model, I visualized the tree and the criteria based on which the decisions were made.生成模型后,我将树和决策所依据的标准可视化。 However, I wish to modify the thresholds in some criteria manually to see how the output changes for the same.但是,我希望手动修改某些标准中的阈值,以查看相同的输出如何变化。 Is there any method to do so?有什么方法可以这样做吗? Or any library that converts the decision tree into a bunch of if-else statements once it has learned the required thresholds from the dataset and vice-versa?或者任何库一旦从数据集中学习到所需的阈值,就会将决策树转换为一堆 if-else 语句,反之亦然?

I know that the thresholds chosen by the module are based on some impurity metrics like Gini-impurity, information gain, etc. However, I still would like to experiment with those threshold values.我知道模块选择的阈值基于一些杂质指标,如基尼杂质、信息增益等。但是,我仍然想用这些阈值进行试验。

Thanks!谢谢!

Yes, you can easily do this.是的,您可以轻松做到这一点。

A sklearn Decision Tree exposes its underlying tree through the tree_ attribute. sklearn决策树通过tree_属性公开其底层树。 This tree_ , among other things, have an attribute threshold , which is a numpy array containing threshold values of all nodes.除其他外,这个tree_有一个属性threshold ,它是一个包含所有节点阈值的 numpy 数组。 You can modify this array, thereby changing the thresholds.您可以修改此数组,从而更改阈值。

For example:例如:

X,y = load_breast_cancer(return_X_y=True)
dt = DecisionTreeClassifier(max_depth=3).fit(X,y)
print(dt.tree_.threshold)     #All the thresholds, size equals "dt.tree_.node_count"
dt.tree_.threshold[3] = 10.0  #Manually modifying a threshold    

To verify, If you compare accuracy on a seperate test set before and after this modification (assuming you've modified a non-leaf node), you should notice a change (which is likely to be worse).为了验证,如果您在此修改之前和之后比较单独测试集的准确性(假设您修改了一个非叶节点),您应该注意到一个变化(这可能更糟)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM