{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# [모듈 00] 데이터 변환 및 수집 워크플로를 위한 설정 환경\n",
"- 이 노트북은 데이터 변환 및 수집 워크플로를 프로비저닝하는 사용자 지정 SageMaker 프로젝트에 필요한 리소스와 매개변수를 설정합니다.\n",
"- 특정 사용 사례 및 요구 사항에 따라 고유한 사용자 지정 프로젝트의 경우 프로젝트 프로비저닝의 일부로 이러한 모든 리소스를 생성하는 것을 고려할 수 있습니다.\n",
"\n",
"노트북은 필수 리소스를 생성하기 위한 다음 활동을 안내합니다.\n",
"\n",
"1. 아키텍쳐 개요\n",
"2. 기본 변수 설정\n",
" - 데이터 업로드를 위한 Amazon S3 버킷 가져오기\n",
" - 데이타 랭글러 흐름 이름 설정 \n",
"3. 데이터 세트 준비\n",
" - 데이터 세트 다운로드 및 데이터 탐색\n",
"4. 데이터 랭글러 흐름 (Flow) 생성\n",
" - 데이터 변환 및 피쳐 수집을 위한 Amazon Data Wrangler 흐름 생성\n",
"5. 피쳐 스토어\n",
" - 피쳐가 저장되는 피쳐 저장소에 새 피쳐 그룹 생성\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. 아키텍쳐 개요\n",
"\n",
"\n",
"\n",
"1. Amazon S3 버킷에 업로드된 데이터 파일\n",
"2. 데이터 처리 및 변환 프로세스 시작\n",
"3. 추출, 처리 및 변환된 기능은 Feature Store에서 지정된 기능 그룹으로 수집됩니다.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. 기본 변수 설정 "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.70.0\n"
]
}
],
"source": [
"import os\n",
"import json\n",
"import boto3\n",
"import pandas as pd\n",
"import sagemaker\n",
"from sagemaker.session import Session\n",
"from sagemaker.feature_store.feature_definition import FeatureDefinition\n",
"from sagemaker.feature_store.feature_definition import FeatureTypeEnum\n",
"from sagemaker.feature_store.feature_group import FeatureGroup\n",
"\n",
"import time\n",
"from time import gmtime, strftime\n",
"import uuid\n",
"\n",
"\n",
"print(sagemaker.__version__)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## 2.1. `domain_id` and `execution_role` 얻기\n",
"- 세이지 메이커 스튜디오의 domain_id 및 스튜디오 실행을 하는 execution_role 가져옵니다."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SageMaker domain id: d-uf1m5smfybav\n",
"Stored 'domain_id' (str)\n"
]
}
],
"source": [
"NOTEBOOK_METADATA_FILE = \"/opt/ml/metadata/resource-metadata.json\"\n",
"domain_id = None\n",
"\n",
"if os.path.exists(NOTEBOOK_METADATA_FILE):\n",
" with open(NOTEBOOK_METADATA_FILE, \"rb\") as f:\n",
" domain_id = json.loads(f.read()).get('DomainId')\n",
" print(f\"SageMaker domain id: {domain_id}\")\n",
"\n",
"%store domain_id"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stored 'execution_role' (str)\n"
]
}
],
"source": [
"r = boto3.client(\"sagemaker\").describe_domain(DomainId=domain_id)\n",
"execution_role = r[\"DefaultUserSettings\"][\"ExecutionRole\"]\n",
"\n",
"%store execution_role"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.2. 데이터용 S3 버킷 가져오고 변수 설정\n",
"- 모든 솔루션 아티팩트 및 데이터를 저장하기 위해 SageMaker 기본 버킷을 사용합니다. 고유한 버킷을 생성하거나 사용하도록 선택할 수 있습니다. \n",
"- SageMaker 실행 역할 및 `AmazonSageMakerServiceCatalogProductsUseRole` 역할에 연결된 해당 권한이 있는지 확인하여 객체를 나열하고 읽고 버킷에 넣을 수 있습니다."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"data_bucket = None # you can use your own S3 bucket name\n",
"sagemaker_session = Session()\n",
"\n",
"if data_bucket is None:\n",
" data_bucket = sagemaker_session.default_bucket()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sagemaker-us-east-1-569441333767\n"
]
}
],
"source": [
"print(data_bucket)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"⭐ 다음 변수의 기본값을 유지하거나 원하는 경우 변경할 수 있습니다."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# set some literals\n",
"s3_data_prefix = f\"{data_bucket}/feature-store-ingestion-pipeline/dataset/\"\n",
"s3_flow_prefix = f\"{data_bucket}/feature-store-ingestion-pipeline/dw-flow/\"\n",
"s3_fs_query_output_prefix = f\"{data_bucket}/feature-store-ingestion-pipeline/fs_query_results/\"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data Wrangler flow upload and a feature group will have this unique suffix: 07-04-20-04-a58253ce\n"
]
}
],
"source": [
"unique_suffix = f\"{strftime('%d-%H-%M-%S', gmtime())}-{str(uuid.uuid4())[:8]}\"\n",
"abalone_dataset_file_name = \"abalone.csv\"\n",
"abalone_dataset_local_path = \"../dataset/\"\n",
"abalone_dataset_local_url = f\"{abalone_dataset_local_path}{abalone_dataset_file_name}\"\n",
"\n",
"print(f\"Data Wrangler flow upload and a feature group will have this unique suffix: {unique_suffix}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.3 데이타 랭글러 흐름 이름 설정\n",
"
\n", " | sex | \n", "length | \n", "diameter | \n", "height | \n", "whole_weight | \n", "shucked_weight | \n", "viscera_weight | \n", "shell_weight | \n", "rings | \n", "
---|---|---|---|---|---|---|---|---|---|
0 | \n", "M | \n", "0.455 | \n", "0.365 | \n", "0.095 | \n", "0.5140 | \n", "0.2245 | \n", "0.1010 | \n", "0.150 | \n", "15 | \n", "
1 | \n", "M | \n", "0.350 | \n", "0.265 | \n", "0.090 | \n", "0.2255 | \n", "0.0995 | \n", "0.0485 | \n", "0.070 | \n", "7 | \n", "
2 | \n", "F | \n", "0.530 | \n", "0.420 | \n", "0.135 | \n", "0.6770 | \n", "0.2565 | \n", "0.1415 | \n", "0.210 | \n", "9 | \n", "
3 | \n", "M | \n", "0.440 | \n", "0.365 | \n", "0.125 | \n", "0.5160 | \n", "0.2155 | \n", "0.1140 | \n", "0.155 | \n", "10 | \n", "
4 | \n", "I | \n", "0.330 | \n", "0.255 | \n", "0.080 | \n", "0.2050 | \n", "0.0895 | \n", "0.0395 | \n", "0.055 | \n", "7 | \n", "