2024-10-04 09:18:24 发布
网友
我有一个来自sklearn的决策树分类器,我使用pydotplus来展示它。 然而,我不太喜欢在每个节点上都有大量的信息(熵、样本和值)。在
为了更容易地向人们解释,我只想保留决定权和全班同学的意见。 在哪里可以修改代码?在
谢谢。在
根据documentation,不可能不在框内设置附加信息。唯一可以隐式忽略的是impurity参数。在
impurity
然而,我已经做了另一个明确的方式,有点歪。首先,我保存.dot文件,将杂质设置为False。然后,我打开它并将其转换为字符串格式。我使用正则表达式减去多余的标签并重新保存。在
代码是这样的:
import pydotplus # pydot library: install it via pip install pydot from sklearn.tree import DecisionTreeClassifier from sklearn.tree import export_graphviz from sklearn.datasets import load_iris data = load_iris() clf = DecisionTreeClassifier() clf.fit(data.data, data.target) export_graphviz(clf, out_file='tree.dot', impurity=False, class_names=True) PATH = '/path/to/dotfile/tree.dot' f = pydot.graph_from_dot_file(PATH).to_string() f = re.sub('(\\\\nsamples = [0-9]+)(\\\\nvalue = \[[0-9]+, [0-9]+, [0-9]+\])', '', f) f = re.sub('(samples = [0-9]+)(\\\\nvalue = \[[0-9]+, [0-9]+, [0-9]+\])\\\\n', '', f) with open('tree_modified.dot', 'w') as file: file.write(f)
以下是修改前后的图像:
在您的例子中,框中似乎有更多的参数,所以您可能需要稍微调整一下代码。在
我希望这有帮助!在
根据documentation,不可能不在框内设置附加信息。唯一可以隐式忽略的是
impurity
参数。在然而,我已经做了另一个明确的方式,有点歪。首先,我保存.dot文件,将杂质设置为False。然后,我打开它并将其转换为字符串格式。我使用正则表达式减去多余的标签并重新保存。在
代码是这样的:
以下是修改前后的图像:
在您的例子中,框中似乎有更多的参数,所以您可能需要稍微调整一下代码。在
我希望这有帮助!在
相关问题 更多 >
编程相关推荐