Python 如何让catboost可视化显示类别

Python 如何让catboost可视化显示类别,python,machine-learning,catboost,Python,Machine Learning,Catboost,考虑以下数据: import pandas as pd y_train = pd.DataFrame({0: {14194: 'Fake', 13891: 'Fake', 13247: 'Fake', 11236: 'Fake', 2716: 'Real', 2705: 'Real', 16133: 'Fake', 7652: 'Real', 7725: 'Real', 16183: 'Fake'}}) X_train = pd.DataFrame({'one': {14194: 'e',

考虑以下数据:

import pandas as pd
y_train = pd.DataFrame({0: {14194: 'Fake', 13891: 'Fake', 13247: 'Fake', 11236: 'Fake', 2716: 'Real', 2705: 'Real', 16133: 'Fake', 7652: 'Real', 7725: 'Real', 16183: 'Fake'}})

X_train = pd.DataFrame({'one': {14194: 'e',
  13891: 'b',
  13247: 'v',
  11236: 't',
  2716: 'e',
  2705: 'e',
  16133: 'h',
  7652: 's',
  7725: 's',
  16183: 's'},
 'two': {14194: 'a',
  13891: 'a',
  13247: 'e',
  11236: 'n',
  2716: 'c',
  2705: 'a',
  16133: 'n',
  7652: 'e',
  7725: 'h',
  16183: 'e'},
 'three': {14194: 's',
  13891: 'l',
  13247: 'n',
  11236: 'c',
  2716: 'h',
  2705: 'r',
  16133: 'i',
  7652: 'r',
  7725: 'e',
  16183: 's'},
 'four': {14194: 'd',
  13891: 'e',
  13247: 'r',
  11236: 'g',
  2716: 'o',
  2705: 'r',
  16133: 'p',
  7652: 'v',
  7725: 'r',
  16183: 'i'},
 'five': {14194: 'f',
  13891: 'b',
  13247: 'o',
  11236: 'b',
  2716: 'i',
  2705: 'i',
  16133: 'i',
  7652: 'i',
  7725: 'b',
  16183: 'i'},
 'six': {14194: 'p',
  13891: 's',
  13247: 'l',
  11236: 'l',
  2716: 'n',
  2705: 'n',
  16133: 'n',
  7652: 'l',
  7725: 'e',
  16183: 'u'},
 'seven': {14194: 's',
  13891: 's',
  13247: 's',
  11236: 'e',
  2716: 'g',
  2705: 'g',
  16133: 's',
  7652: 'e',
  7725: 't',
  16183: 'r'}})
以及以下代码:

from catboost import CatBoostClassifier
from catboost import Pool
cat_features = list(X_train.columns)
pool = Pool(X_train, y_train, cat_features=list(range(7)), feature_names=cat_features)
model = CatBoostClassifier(verbose=0).fit(pool)
model.plot_tree(
tree_idx=1,
pool=pool # "pool" is required parameter for trees with one hot features
)
我得到以下信息:

但是我不明白{five}pr_num0 tb0 type0,value>8意味着什么。我希望它看起来像手册中的泰坦尼克号示例:

import catboost
from catboost import CatBoostClassifier, Pool

from catboost.datasets import titanic
titanic_df = titanic()

X = titanic_df[0].drop('Survived',axis=1)
y = titanic_df[0].Survived

is_cat = (X.dtypes != float)
for feature, feat_is_cat in is_cat.to_dict().items():
    if feat_is_cat:
        X[feature].fillna("NAN", inplace=True)

cat_features_index = np.where(is_cat)[0]
pool = Pool(X, y, cat_features=cat_features_index, feature_names=list(X.columns))

model = CatBoostClassifier(
    max_depth=2, verbose=False, max_ctr_complexity=1, iterations=2).fit(pool)

model.plot_tree(
    tree_idx=0,
    pool=pool
)
这使得:


例如,我怎样才能得到性的等价物,value=Female?例如,
One,value=b

TLDR这不是一个真正的可视化问题,而是更多关于如何在Catboost中进行功能拆分的内容

Catboost根据一个名为
one\u hot\u max\u size
的参数来决定哪一个功能是一个hot,哪一个ctr。如果要素中的类数为1,2,3),则按ctr处理。将其设置得足够高将允许您强制catboost将您的列编码为一个热列

{five}pr_num0 tb0 type0,value>8
基本上是ctr拆分的标签和值。没有关于这方面的文档,但是在检查github repo之后,似乎标签是使用多重散列生成的

更多详情见下文


如何选择要素拆分? 分3步为叶选择
特征分割
对:

  • 列表由可能的候选对象(“特征分割对”)组成,这些候选对象将被分配给作为分割的叶
  • 为每个对象计算多个惩罚函数(条件是从步骤1获得的所有候选对象都已分配给叶)
  • 将选择惩罚最小的拆分
  • 要素拆分的类型 有三种类型的拆分:
    FloatFeature
    OneHotFeature
    OnlineCtr
    。这些是基于对特征进行的编码

    from catboost import CatBoostClassifier, Pool
    import pandas as pd
    
    X_train.describe().loc['unique']
    
  • FloatFeature:浮点特征分割采用浮点型特征,并计算分割值(边框)。浮动特征在可视化中表示为特征索引和边界值():
  • OnlineCtr:ctr您可以在catboost模型中看到的第三种分割类型。对于与一个热编码()一起使用的功能,不计算CTR。如果功能中可能的类数超过了
    one_hot\u max_size
    设置的限制,则catboost会自动使用ctr对功能进行编码,因此拆分类型为OnlineCtr。其表示为特征名称、一些表示唯一类和值的伪标记:
  • 分析手头的数据集 让我们首先看看每个特性中唯一类的数量

    from catboost import CatBoostClassifier, Pool
    import pandas as pd
    
    X_train.describe().loc['unique']
    
    如您所见,唯一类的最小数量是4(在特性中称为“5”),最大数量是8。让我们设置我们的
    one\u hot\u max\u size=4

    cat_features = list(X_train.columns)
    pool = Pool(X_train, y_train, cat_features=list(range(7)), feature_names=cat_features)
    model = CatBoostClassifier(verbose=0, one_hot_max_size=4).fit(pool)
    
    model.plot_tree(tree_idx=1,pool=pool)
    

    功能“五”现在是
    OneHotFeature
    ,并导致拆分描述
    five,value=i
    。但是,功能“一”仍然是一个
    在线中心

    现在让我们设置
    one\u hot\u max\u size=8
    ,这是最大可能的唯一类。这将确保每个功能都是
    OneHotFeature
    ,而不是
    OnlineCtr

    cat_features = list(X_train.columns)
    pool = Pool(X_train, y_train, cat_features=list(range(7)), feature_names=cat_features)
    model = CatBoostClassifier(verbose=0, one_hot_max_size=8).fit(pool)
    
    model.plot_tree(tree_idx=1,pool=pool)
    


    希望这能澄清您的问题,为什么《泰坦尼克号》中的《性》与您正在使用的功能相比,以不同的方式显示

    欲了解更多关于此的信息,请查看以下链接-


  • 这似乎是使用
    repr
    时出现的问题。有没有一种使用自定义字符串发生器功能的方法?@wizzwizz4我很想知道。你能详细谈谈你对此的看法吗?我提供的示例是完整的,因此您应该能够在PC上运行它,并获得相同的输出。谢谢您的回答。我仍然感到沮丧的是,对于一个大的数据集,如果您将一个hot\u max\u size设置为大,那么即使它现在是可解释的,也会得到一个更差的分类器。这似乎是因为它不能再进行“功能组合”了。我建议将基数较小的功能保持为一个热门功能,并将基数较大的功能拆分为ctr。
    one      6
    two      5
    three    8
    four     8
    five     4
    six      6
    seven    5
    Name: unique, dtype: object
    
    cat_features = list(X_train.columns)
    pool = Pool(X_train, y_train, cat_features=list(range(7)), feature_names=cat_features)
    model = CatBoostClassifier(verbose=0, one_hot_max_size=4).fit(pool)
    
    model.plot_tree(tree_idx=1,pool=pool)
    
    cat_features = list(X_train.columns)
    pool = Pool(X_train, y_train, cat_features=list(range(7)), feature_names=cat_features)
    model = CatBoostClassifier(verbose=0, one_hot_max_size=8).fit(pool)
    
    model.plot_tree(tree_idx=1,pool=pool)