python - 在 Jupyter Notebook 中可视化决策树

标签 python scikit-learn decision-tree

有没有办法在 Jupyter Notebook 上“分解”以下树?它是一个简单的决策树,但我不知道是什么让它看起来崩溃了。以下是相关的代码片段和树本身。

%matplotlib inline
%config InlineBackend.figure_format = 'retina'
from matplotlib import pyplot as plt
plt.rcParams['figure.figsize'] = (10, 8)

import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import collections
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score


#some more code


# Some feature values are present in train and absent in test and vice-versa.
y = df_train['Should_Vacation_there']
df_train, df_test = intersect_features(train=df_train, test=df_test)
df_train

#training a decision tree
dt = DecisionTreeClassifier(criterion='entropy', random_state=17)
dt.fit(df_train, y);

#displaying the tree
plot_tree(dt, feature_names=df_train.columns, filled=True,
         class_names=["Should go there", "Shouldn't go there"]);

enter image description here

最佳答案

#%config InlineBackend.figure_format = 'retina' 是这里的罪魁祸首。对其进行注释会生成格式良好的树。

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import collections
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

%matplotlib inline
#%config InlineBackend.figure_format = 'retina'

iris = load_iris()
plt.rcParams['figure.figsize'] = (10, 8)

#some more code
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)

# Some feature values are present in train and absent in test and vice-versa.
#y = df_train['Should_Vacation_there']
#df_train, df_test = intersect_features(train=df_train, test=df_test)
#df_train

#training a decision tree
dt = DecisionTreeClassifier(criterion='entropy', random_state=17)
dt.fit(X_train, y_train)

#displaying the tree
plot_tree(dt, feature_names=iris.feature_names, filled=True,
         class_names=iris.target_names);

enter image description here

关于python - 在 Jupyter Notebook 中可视化决策树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63549299/

相关文章:

python - 如何使用 scikit-learn 中的散列技巧对二元语法进行矢量化?

python - Sklearn.model_selection GridsearchCV ValueError : C <= 0

python-3.x - 管道OrdinalEncoder ValueError找到未知类别

r - 绘制方决策树

python - 如何将用户输入分配给变量或列表

python - 哪种 SciPy 稀疏矩阵类最适合计算距离矩阵?

python - 为什么单棵树的随机森林比决策树分类器好得多?

python - 值错误 : Number of labels=34866 does not match number of samples=2

python - 如何从字典和 Pandas 列表中创建新的数据框

python - Flask 引发 TypeError : The view function did not return a valid response