{Forecast}開始方法 (AWS CLI)


https://docs.aws.amazon.com/ja_jp/forecast/latest/dg/gs-cli.html

 

★実行すると予測器のトレーニングとして30時間消費し、無料利用枠を超過し、500円程度の課金となるので注意

-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

 

-- 1.2 jqインストール
sudo yum -y install jq

-- 2. S3 バケットを作成しトレーニングデータをアップロードする

wget https://docs.aws.amazon.com/ja_jp/forecast/latest/dg/samples/electricityusagedata.zip

unzip electricityusagedata.zip
ls -ltr
head electricityusagedata.csv

aws s3 mb s3://bucket123

aws s3 ls


aws s3 cp electricityusagedata.csv s3://bucket123

aws s3 ls s3://bucket123 --recursive

 

-- 3. Amazon Forecast用のIAMロールの作成
-- 3.1 IAMポリシー作成(Forecast用)
vim policy01.json

{
    "Version": "2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "s3:Get*",
            "s3:List*",
            "s3:PutObject"
         ],
         "Resource":[
            "arn:aws:s3:::bucket123", 
            "arn:aws:s3:::bucket123/*" 
         ]
      }
   ]
}

aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json

-- 3.2 IAMロール作成(Forecast用)
vim role01.json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "forecast.amazonaws.com"
      },
      "Action": "sts:AssumeRole",
      "Condition": {
        "StringEquals": {
          "aws:SourceAccount": "999999999999"
        },
        "ArnLike": {
          "aws:SourceArn": "arn:aws:forecast:ap-northeast-1:999999999999:*"
        }
      }
    }
  ]
}


aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json

-- 3.3 ポリシーをロールにアタッチ(Forecast用)
aws iam attach-role-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01 \
--role-name role01

 

-- 4. レーニングデータのインポート


-- 4.1 データセットの作成
aws forecast create-dataset \
--dataset-name ds01 \
--domain CUSTOM \
--dataset-type TARGET_TIME_SERIES \
--data-frequency H \
--schema '{
  "Attributes": [
    {
      "AttributeName": "timestamp",
      "AttributeType": "timestamp"
    },
    {
      "AttributeName": "target_value",
      "AttributeType": "float"
    },
    {
      "AttributeName": "item_id",
      "AttributeType": "string"
    }
  ]
}'

aws forecast describe-dataset \
--dataset-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset/ds01

-- 4.2 データセットグループの作成

aws forecast create-dataset-group \
--dataset-group-name dsg01 \
--dataset-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset/ds01 \
--domain CUSTOM


aws forecast describe-dataset-group \
--dataset-group-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset-group/dsg01


-- 4.3 S3 バケットからデータセットにトレーニングデータをインポート

aws forecast create-dataset-import-job \
--dataset-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset/ds01 \
--dataset-import-job-name job01 \
--data-source '{
    "S3Config": {
      "Path": "s3://bucket123/electricityusagedata.csv",
      "RoleArn": "arn:aws:iam::999999999999:role/role01"
    }
  }'

aws forecast describe-dataset-import-job \
--dataset-import-job-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset-import-job/ds01/job01


★10分程度かかる

 


-- 5. 予測子の作成

-- 5.1 予測子の作成

aws forecast create-auto-predictor \
--predictor-name pd01 \
--data-config DatasetGroupArn="arn:aws:forecast:ap-northeast-1:999999999999:dataset-group/dsg01" \
--forecast-horizon 36 \
--forecast-frequency D


aws forecast describe-auto-predictor \
--predictor-arn arn:aws:forecast:ap-northeast-1:999999999999:predictor/pd01_01111111111111111111111111


★70分程度かかる


-- 5.2 予測子の精度メトリクスを取得

aws forecast get-accuracy-metrics \
--predictor-arn arn:aws:forecast:ap-northeast-1:999999999999:predictor/pd01_01111111111111111111111111

 

 


-- 6. Forecast を作成する


-- 6.1 予測の作成

aws forecast create-forecast \
--forecast-name fc01 \
--predictor-arn arn:aws:forecast:ap-northeast-1:999999999999:predictor/pd01_01111111111111111111111111

aws forecast describe-forecast \
--forecast-arn arn:aws:forecast:ap-northeast-1:999999999999:forecast/fc01


★10分程度かかる

 


-- 6.2 予測エクスポートジョブの作成


aws forecast create-forecast-export-job \
--forecast-export-job-name job02 \
--forecast-arn arn:aws:forecast:ap-northeast-1:999999999999:forecast/fc01 \
--destination S3Config="{Path='s3://bucket123',RoleArn='arn:aws:iam::999999999999:role/role01'}"

aws forecast describe-forecast-export-job \
--forecast-export-job-arn arn:aws:forecast:ap-northeast-1:999999999999:forecast-export-job/fc01/job02


aws s3 ls s3://bucket123 --recursive

aws s3 cp s3://bucket123/job02_2022-09-07T14-46-38Z_part0.csv - | head


aws forecastquery query-forecast \
--forecast-arn arn:aws:forecast:ap-northeast-1:999999999999:forecast/fc01 \
--start-date 2015-01-02T00:00:00 \
--end-date   2015-01-02T02:00:00 \
--filters '{"item_id":"client_339"}'


-- 7. クリーンアップ


-- 予測エクスポートジョブの削除

aws forecast describe-forecast-export-job \
--forecast-export-job-arn arn:aws:forecast:ap-northeast-1:999999999999:forecast-export-job/fc01/job02

aws forecast delete-forecast-export-job \
--forecast-export-job-arn arn:aws:forecast:ap-northeast-1:999999999999:forecast-export-job/fc01/job02


-- 予測の削除

aws forecast describe-forecast \
--forecast-arn arn:aws:forecast:ap-northeast-1:999999999999:forecast/fc01

aws forecast delete-forecast \
--forecast-arn arn:aws:forecast:ap-northeast-1:999999999999:forecast/fc01

 


-- 予測子の削除

aws forecast describe-auto-predictor \
--predictor-arn arn:aws:forecast:ap-northeast-1:999999999999:predictor/pd01_01111111111111111111111111

aws forecast delete-predictor \
--predictor-arn arn:aws:forecast:ap-northeast-1:999999999999:predictor/pd01_01111111111111111111111111

 


-- データセットインポートジョブの削除

aws forecast describe-dataset-import-job \
--dataset-import-job-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset-import-job/ds01/job01


aws forecast delete-dataset-import-job \
--dataset-import-job-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset-import-job/ds01/job01


-- データセットの削除

aws forecast describe-dataset \
--dataset-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset/ds01

aws forecast delete-dataset \
--dataset-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset/ds01


-- データセットグループの削除

aws forecast describe-dataset-group \
--dataset-group-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset-group/dsg01

aws forecast delete-dataset-group \
--dataset-group-arn arn:aws:forecast:ap-northeast-1:999999999999:dataset-group/dsg01

 

-- IAMロールの削除(Forecast用)
aws iam list-roles | grep role01

aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::999999999999:policy/policy01

aws iam delete-role --role-name role01


-- IAMポリシーの削除(Forecast用)
aws iam list-policies | grep policy01

aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01

 

-- S3バケットの削除
aws s3 ls
aws s3 rb s3://bucket123 --force