Last active
August 29, 2015 14:25
-
-
Save horike37/f9971b320f79a80e7bbf to your computer and use it in GitHub Desktop.
Amazon API Gateway + Lambda + Amazon Machine Learning で会員の購買予測が出来るWordPressプラグインを作る ref: http://qiita.com/horike37/items/cd9995e986249d011a7b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var aws = require('aws-sdk'); | |
var machinelearning = new aws.MachineLearning(); | |
var params = { | |
MLModelId: '<Machine LearningのモデルID>' | |
}; | |
exports.handler = function(event, context) { | |
machinelearning.getMLModel(params, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
} else { | |
var params = { | |
MLModelId: '<Machine LearningのモデルID>', | |
PredictEndpoint: data.EndpointInfo.EndpointUrl, | |
Record: { | |
age: event.age, | |
job: event.job, | |
marital: event.marital, | |
education: event.education, | |
default: event.default, | |
housing: event.housing, | |
loan: event.loan, | |
contact: event.contact, | |
month: event.month, | |
day_of_week: event.day_of_week, | |
duration: event.duration, | |
campaign: event.campaign, | |
pdays: event.pdays, | |
previous: event.previous, | |
poutcome: event.poutcome, | |
emp_var_rate: event.emp_var_rate, | |
cons_price_idx: event.cons_price_idx, | |
cons_conf_idx: event.cons_conf_idx, | |
euribor3m: event.euribor3m, | |
nr_employed: event.nr_employed, | |
} | |
}; | |
machinelearning.predict(params, function(err, data) { | |
if (err) console.log(err, err.stack); | |
else context.succeed(data); | |
}); | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Prediction": { | |
"details": { | |
"Algorithm": "SGD", | |
"PredictiveModelType": "BINARY" | |
}, | |
"predictedLabel": "1", | |
"predictedScores": { | |
"1": 0.3982754647731781 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$response = wp_remote_get('<API Gatewayへのエンドポイント>?age='.<AGE>.'&job='.<job>.・・・・<other parameter>); | |
if( !is_wp_error( $response ) && $response["response"]["code"] === 200 ) { | |
$response_body = json_decode($response["body"]); | |
update_user_meta( $user_id, 'Predict', $response_body ); | |
} else { | |
// Handle error here. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WP_Machine_Learning_Sample { | |
private static $instance; | |
private function __construct() {} | |
public static function get_instance() { | |
if( !isset( self::$instance ) ) { | |
$c = __CLASS__; | |
self::$instance = new $c(); | |
} | |
return self::$instance; | |
} | |
public function init() { | |
add_filter( 'manage_users_columns', array( $this, 'manage_users_columns' ) ); | |
add_filter( 'manage_users_custom_column', array( $this, 'manage_users_custom_column' ), 10, 3 ); | |
} | |
public function manage_users_custom_column( $val, $column_name, $user_id ) { | |
$predict = get_user_meta( $user_id, 'predict', true ); | |
if ( empty($predict) ) { | |
return; | |
} | |
$predict = json_decode($predict); | |
switch ($column_name) { | |
case 'kobaiyosoku' : | |
if ( $predict->Prediction->predictedLabel === '1') { | |
return '契約してくれそう'; | |
} else { | |
return '契約してくれなさそう'; | |
} | |
break; | |
case 'kobaiscore' : | |
$label = $predict->Prediction->predictedLabel; | |
return $predict->Prediction->predictedScores->{$label}; | |
break; | |
default: | |
} | |
return $val; | |
} | |
public function manage_users_columns( $column ) { | |
$column['kobaiyosoku'] = '購買予測'; | |
$column['kobaiscore'] = 'スコア'; | |
return $column; | |
} | |
} | |
$wpmls_instance = WP_Machine_Learning_Sample::get_instance(); | |
$wpmls_instance->init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment