如何让 catboost 可视化显示类别

Rap*_*ael 4 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',
  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'}})
Run Code Online (Sandbox Code Playgroud)

和以下代码:

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
)
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

在此输入图像描述

但我不明白 { 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
)
Run Code Online (Sandbox Code Playgroud)

这给出:

在此输入图像描述

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

Aks*_*gal 8

太长了;这实际上并不是一个可视化问题,而是更多关于如何在 Catboost 中完成特征分割的问题。

\n

Catboost 根据名为 的参数决定哪个功能为 one-hot 以及哪个为 ctr one_hot_max_size。如果某个特征中的类数量<= one_hot_max_size,那么它将被视为独热。默认情况下,其设置为 2。因此,只有二元特征(0,1 或男性、女性)被视为 one-hot,其他特征(例如 PClass -> 1,2,3)被视为 ctr。将其设置得足够高将允许您强制 catboost 将您的列编码为 one-hot。

\n

{five} pr_num0 tb0 type0, value>8基本上是一个标签,是 ctr 分割的值。没有可用的文档,但在检查 github 存储库后,似乎标签是使用多重哈希生成的。

\n

更多详细信息如下。

\n
\n

如何选择特征分割?

\n

feature-split通过 3 个步骤为叶子选择一对:

\n
    \n
  1. 列表由可能的候选者(\xe2\x80\x9c特征分割对\xe2\x80\x9d)组成,作为分割分配给叶子。
  2. \n
  3. 为每个对象计算多个惩罚函数(条件是从步骤 1 获得的所有候选都已分配给叶子)。
  4. \n
  5. 选择惩罚最小的分割。
  6. \n
\n

特征分割的类型

\n

分割分为三种类型:FloatFeatureOneHotFeatureOnlineCtr。这些基于对特征进行的编码。

\n
    \n
  1. FloatFeature:浮动特征分割采用浮动类型特征并计算分割值(边界)。浮动特征在可视化中表示为特征索引和边界值(检查此):
  2. \n
\n
9, border<257.23    #feature index, border value\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. OneHotFeature:在 one-hot 功能中,每个类都可以由max of n possible values (0 or 1). n由一个名为 的参数决定,one_hot_max_size默认设置为 2。请注意,在泰坦尼克号数据集的情况下,Sex只有 2 个可能的值,MaleFemale。如果您进行了设置one_hot_max_size=4,那么 catboost 将使用一个 hot 来对最多 4 个独特类别的特征进行编码(例如,泰坦尼克号中的 Pclass 有 3 个独特类别)。独热特征由特征名称及其值表示:
  2. \n
\n
Sex, value=Female    #feature name, value\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. OnlineCtr: ctr 是您可以在 catboost 模型中看到的第三种分割类型。对于使用 one-hot 编码(链接)的特征,不会计算 Ctr。如果一个特征中可能的类别数量超过了设置的限制,那么one_hot_max_sizecatboost 会自动使用 ctr 来对特征进行编码,因此 split 的类型是 OnlineCtr。它用特征名称、一些代表唯一类和值的虚拟标记表示:
  2. \n
\n
{five} pr_num1 tb0 type0, value>9  #Label, value\n\n##Inspecting github, the label seems to be from a multihash\n##The multihash seems to be made from (CatFeatureIdx, CtrIdx, TargetBorderIdx, PriorIdx)\n##https://github.com/catboost/catboost/blob/master/catboost/libs/data/ctrs.h\n
Run Code Online (Sandbox Code Playgroud)\n

分析手头的数据集

\n

让我们首先看看每个功能中独特类的数量。

\n
from catboost import CatBoostClassifier, Pool\nimport pandas as pd\n\nX_train.describe().loc[\'unique\']\n
Run Code Online (Sandbox Code Playgroud)\n
one      6\ntwo      5\nthree    8\nfour     8\nfive     4\nsix      6\nseven    5\nName: unique, dtype: object\n
Run Code Online (Sandbox Code Playgroud)\n

正如您所看到的,唯一类的最小数量是 4(在称为“五”的功能中),最大数量是 8。让我们设置我们的one_hot_max_size = 4.

\n
cat_features = list(X_train.columns)\npool = Pool(X_train, y_train, cat_features=list(range(7)), feature_names=cat_features)\nmodel = CatBoostClassifier(verbose=0, one_hot_max_size=4).fit(pool)\n\nmodel.plot_tree(tree_idx=1,pool=pool)\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述

\n

特征“五”现在是OneHotFeature并且导致 的分裂描述five, value=i。然而,功能“One”仍然是一个OnlineCtr.

\n

现在让我们设置one_hot_max_size = 8,这是最大可能的唯一类。这将确保每个功能都是OneHotFeature或不是OnlineCtr

\n
cat_features = list(X_train.columns)\npool = Pool(X_train, y_train, cat_features=list(range(7)), feature_names=cat_features)\nmodel = CatBoostClassifier(verbose=0, one_hot_max_size=8).fit(pool)\n\nmodel.plot_tree(tree_idx=1,pool=pool)\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述

\n
\n

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

\n

有关此内容的更多阅读,请检查这些链接 -

\n
    \n
  1. https://colab.research.google.com/github/catboost/tutorials/blob/master/model_analysis/model_export_as_json_tutorial.ipynb
  2. \n
  3. https://catboost.ai/docs/features/categorical-features.html
  4. \n
  5. https://catboost.ai/docs/concepts/algorithm-main-stages_cat-to-numberic.html#algorithm-main-stages_cat-to-numberic
  6. \n
  7. https://github.com/catboost/tutorials/blob/master/model_analysis/visualize_decision_trees_tutorial.ipynb
  8. \n
\n