ClickHouse/docs/zh/guides/apply-catboost-model.md
Ivan Blinkov cd14f9ebcb
SQL reference refactoring (#10857)
* split up select.md

* array-join.md basic refactoring

* distinct.md basic refactoring

* format.md basic refactoring

* from.md basic refactoring

* group-by.md basic refactoring

* having.md basic refactoring

* additional index.md refactoring

* into-outfile.md basic refactoring

* join.md basic refactoring

* limit.md basic refactoring

* limit-by.md basic refactoring

* order-by.md basic refactoring

* prewhere.md basic refactoring

* adjust operators/index.md links

* adjust sample.md links

* adjust more links

* adjust operatots links

* fix some links

* adjust aggregate function article titles

* basic refactor of remaining select clauses

* absolute paths in make_links.sh

* run make_links.sh

* remove old select.md locations

* translate docs/es

* translate docs/fr

* translate docs/fa

* remove old operators.md location

* change operators.md links

* adjust links in docs/es

* adjust links in docs/es

* minor texts adjustments

* wip

* update machine translations to use new links

* fix changelog

* es build fixes

* get rid of some select.md links

* temporary adjust ru links

* temporary adjust more ru links

* improve curly brace handling

* adjust ru as well

* fa build fix

* ru link fixes

* zh link fixes

* temporary disable part of anchor checks
2020-05-15 07:34:54 +03:00

6.6 KiB
Raw Blame History

machine_translated machine_translated_rev toc_priority toc_title
true 72537a2d52 41 应用CatBoost模型

在ClickHouse中应用Catboost模型

CatBoost 是一个自由和开源的梯度提升库开发 Yandex 用于机器学习。

通过此指令您将学习如何通过从SQL运行模型推理在ClickHouse中应用预先训练好的模型。

在ClickHouse中应用CatBoost模型:

  1. 创建表.
  2. 将数据插入到表中.
  3. 碌莽禄into拢Integrate010-68520682<url> (可选步骤)。
  4. 从SQL运行模型推理.

有关训练CatBoost模型的详细信息请参阅 培训和应用模型.

先决条件

如果你没有 Docker 然而,安装它。

!!! note "注" Docker 是一个软件平台允许您创建容器将CatBoost和ClickHouse安装与系统的其余部分隔离。

在应用CatBoost模型之前:

1.码头窗口映像 从注册表:

$ docker pull yandex/tutorial-catboost-clickhouse

此Docker映像包含运行CatBoost和ClickHouse所需的所有内容代码、运行时、库、环境变量和配置文件。

2. 确保已成功拉取Docker映像:

$ docker image ls
REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
yandex/tutorial-catboost-clickhouse   latest              622e4d17945b        22 hours ago        1.37GB

3. 基于此映像启动一个Docker容器:

$ docker run -it -p 8888:8888 yandex/tutorial-catboost-clickhouse

1. 创建表

为训练样本创建ClickHouse表:

1. 在交互模式下启动ClickHouse控制台客户端:

$ clickhouse client

!!! note "注" ClickHouse服务器已经在Docker容器内运行。

2. 使用以下命令创建表:

:) CREATE TABLE amazon_train
(
    date Date MATERIALIZED today(),
    ACTION UInt8,
    RESOURCE UInt32,
    MGR_ID UInt32,
    ROLE_ROLLUP_1 UInt32,
    ROLE_ROLLUP_2 UInt32,
    ROLE_DEPTNAME UInt32,
    ROLE_TITLE UInt32,
    ROLE_FAMILY_DESC UInt32,
    ROLE_FAMILY UInt32,
    ROLE_CODE UInt32
)
ENGINE = MergeTree ORDER BY date

3. 从ClickHouse控制台客户端退出:

:) exit

2. 将数据插入到表中

插入数据:

1. 运行以下命令:

$ clickhouse client --host 127.0.0.1 --query 'INSERT INTO amazon_train FORMAT CSVWithNames' < ~/amazon/train.csv

2. 在交互模式下启动ClickHouse控制台客户端:

$ clickhouse client

3. 确保数据已上传:

:) SELECT count() FROM amazon_train

SELECT count()
FROM amazon_train

+-count()-+
|   65538 |
+-------+

3. 碌莽禄into拢Integrate010-68520682<url>

!!! note "注" 可选步骤。 Docker映像包含运行CatBoost和ClickHouse所需的所有内容。

碌莽禄to拢integrate010-68520682<url>:

1. 构建评估库。

评估CatBoost模型的最快方法是编译 libcatboostmodel.<so|dll|dylib> 图书馆. 有关如何构建库的详细信息,请参阅 CatBoost文件.

2. 例如,在任何地方和任何名称创建一个新目录, data 并将创建的库放入其中。 Docker映像已经包含了库 data/libcatboostmodel.so.

3. 例如在任何地方和任何名称为config model创建一个新目录, models.

4. 创建具有任意名称的模型配置文件,例如, models/amazon_model.xml.

5. 描述模型配置:

<models>
    <model>
        <!-- Model type. Now catboost only. -->
        <type>catboost</type>
        <!-- Model name. -->
        <name>amazon</name>
        <!-- Path to trained model. -->
        <path>/home/catboost/tutorial/catboost_model.bin</path>
        <!-- Update interval. -->
        <lifetime>0</lifetime>
    </model>
</models>

6. 将CatBoost的路径和模型配置添加到ClickHouse配置:

<!-- File etc/clickhouse-server/config.d/models_config.xml. -->
<catboost_dynamic_library_path>/home/catboost/data/libcatboostmodel.so</catboost_dynamic_library_path>
<models_config>/home/catboost/models/*_model.xml</models_config>

4. 从SQL运行模型推理

对于测试模型运行ClickHouse客户端 $ clickhouse client.

让我们确保模型正常工作:

:) SELECT
    modelEvaluate('amazon',
                RESOURCE,
                MGR_ID,
                ROLE_ROLLUP_1,
                ROLE_ROLLUP_2,
                ROLE_DEPTNAME,
                ROLE_TITLE,
                ROLE_FAMILY_DESC,
                ROLE_FAMILY,
                ROLE_CODE) > 0 AS prediction,
    ACTION AS target
FROM amazon_train
LIMIT 10

!!! note "注" 功能 模型值 返回带有多类模型的每类原始预测的元组。

让我们预测一下:

:) SELECT
    modelEvaluate('amazon',
                RESOURCE,
                MGR_ID,
                ROLE_ROLLUP_1,
                ROLE_ROLLUP_2,
                ROLE_DEPTNAME,
                ROLE_TITLE,
                ROLE_FAMILY_DESC,
                ROLE_FAMILY,
                ROLE_CODE) AS prediction,
    1. / (1 + exp(-prediction)) AS probability,
    ACTION AS target
FROM amazon_train
LIMIT 10

!!! note "注" 更多信息 exp() 功能。

让我们计算样本的LogLoss:

:) SELECT -avg(tg * log(prob) + (1 - tg) * log(1 - prob)) AS logloss
FROM
(
    SELECT
        modelEvaluate('amazon',
                    RESOURCE,
                    MGR_ID,
                    ROLE_ROLLUP_1,
                    ROLE_ROLLUP_2,
                    ROLE_DEPTNAME,
                    ROLE_TITLE,
                    ROLE_FAMILY_DESC,
                    ROLE_FAMILY,
                    ROLE_CODE) AS prediction,
        1. / (1. + exp(-prediction)) AS prob,
        ACTION AS tg
    FROM amazon_train
)

!!! note "注" 更多信息 avg()日志() 功能。

原始文章