Skip to content

Instantly share code, notes, and snippets.

@horike37
Last active September 30, 2015 04:13
Show Gist options
  • Save horike37/e3d9bff86a1293931827 to your computer and use it in GitHub Desktop.
Save horike37/e3d9bff86a1293931827 to your computer and use it in GitHub Desktop.
Amazon Machine Learningのモデル作成から評価、データ予測を行うまでのPHPスクリプト
<?php
require_once("vendor/autoload.php");
Use Aws\MachineLearning\MachineLearningClient;
$hash_value = mt_rand();
$client = MachineLearningClient::factory(array('version'=>'2014-12-12','region'=>'us-east-1'));
//訓練データ定義
$ds_train = array(
'DataSourceId' => 'ds-train-' . $hash_value,
'DataSourceName' => 'ds-train',
'DataSpec' => array(
'DataLocationS3' => 's3://<データソースのロケーション>',
'DataSchemaLocationS3' => 's3://<スキーマ定義データのロケーション>',
'DataRearrangement' => '{"randomSeed":"'.$hash_value.'", "splitting":{"percentBegin":0,"percentEnd":70}}',
),
'ComputeStatistics' => true
);
//評価データ定義
$ds_eval = array(
'DataSourceId' => 'ds-eval-' . $hash_value,
'DataSourceName' => 'ds-eval',
'DataSpec' => array(
'DataLocationS3' => 's3://<データソースのロケーション>',
'DataSchemaLocationS3' => 's3://<スキーマ定義データのロケーション>',
'DataRearrangement' => '{"randomSeed":"'.$hash_value.'", "splitting":{"percentBegin":70,"percentEnd":100}}',
),
);
//モデル定義
$ml = array(
'MLModelId' => 'model-'. $hash_value,
'MLModelName' => 'model',
'MLModelType' => 'REGRESSION',
'TrainingDataSourceId' => 'ds-train-' . $hash_value,
);
//評価定義
$eval = array(
'EvaluationId' => 'eval-' . $hash_value,
'EvaluationName' => 'eval',
'MLModelId' => 'model-001-' . $hash_value,
'EvaluationDataSourceId' => 'ds-eval-' . $hash_value,
);
//バッチ入力データ定義
$batch = array(
'DataSourceId' => 'batch-' . $hash_value,
'DataSourceName' => 'batch',
'DataSpec' => array(
'DataLocationS3' => 's3://<データソースのロケーション>',
'DataSchemaLocationS3' => 's3://<スキーマ定義データのロケーション>',
),
'ComputeStatistics' => true
);
//バッチ出力データ定義
$batchproduction = array(
'BatchPredictionId' => 'batch-pr-' . $hash_value,
'BatchPredictionName' => 'batch-pr',
'MLModelId' => 'model-' . $hash_value,
'BatchPredictionDataSourceId' => 'batch-' . $hash_value,
'OutputUri' => 's3://<データ出力先のロケーション>',
);
//モデル作成と評価
$client->createDataSourceFromS3($ds_train);
$client->createDataSourceFromS3($ds_eval);
$client->createMLModel($ml);
$client->createEvaluation($eval);
//データ予測
$client->createDataSourceFromS3($batch);
$client->createBatchPrediction($batchproduction);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment