使用dtreeviz可视化决策

2024-04-25 15:27:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我喜欢Dtreeviz library - GitHub提供的决策树可视化,可以使用

# Install libraries
!pip install dtreeviz
!apt-get install graphviz

# Sample code
from sklearn.datasets import *
from sklearn import tree
from dtreeviz.trees import *
from IPython.core.display import display, HTML

classifier = tree.DecisionTreeClassifier(max_depth=4)
cancer = load_breast_cancer()

classifier.fit(cancer.data, cancer.target)
viz = dtreeviz(classifier,
               cancer.data,
               cancer.target,
               target_name='cancer',
               feature_names=cancer.feature_names, 
               class_names=["malignant", "benign"],
               fancy=False) 

display(HTML(viz.svg()))

然而,当我将上述方法应用于我自己制作的dtree时,代码会爆炸,因为我的数据在pandas DF(或np数组)中,而不是scikit learn bunch对象。

现在,在Sci-kit learn - How to create a Bunch object他们非常严厉地告诉我不要试图创建一个bunch对象;但是我也没有技能将DF或NP数组转换成上面的viz函数可以接受的东西。

我们可以假设我的测向有九个特征和一个目标,叫做“Feature01”、“Feature02”等和“Target01”。

这个我通常会分开

^{pr2}$

然后按照我愉快的方式分配一个分类器,或者如果对于ML,创建一个测试/训练分割。

当调用dtreeviz时,这些都没有帮助,因为它需要像“feature_names”(我认为它是“bunch”对象中包含的内容)。因为我不能把我的DF转换成一堆,所以我被卡住了。哦,请带上你的智慧。

更新:我想任何简单的DF都能说明我的难题。我们可以一起荡秋千

import pandas as pd

Things = {'Feature01': [3,4,5,0], 
          'Feature02': [4,5,6,0], 
          'Feature03': [1,2,3,8], 
          'Target01': ['Red','Blue','Teal','Red']}
DF = pd.DataFrame(Things,
                  columns= ['Feature01', 'Feature02', 
                            'Feature02', 'Target01']) 

例如DF。现在,我可以走了吗

DataNP = DF.to_numpy()
classifier.fit(DF.data, DF.target)
feature_names = ['Feature01', 'Feature02', 'Feature03'] 
#..and what if I have 50 features...

viz = dtreeviz(classifier,
               DF.data,
               DF.target,
               target_name='Target01',
               feature_names=feature_names, 
               class_names=["Red", "Blue", "Teal"],
               fancy=False) 

还是这愚蠢?感谢您的指导!


Tags: fromimporttargetdfdatanamesdisplayfeature
2条回答
  • sklearn的决策树需要数值目标值
  • 可以使用sklearn的LabelEncoder将字符串转换为整数

    from sklearn import preprocessing
    
    label_encoder = preprocessing.LabelEncoder()
    label_encoder.fit(df.Target01)
    
    df['target'] = label_encoder.transform(df.Target01)
    
  • dtreeviz期望class_nameslist或{},所以让我们从label_encoder得到它

    class_names = list(label_encoder.classes_)        
    

完整代码

import pandas as pd
from sklearn import preprocessing, tree
from dtreeviz.trees import dtreeviz

Things = {'Feature01': [3,4,5,0], 
          'Feature02': [4,5,6,0], 
          'Feature03': [1,2,3,8], 
          'Target01': ['Red','Blue','Teal','Red']}
df = pd.DataFrame(Things,
                  columns= ['Feature01', 'Feature02', 
                            'Feature02', 'Target01']) 

label_encoder = preprocessing.LabelEncoder()
label_encoder.fit(df.Target01)
df['target'] = label_encoder.transform(df.Target01)

classifier = tree.DecisionTreeClassifier()
classifier.fit(df.iloc[:,:3], df.target)

dtreeviz(classifier,
         df.iloc[:,:3],
         df.target,
         target_name='toy',
         feature_names=df.columns[0:3],
         class_names=list(label_encoder.classes_)
         )

enter image description here


旧答案

让我们使用cancer数据集来创建Pandas数据帧

df = pd.DataFrame(cancer.data, columns=cancer.feature_names)
df['target'] = cancer.target

它给出了下面的数据帧。在

mean radius mean texture    mean perimeter  mean area   mean smoothness mean compactness    mean concavity  mean concave points mean symmetry   mean fractal dimension  radius error    texture error   perimeter error area error  smoothness error    compactness error   concavity error concave points error    symmetry error  fractal dimension error worst radius    worst texture   worst perimeter worst area  worst smoothness    worst compactness   worst concavity worst concave points    worst symmetry  worst fractal dimension target
0   17.99   10.38   122.8   1001.0  0.1184  0.2776  0.3001  0.1471  0.2419  0.07871 1.095   0.9053  8.589   153.4   0.006399    0.04904 0.05373 0.01587 0.03003 0.006193    25.38   17.33   184.6   2019.0  0.1622  0.6656  0.7119  0.2654  0.4601  0.1189  0
1   20.57   17.77   132.9   1326.0  0.08474 0.07864 0.0869  0.07017 0.1812  0.05667 0.5435  0.7339  3.398   74.08   0.005225    0.01308 0.0186  0.0134  0.01389 0.003532    24.99   23.41   158.8   1956.0  0.1238  0.1866  0.2416  0.186   0.275   0.08902 0
2   19.69   21.25   130.0   1203.0  0.1096  0.1599  0.1974  0.1279  0.2069  0.05999 0.7456  0.7869  4.585   94.03   0.00615 0.04006 0.03832 0.02058 0.0225  0.004571    23.57   25.53   152.5   1709.0  0.1444  0.4245  0.4504  0.243   0.3613  0.08758 0
[...]
568 7.76    24.54   47.92   181.0   0.05263 0.04362 0.0 0.0 0.1587  0.05884 0.3857  1.428   2.548   19.15   0.007189    0.00466 0.0 0.0 0.02676 0.002783    9.456   30.37   59.16   268.6   0.08996 0.06444 0.0 0.0 0.2871  0.07039 1

对于你的分类器,它可以用以下方式使用。在

classifier.fit(df.iloc[:,:-1], df.target)

也就是说,只需将最后一列作为训练/输入,target列作为输出/目标。在

可视化也是一样:

viz = dtreeviz(classifier,
               df.iloc[:,:-1],
               df.target,
               target_name='cancer',
               feature_names=df.columns[0:-1],
               class_names=["malignant", "benign"]) 

我认为您对文档中提供的示例感到困惑。在

Here让我们看看iris数据集的示例。在

from sklearn.datasets import *

# Loading iris data
iris = load_iris()

# Type of iris
type(iris)
<class 'sklearn.utils.Bunch'>

如您所述,数据集存储为sklearn Bunch对象。在

但是dtreeviz没有在其任何参数中使用此对象。所有参数都是numpy数组。在

^{pr2}$

因此很明显,dtreeviz方法正在使用numpy数组,并且没有使用Bunch对象。在您的例子中,要素名称与选定要素的列名无关。在

更新

# Replace the following the the sample code to fit your dataframe
cancer.data <> DF.iloc[:, :-1]
cancer.target <> DF['Target01']

# Other parameters
feature_names = DF.columns[:-1]
class_names = DF['Target01'].unique()

相关问题 更多 >