

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Amazon Redshift ML 执行高级分析
<a name="perform-advanced-analytics-using-amazon-redshift-ml"></a>

*Po Hong 和 Chyanna Antonio，Amazon Web Services*

## Summary
<a name="perform-advanced-analytics-using-amazon-redshift-ml-summary"></a>

在 Amazon Web Services (AWS) Cloud，您可使用 Amazon Redshift machine learning (Amazon Redshift ML) 对存储在 Amazon Redshift cluster 或 Amazon Simple Storage Service (Amazon S3) 的数据执行机器学习分析。Amazon Redshift ML 支持有监督学习，这种学习常用于高级分析。Amazon Redshift ML 用例包括收入预测、信用卡欺诈检测以及客户生命周期价值 (CLV) 或客户流失预测。

Amazon Redshift ML 使数据库用户可以轻松地使用标准的 SQL 命令创建、训练和部署 ML 模型。Amazon Redshift ML 使用 Amazon A SageMaker utopilot 自动训练和调整最佳机器学习模型，以便根据您的数据进行分类或回归，同时保持控制和可见性。

亚马逊 Redshift、Amazon S3 和亚马逊之间的所有交互 SageMaker 都被抽象出来并实现了自动化。ML 模型经过训练和部署后，它将作为[用户定义函数](https://docs.aws.amazon.com/redshift/latest/dg/user-defined-functions.html) (UDF) 在 Amazon Redshift 中使用，并可用于 SQL 查询。 

此模式补充了 AWS 博客中的 “[使用 SQL 在 Amazon Redshift 中创建、训练和部署机器学习模型](https://aws.amazon.com/blogs/big-data/create-train-and-deploy-machine-learning-models-in-amazon-redshift-using-sql-with-amazon-redshift-ml/)” 和 [“入门资源中心” 中的 “使用 SageMaker亚马逊构建、训练和部署机器学习模型](https://aws.amazon.com/getting-started/hands-on/build-train-deploy-machine-learning-model-sagemaker/)[”](https://aws.amazon.com/getting-started/) 教程。

## 先决条件和限制
<a name="perform-advanced-analytics-using-amazon-redshift-ml-prereqs"></a>

**先决条件**
+ 一个有效的 Amazon Web Services account
+ Amazon Redshift 表内现有数据

**技能**
+ 熟悉 Amazon Redshift ML 使用的术语和概念，包括*机器学习*、*培训*和*预测*。有关这方面的更多信息，请参阅 Amazon Machine Learning (Amazon ML) 文档中的[ML 模型培训](https://docs.aws.amazon.com/machine-learning/latest/dg/training-ml-models.html)。
+ 体验 Amazon Redshift 用户设置、访问管理以及标准 SQL 语法。有关的更多信息，请参阅 Amazon Redshift 文档中的 [Amazon Redshift 入门](https://docs.aws.amazon.com/redshift/latest/gsg/getting-started.html)。
+ Amazon S3 和 AWS Identity and Acess Management (IAM) 方面的专长和经验。 
+ 在 AWS 命令行界面（AWS CLI）中运行命令也是有益的，但不是必需的。

**限制**
+ Amazon Redshift 集群与 S3 存储桶必须位于同一 AWS 区域。
+ 此模式方法仅支持有监督学习模型，例如回归、二进制分类以及多类分类。 

## 架构
<a name="perform-advanced-analytics-using-amazon-redshift-ml-architecture"></a>

![工作流程展示了 Amazon Redshift 机器学习 SageMaker 如何使用来构建、训练和部署机器学习模型。](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/3b6a5f49-c647-45f4-940e-b623f989326a/images/36cc39fe-c1f3-4d68-b89e-e636156001a6.png)


以下步骤说明了 Amazon Redshift 机器学习如何使用 SageMaker 来构建、训练和部署机器学习模型： 

1. Amazon Redshift 将训练数据导出到 S3 存储桶中。

1. SageMaker Autopilot 会自动预处理训练数据。

1. 调用该`CREATE MODEL`语句后，Amazon Redshift ML 将 SageMaker 用于训练。

1. SageMaker Autopilot 搜索并推荐用于优化评估指标的机器学习算法和最佳超参数。

1. Amazon Redshift ML 会在 Amazon Redshift 集群中将输出 ML 模型注册为 SQL 函数。

1. ML 模型的函数可用于 SQL 语句。 

**技术堆栈**
+ Amazon Redshift
+ SageMaker
+ Amazon S3

## 工具
<a name="perform-advanced-analytics-using-amazon-redshift-ml-tools"></a>
+ [Amazon Redshift](https://docs.aws.amazon.com/redshift/latest/dg/welcome.html) – Amazon Redshift 是一种完全托管的企业 PB 级数据仓库服务。
+ [Amazon Redshift ML](https://docs.aws.amazon.com/redshift/latest/dg/machine_learning.html) – Amazon Redshift 机器学习 (Amazon Redshift ML) 是一种基于云的稳健服务，能够让所有技能水平的分析人员和数据科学家都能轻松使用 ML 技术。
+ [Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) – Amazon Simple Storage Service (Amazon S3) 是一项面向互联网的存储服务。 
+ [Amazon SageMaker](https://docs.aws.amazon.com/sagemaker/latest/dg/whatis.html) — SageMaker 是一项完全托管的机器学习服务。 
+ [Amazon A SageMaker uto SageMaker pil](https://docs.aws.amazon.com/sagemaker/latest/dg/autopilot-automate-model-development.html) ot — Autopilot 是一款功能集，可自动执行自动机器学习 (AutoML) 过程中的关键任务。

 

**代码**

您可使用以下代码，在 Amazon Redshift 中创建受监管的 ML 模型：

```
"CREATE MODEL customer_churn_auto_model
FROM (SELECT state,
             account_length,
             area_code,
             total_charge/account_length AS average_daily_spend, 
             cust_serv_calls/account_length AS average_daily_cases,
             churn 
      FROM customer_activity
      WHERE  record_date < '2020-01-01' 
     )
TARGET churn
FUNCTION ml_fn_customer_churn_auto
IAM_ROLE 'arn:aws:iam::XXXXXXXXXXXX:role/Redshift-ML'
SETTINGS (
  S3_BUCKET 'your-bucket'
);")
```

**注意**  
`SELECT` 状态可以指 Amazon Redshift 常规表或 Amazon Redshift Spectrum 外部表，或者同时参考两者。

## 操作说明
<a name="perform-advanced-analytics-using-amazon-redshift-ml-epics"></a>

### 准备训练与测试数据集
<a name="prepare-a-training-and-test-dataset"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 准备训练与测试数据集。 | 登录 AWS 管理控制台并打开亚马逊 SageMaker 控制台。按照[构建、训练和部署机器学习模型](https://aws.amazon.com/getting-started/hands-on/build-train-deploy-machine-learning-model-sagemaker/)教程创建包含标签列（*监督训练*）且没有标题的 .csv 或 Apache Parquet 文件。 我们建议您将原始数据集进行打乱，并拆分为用于模型训练的训练集（70%）和用于模型性能评估的测试集（30%）。 | 数据科学家 | 

### 准备与配置技术堆栈
<a name="prepare-and-configure-the-technology-stack"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建和配置 Amazon Redshift 集群。 | 在 Amazon Redshift 控制台，根据您的要求创建集群。有关更多信息，请参阅 Amazon Redshift 文档中的[创建集群](https://docs.aws.amazon.com/redshift/latest/dg/tutorial-loading-data-launch-cluster.html)。 Amazon Redshift 集群必须使用 `SQL_PREVIEW` 维护轨道创建。有关预览轨道的更多信息，请参阅 Amazon Redshift 文档中的[选择集群维护轨道](https://docs.aws.amazon.com/redshift/latest/mgmt/working-with-clusters.html#rs-mgmt-maintenance-tracks)。 | 数据库管理员、云架构师 | 
| 创建 S3 存储桶以存储训练数据和模型构件。 | 在 Amazon S3 控制台，创建 S3 存储桶以训练和测试数据。有关创建 S3 存储桶的更多信息，请参阅 AWS 快速入门中的[创建 S3 存储桶](https://docs.aws.amazon.com/quickstarts/latest/s3backup/step-1-create-bucket.html)。 请确保您的 Amazon Redshift 集群与 S3 存储桶位于同一区域。  | 数据库管理员、云架构师 | 
| 创建 IAM policy，并将其附加至 Amazon Redshift 集群。 | 创建 IAM 策略以允许 Amazon Redshift 集群访问 SageMaker 和亚马逊 S3。有关说明和步骤，请参阅 Amazon Redshift 文档中的[使用 Amazon Redshift ML 的集群设置](https://docs.aws.amazon.com/redshift/latest/dg/cluster-setup.html)。 | 数据库管理员、云架构师 | 
| 允许 Amazon Redshift 用户和群组访问架构和表格。 | 授予权限，允许 Amazon Redshift 中的用户和群组访问内部和外部架构和表格。有关步骤和说明，请参阅 Amazon Redshift 文档的[管理权限和所有权](https://docs.aws.amazon.com/redshift/latest/dg/permissions-ownership.html)。 | 数据库管理员 | 

### 在 Amazon Redshift 中创建和训练 ML 模型
<a name="create-and-train-the-ml-model-in-amazon-redshift"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在 Amazon Redshift 中创建和训练 ML 模型。 | 在 Amazon Redshift ML 中创建和训练您的 ML 模型。有关更多信息，请参阅 Amazon Redshift 文档中的 `CREATE MODEL` 语句。 | 开发人员、数据科学家 | 

### 在 Amazon Redshift 中执行批量推理与预测
<a name="perform-batch-inference-and-prediction-in-amazon-redshift"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 使用生成的 ML 模型函数执行推理。 | 有关使用生成 ML 模型函数执行推理的更多信息，请参阅 Amazon Redshift 文档中的[预测](https://docs.aws.amazon.com/redshift/latest/dg/prediction.html)。 | 数据科学家、商业智能用户 | 

## 相关资源
<a name="perform-advanced-analytics-using-amazon-redshift-ml-resources"></a>

**准备训练与测试数据集**
+ [使用 Amazon 构建、训练和部署机器学习模型 SageMaker](https://aws.amazon.com/getting-started/hands-on/build-train-deploy-machine-learning-model-sagemaker/)

 

**准备与配置技术堆栈**
+ [创建 Amazon Redshift 集群。](https://docs.aws.amazon.com/redshift/latest/dg/tutorial-loading-data-launch-cluster.html)
+ [选择 Amazon Redshift 集群维护轨道](https://docs.aws.amazon.com/redshift/latest/mgmt/working-with-clusters.html#rs-mgmt-maintenance-tracks)
+ [创建 S3 存储桶](https://docs.aws.amazon.com/quickstarts/latest/s3backup/step-1-create-bucket.html)
+ [为使用 Amazon Redshift ML 设置 Amazon Redshift 集群](https://docs.aws.amazon.com/redshift/latest/dg/cluster-setup.html)
+ [在 Amazon Redshift 中管理权限与所有权](https://docs.aws.amazon.com/redshift/latest/dg/permissions-ownership.html)

 

**在 Amazon Redshift 中创建和训练 ML 模型**
+ [在 Amazon Redshift 中创建模型语句](https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_MODEL.html#r_simple_create_model) 

 

**在 Amazon Redshift 中执行批量推理与预测**
+ [Amazon Redshift 中的预测](https://docs.aws.amazon.com/redshift/latest/dg/prediction.html) 

 

**其他资源**
+ [Amazon Redshift ML 入门](https://docs.aws.amazon.com/redshift/latest/dg/geting-started-machine-learning.html)
+ [使用 SQL 和 Amazon Redshift ML 在 Amazon Redshift 中构建、训练和部署 ML 模型](https://aws.amazon.com/blogs/big-data/create-train-and-deploy-machine-learning-models-in-amazon-redshift-using-sql-with-amazon-redshift-ml/)
+ [Amazon Redshift 合作伙伴](https://aws.amazon.com/redshift/partners/?partner-solutions-cards.sort-by=item.additionalFields.partnerNameLower&partner-solutions-cards.sort-order=aschttps://aws.amazon.com/redshift/partners/?partner-solutions-cards.sort-by=item.additionalFields.partnerNameLower&partner-solutions-cards.sort-order=asc)
+ [AWS 机器学习能力合作伙伴](https://aws.amazon.com/machine-learning/partner-solutions/?partner-solutions-cards.sort-by=item.additionalFields.partnerNameLower&partner-solutions-cards.sort-order=asc)