Amazon Machine Learningで自動生成されたData Schema JSONを確認する

2015.07.31

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

最近、Amazon Machine Learning(AML)を使い始めています。その過程で疑問に思ったことを少しづつブログに残して行こうと思います。

というわけで、非常に簡単なエントリから。

Data Schemaを確認する

Amazon Machine Learningでどういったことができるのか、という確認をするためにAmazon Machine Learning Developer Guideに記載されたTutorialを実施していました。

そしてドキュメントを読み進めていくと、Data Schemaという概念の説明がありました。

Data Schemaとは、DataSource(Amazon Machine Learningに投入するデータに関するメタデータ)に関する項目の一つで、以下の内容を定義するJSON形式のテキストです。

  • DataSourceのファイル形式(csv)
  • データに対するヘッダ行の有無
  • 各項目の型(Numeric, Categorical, Binary, Text)

Documentに記載されたTutorialでは、ウィザードに沿って進めていくことになるため、このJSONの存在を意識することはありません。AMLが自動的に判別し、適切な設定を自動生成してくれるからです。

では、具体的にどのようなJSONが生成されたか確認することは可能なのでしょうか。現状、Management Consoleからは確認することができないようですが、API GetDataSourceを利用すれば確認することが可能です。ただし、Verboseオプションを付けないとData Schemeは確認できないようです。

AWS CLIを利用して確認してみました。

~$ aws machinelearning get-data-source --region us-east-1 --data-source-id <DataSourceId> --verbose
{
    "Status": "COMPLETED",
    "NumberOfFiles": 1,
    "Name": "Banking.csv",
    "DataLocationS3": "s3://example-files/banking.csv",
    "CreatedByIamUser": "arn:aws:iam::xxxxxxxxxx:user/mochizuki",
    "DataSizeInBytes": 4882918,
    "ComputeStatistics": true,
    "LastUpdatedAt": 1438116539.798,
    "DataSourceId": "ds-xxxxxxxxx",
    "LogUri": "https://eml-prod-emr.s3.amazonaws.com/xxxxxxxxx-ds-ds-xxxxxxxxxxx/userlog/xxxxxxxxxx-ds-ds-xxxxxxxx
    "DataSourceSchema": "{\"version\":\"1.0\",\"rowId\":null,\"rowWeight\":null,\"targetAttributeName\":\"y\",\"dataFormat\":\"CSV\",\"dataFileContainsHeader\":true,\"attributes\":[{\"attributeName\":\"age\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"job\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"marital\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"education\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"default\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"housing\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"loan\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"contact\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"month\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"day_of_week\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"duration\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"campaign\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"pdays\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"previous\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"poutcome\",\"attributeType\":\"CATEGORICAL\"},{\"attributeName\":\"emp_var_rate\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"cons_price_idx\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"cons_conf_idx\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"euribor3m\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"nr_employed\",\"attributeType\":\"NUMERIC\"},{\"attributeName\":\"y\",\"attributeType\":\"BINARY\"}],\"excludedAttributeNames\":[]}",
    "CreatedAt": 1438116220.046
}

DataSourceSchemaにJSONがあるのが見えますね。jqでもう少し読みやすく整形しましょう。JSONがエスケープされたStringとして格納されているので、jqのfromjson関数を利用します。

~$ aws machinelearning get-data-source --region us-east-1 --data-source-id <DataSourceId> --verbose \
    | jq '.DataSourceSchema | fromjson'
{
  "version": "1.0",
  "rowId": null,
  "rowWeight": null,
  "targetAttributeName": "y",
  "dataFormat": "CSV",
  "dataFileContainsHeader": true,
  "attributes": [
    {
      "attributeName": "age",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "job",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "marital",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "education",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "default",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "housing",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "loan",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "contact",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "month",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "day_of_week",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "duration",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "campaign",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "pdays",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "previous",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "poutcome",
      "attributeType": "CATEGORICAL"
    },
    {
      "attributeName": "emp_var_rate",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "cons_price_idx",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "cons_conf_idx",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "euribor3m",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "nr_employed",
      "attributeType": "NUMERIC"
    },
    {
      "attributeName": "y",
      "attributeType": "BINARY"
    }
  ],
  "excludedAttributeNames": []
}

ドキュメントに記載のあるJSONを取り出すことができました。

こういう細かな定義を気にせずに機械学習を試すことができるのが、AMLの最も大きなポイントだと思います。ですが、細かい設定を気にする必要がある時などにはこういった形で定義を確認できることを覚えておくと良いと思います。

参考