Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: polish talkingdata_demo.md #2994

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 34 additions & 43 deletions docs/zh/use_case/talkingdata_demo.md
Original file line number Diff line number Diff line change
@@ -1,79 +1,65 @@
# TalkingData 广告欺诈检测(OpenMLDB + XGboost)

我们将演示如何使用 [OpenMLDB](https://github.com/4paradigm/OpenMLDB) 与其他开源软件一起开发一个完整的机器学习应用程序,完成 TalkingData 广告欺诈检测挑战(有关此挑战的更多信息请参阅 [Kaggle](https://www.kaggle.com/c/talkingdata-adtracking-fraud-detection/overview)
本文将演示如何使用 OpenMLDB 与开源软件 XGboost 联合开发一个完整的机器学习应用,完成 [TalkingData 广告欺诈检测挑战](https://www.kaggle.com/c/talkingdata-adtracking-fraud-detection/overview)。

## 准备

本文基于 OpenMLDB CLI 进行开发和部署,首先需要下载样例数据并且启动 OpenMLDB CLI。推荐使用 Docker 镜像来快速体验。

- Docker 版本:>= 18.03

## 1 准备工作
### 拉取镜像

### 1.1 下载并安装OpenMLDB
在命令行执行以下命令拉取 OpenMLDB 镜像,并启动 Docker 容器,OpenMLDB 和依赖项都已安装:

#### 1.1.1 在 Docker 中运行

我们建议您使用docker来运行此 Demo。OpenMLDB 和依赖项都已安装完毕。

**启动 Docker**

```
```bash
docker run -it 4pdosc/openmldb:0.7.1 bash
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.7.1 is old, use 0.8.5

```

#### 1.1.2 在本地运行
### 准备数据

下载 OpenMLDB 服务器 pkg,版本>=0.5.0
本例使用 `train.csv` 的前 10000 行作为示例数据,详情请参见代码 [train\_sample.csv](https://github.com/4paradigm/OpenMLDB/tree/main/demo/talkingdata-adtracking-fraud-detection)

安装所有依赖项:

```
pip install pandas xgboost==1.4.2 sklearn tornado "openmldb>=0.5.0" requests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why delete the pip installations?

```

### 1.2 准备数据

我们只使用 `train.csv` 的前10000行作为示例数据,请参见[train\_sample.csv](https://github.com/4paradigm/OpenMLDB/tree/main/demo/talkingdata-adtracking-fraud-detection)。

如果你想要测试完整数据,请通过以下方式下载
如果你想要测试完整数据,请通过以下方式下载:

```
kaggle competitions download -c talkingdata-adtracking-fraud-detection
```

并将数据解压缩到 `demo/talkingdata-adtracking-fraud-detection/data` 。然后调用 [train\_and\_serve.py](https://github.com/4paradigm/OpenMLDB/blob/main/demo/talkingdata-adtracking-fraud-detection/train_and_serve.py)中的 `cut_data()` 方法,制作新的csv样本用于训练
并将数据解压缩到 `demo/talkingdata-adtracking-fraud-detection/data`。然后调用 [train\_and\_serve.py](https://github.com/4paradigm/OpenMLDB/blob/main/demo/talkingdata-adtracking-fraud-detection/train_and_serve.py) 中的 `cut_data()` 方法,制作新的 CSV 数据样本用于训练

### 启动 OpenMLDB 集群

### 1.3 启动 OpenMLDB 集群
镜像内提供了 init.sh 脚本帮助用户快速启动集群。

```
/work/init.sh
```

### 1.4 启动预测服务器
### 启动预测服务器

即使您还没有部署预测服务器,您也可以启动它,使用选项 `--no-init`。
即使还没有部署预测服务器,你也可以使用选项 `--no-init` 启动

```
python3 /work/talkingdata/predict_server.py --no-init > predict.log 2>&1 &
```


```{tip}
- 训练完毕后,您可以发送 post 请求至 `<ip>:<port>/update` 更新预测服务器。
- 您可以运行 `pkill -9 python3` 命令,关闭后台预测服务器。
- 训练完毕后,可以发送 post 请求至 `<ip>:<port>/update` 更新预测服务器。
- 可以运行 `pkill -9 python3` 命令,关闭后台预测服务器。
```


## 2 训练并应用
## 训练并应用

```
cd /work/talkingdata
python3 train_and_serve.py
```

我们使用 OpenMLDB 提取特征,并通过 xgboost 进行训练,请参见[train\_and\_serve.py](https://github.com/4paradigm/OpenMLDB/blob/main/demo/talkingdata-adtracking-fraud-detection/train_and_serve.py)。
使用 OpenMLDB 提取特征,并通过 XGboost 训练机器学习模型,请参见 [train\_and\_serve.py](https://github.com/4paradigm/OpenMLDB/blob/main/demo/talkingdata-adtracking-fraud-detection/train_and_serve.py)。程序具体内容有如下步骤:

1. 将数据加载到离线存储
2. 离线特征提取
2. 离线特征提取
* ip-day-hour 组合的点击次数 -> 窗口期 1h
* ip-app 组合的点击次数 -> 无限窗口期
* ip-app-os 组合的点击次数 -> 无限窗口期
Expand All @@ -82,21 +68,26 @@ python3 train_and_serve.py
5. 加载数据到在线存储
6. 更新预测服务器上的模型

## 3 预测
## 预测

向预测服务器发送post请求 `<ip>:<port>/predict` 即可进行一次预测。或者您也可以运行下面的python脚本
向预测服务器发送 POST 请求 `<ip>:<port>/predict` 即可进行一次预测。或者也可以运行下面的 Python 脚本

```
python3 predict.py
```

## 4 提示
## 提示

预构建的 xgboost python wheel 可能与您计算机中的 openmldb python sdk 不兼容,可能会出现该报错:
`train\_and\_serve.py core dump at SetGPUAttribute...`
预构建的 XGboost python wheel 可能与您计算机中的 openmldb python sdk 不兼容,可能会出现该报错:`train\_and\_serve.py core dump at SetGPUAttribute...`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python wheel => Python Wheel
openmldb python sdk => OpenMLDB Python SDK


通过源代码构建 XGboost 可解决该问题:进入 XGhboost 源代码所在的目录,并执行:

```bash
cd python-package && python setup.py install
```

通过源代码构建xgboost可解决该问题:进入 xgboost 源代码所在的目录,并执行
`cd python-package && python setup.py install`
或者构建 wheel:

或者构建 wheel :
`python setup.py bdist_wheel`
```bash
python setup.py bdist_wheel
```