如何获取CatBoost可视化显示类别

考虑以下数据:

import pandas as pdy_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 CatBoostClassifierfrom catboost import Poolcat_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)

我得到以下结果:

enter image description here

但是我不明白{five} pr_num0 tb0 type0, value>8 是什么意思。我希望它能像手册中的泰坦尼克号示例那样显示:

这会显示如下:

enter image description here

如何在我自己的示例中获得类似Sex, value = Female的效果?例如,One, value = b


回答:

TLDR; 这不完全是一个可视化问题,而是更多关于Catboost如何进行特征分割的问题。

Catboost根据一个名为one_hot_max_size的参数决定哪些特征进行one-hot编码,哪些进行ctr编码。如果一个特征的类别数<= one_hot_max_size,那么它将被视为one-hot编码。默认值设置为2。因此,只有二元特征(如0,1或male,female)被视为one-hot编码,其他特征(如PClass -> 1,2,3)则被处理为ctr。将这个值设置得足够高,可以强制Catboost将你的列编码为one-hot。

{five} pr_num0 tb0 type0, value>8基本上是一个ctr分割的标签和值。虽然没有相关文档,但通过检查GitHub仓库,发现这个标签似乎是通过多重哈希生成的。

更多详情如下。


特征分割是如何选择的?

一个特征-分割对通过以下3个步骤被选择为叶节点的分割:

  1. 形成一个可能候选列表(“特征-分割对”),这些候选将被分配给叶节点作为分割。
  2. 为每个对象计算若干惩罚函数(假设从步骤1获得的所有候选都已分配给叶节点)。
  3. 选择惩罚最小的分割。

特征分割的类型

有三种类型的分割:FloatFeatureOneHotFeatureOnlineCtr。这些是基于特征编码方式的。

  1. FloatFeature: 浮点特征分割使用浮点类型特征,并计算分割值(边界)。在可视化中,浮点特征以特征索引和边界值的形式表示(查看此处):
9, border<257.23    #特征索引,边界值
  1. OneHotFeature: 在one-hot特征中,每个类别可以表示为最多n个可能的值(0或1)n由一个名为one_hot_max_size的参数决定,默认值为2。注意在泰坦尼克数据集的情况下,Sex只有两种可能的值,MaleFemale。如果你设置one_hot_max_size=4,那么Catboost将使用one-hot编码具有最多4个唯一类别的特征(例如泰坦尼克号中的Pclass有3个唯一类别)。one-hot特征以特征名称和其值的形式表示:
Sex, value=Female    #特征名称,值
  1. OnlineCtr: ctr是Catboost模型中可以看到的第三种分割类型。ctr不用于one-hot编码的特征(链接)。如果一个特征的可能类别数超过one_hot_max_size设置的限制,那么Catboost会自动使用ctr来编码特征,因此这种类型的分割是OnlineCtr。它以特征名称、一些表示唯一类别的虚拟标记和一个值的形式表示:
{five} pr_num1 tb0 type0, value>9  #标签,值##检查GitHub,发现标签似乎来自多重哈希##多重哈希似乎是由(CatFeatureIdx, CtrIdx, TargetBorderIdx, PriorIdx)生成的##https://github.com/catboost/catboost/blob/master/catboost/libs/data/ctrs.h

分析手头的数据集

我们首先来看一下每个特征中唯一类别的数量。

from catboost import CatBoostClassifier, Poolimport pandas as pdX_train.describe().loc['unique']
one      6two      5three    8four     8five     4six      6seven    5Name: unique, dtype: object

如你所见,最少的唯一类别数是4(在名为“five”的特征中),最多是8。我们将one_hot_max_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)

enter image description here

特征“five”现在是OneHotFeature,结果显示为five, value=i。然而,特征“One”仍然是OnlineCtr

现在我们将one_hot_max_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)

enter image description here


希望这能澄清你关于为什么泰坦尼克号中的Sex显示方式与你正在处理的特征不同的问题。

关于此问题更多阅读,请查看这些链接 –

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注