2020-04-03 13:23:32 +00:00
|
|
|
|
---
|
2022-04-10 23:08:18 +00:00
|
|
|
|
sidebar_position: 41
|
|
|
|
|
sidebar_label: "\u5E94\u7528CatBoost\u6A21\u578B"
|
2020-04-03 13:23:32 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
# 在ClickHouse中应用Catboost模型 {#applying-catboost-model-in-clickhouse}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-12-12 16:38:19 +00:00
|
|
|
|
[CatBoost](https://catboost.ai) 是一个由[Yandex](https://yandex.com/company/)开发的开源免费机器学习库。
|
2020-09-28 08:12:14 +00:00
|
|
|
|
|
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
通过本篇文档,您将学会如何用SQL语句调用已经存放在Clickhouse中的预训练模型来预测数据。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
为了在ClickHouse中应用CatBoost模型,需要进行如下步骤:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-12-12 16:38:19 +00:00
|
|
|
|
1. [创建数据表](#create-table).
|
2020-04-08 14:22:25 +00:00
|
|
|
|
2. [将数据插入到表中](#insert-data-to-table).
|
2020-12-12 16:38:19 +00:00
|
|
|
|
3. [将CatBoost集成到ClickHouse中](#integrate-catboost-into-clickhouse) (可跳过)。
|
|
|
|
|
4. [从SQL运行模型推断](#run-model-inference).
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-12-12 16:38:19 +00:00
|
|
|
|
有关训练CatBoost模型的详细信息,请参阅 [训练和模型应用](https://catboost.ai/docs/features/training.html#training).
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
您可以通过[RELOAD MODEL](https://clickhouse.com/docs/en/sql-reference/statements/system/#query_language-system-reload-model)与[RELOAD MODELS](https://clickhouse.com/docs/en/sql-reference/statements/system/#query_language-system-reload-models)语句来重载CatBoost模型。
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
## 先决条件 {#prerequisites}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-12-12 16:38:19 +00:00
|
|
|
|
请先安装 [Docker](https://docs.docker.com/install/)。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
!!! note "注"
|
2022-01-15 16:30:37 +00:00
|
|
|
|
[Docker](https://www.docker.com) 是一个软件平台,用户可以用Docker来创建独立于已有系统并集成了CatBoost和ClickHouse的容器。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
在应用CatBoost模型之前:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
**1.** 从容器仓库拉取示例docker镜像 (https://hub.docker.com/r/yandex/tutorial-catboost-clickhouse) :
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ docker pull yandex/tutorial-catboost-clickhouse
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
此示例Docker镜像包含运行CatBoost和ClickHouse所需的所有内容:代码、运行时、库、环境变量和配置文件。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
**2.** 确保已成功拉取Docker镜像:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ docker image ls
|
|
|
|
|
REPOSITORY TAG IMAGE ID CREATED SIZE
|
|
|
|
|
yandex/tutorial-catboost-clickhouse latest 622e4d17945b 22 hours ago 1.37GB
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
**3.** 基于此镜像启动一个Docker容器:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ docker run -it -p 8888:8888 yandex/tutorial-catboost-clickhouse
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-12 16:38:19 +00:00
|
|
|
|
## 1. 创建数据表 {#create-table}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
为训练样本创建ClickHouse表:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**1.** 在交互模式下启动ClickHouse控制台客户端:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ clickhouse client
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
!!! note "注"
|
|
|
|
|
ClickHouse服务器已经在Docker容器内运行。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**2.** 使用以下命令创建表:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
:) 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
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**3.** 从ClickHouse控制台客户端退出:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
:) exit
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
## 2. 将数据插入到表中 {#insert-data-to-table}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
插入数据:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**1.** 运行以下命令:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ clickhouse client --host 127.0.0.1 --query 'INSERT INTO amazon_train FORMAT CSVWithNames' < ~/amazon/train.csv
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**2.** 在交互模式下启动ClickHouse控制台客户端:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ clickhouse client
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**3.** 确保数据已上传:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
:) SELECT count() FROM amazon_train
|
|
|
|
|
|
|
|
|
|
SELECT count()
|
|
|
|
|
FROM amazon_train
|
|
|
|
|
|
|
|
|
|
+-count()-+
|
|
|
|
|
| 65538 |
|
2020-04-08 14:22:25 +00:00
|
|
|
|
+-------+
|
2020-04-03 13:23:32 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-09-07 07:00:47 +00:00
|
|
|
|
## 3. 将CatBoost集成到ClickHouse中 {#integrate-catboost-into-clickhouse}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
!!! note "注"
|
2022-01-15 16:30:37 +00:00
|
|
|
|
**可跳过。** 示例Docker映像已经包含了运行CatBoost和ClickHouse所需的所有内容。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
为了将CatBoost集成进ClickHouse,需要进行如下步骤:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-12-12 16:38:19 +00:00
|
|
|
|
**1.** 构建评估库。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-29 15:20:55 +00:00
|
|
|
|
评估CatBoost模型的最快方法是编译 `libcatboostmodel.<so|dll|dylib>` 库文件.
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-12-12 16:38:19 +00:00
|
|
|
|
有关如何构建库文件的详细信息,请参阅 [CatBoost文件](https://catboost.ai/docs/concepts/c-plus-plus-api_dynamic-c-pluplus-wrapper.html).
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
**2.** 创建一个新目录(位置与名称可随意指定), 如 `data` 并将创建的库文件放入其中。 示例Docker镜像已经包含了库 `data/libcatboostmodel.so`.
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-12-12 16:38:19 +00:00
|
|
|
|
**3.** 创建一个新目录来放配置模型, 如 `models`.
|
|
|
|
|
|
|
|
|
|
**4.** 创建一个模型配置文件,如 `models/amazon_model.xml`.
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
**5.** 修改模型配置:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` xml
|
|
|
|
|
<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>
|
|
|
|
|
```
|
|
|
|
|
|
2020-09-28 08:12:14 +00:00
|
|
|
|
**6.** 将CatBoost库文件的路径和模型配置添加到ClickHouse配置:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` xml
|
|
|
|
|
<!-- 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>
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
## 4. 使用SQL调用预测模型 {#run-model-inference}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2022-01-15 16:30:37 +00:00
|
|
|
|
为了测试模型是否正常,可以使用ClickHouse客户端 `$ clickhouse client`.
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-09-28 08:12:14 +00:00
|
|
|
|
让我们确保模型能正常工作:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
:) 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
|
2021-07-29 15:20:55 +00:00
|
|
|
|
```
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
!!! note "注"
|
2022-01-15 16:30:37 +00:00
|
|
|
|
函数 [modelEvaluate](../sql-reference/functions/other-functions.md#function-modelevaluate) 会对多类别模型返回一个元组,其中包含每一类别的原始预测值。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-12-12 16:38:19 +00:00
|
|
|
|
执行预测:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
:) 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
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
!!! note "注"
|
2020-09-28 08:12:14 +00:00
|
|
|
|
查看函数说明 [exp()](../sql-reference/functions/math-functions.md) 。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
让我们计算样本的LogLoss:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
:) 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
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
!!! note "注"
|
2021-03-19 15:31:56 +00:00
|
|
|
|
查看函数说明 [avg()](../sql-reference/aggregate-functions/reference/avg.md#agg_function-avg) 和 [log()](../sql-reference/functions/math-functions.md) 。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-09-19 20:05:54 +00:00
|
|
|
|
[原始文章](https://clickhouse.com/docs/en/guides/apply_catboost_model/) <!--hide-->
|