How to call a Azure Machine Learning Web Service from NodeJS

Azure machine learning allows data scientists and developers to embed predictive analytics into applications. To learn more about Azure machine learning visit Azure machine learning documentation . A simplified process flow for Azure machine learning is:

  • Create an Azure machine learning workspace that has an associated Azure storage account.

  • Login to your Azure machine learning workspace and then login into ML Studio. ML Studio is where you can create your machine learning experiments and publish them as a web services. You need to create an experiment which produces the machine learning model, and then publish it as a web service.

  • Once you publish your web service, the portal has a page with information on how to call the web service and example code in C#, Python, and R.

  • Calling the web service allows you to embed your predictive analytics into applications. You will pass new input feature data to the web service. The new feature data will be run through the machine learning model and the web service will send back the prediction.

     

Recently I had a request on how to call an Azure machine learning web service from NodeJS. The portal did not have an example. In this post I will show the basic code in NodeJS to call an Azure machine learning web service.

Below is a simple experiment I created that tries to predict the percent gain ten days from now for the SP 500 Index. At the bottom is a button to set up your web service once you are comfortable with the model.

 

Below is the web service predictive experiment page after you have published it.

 

Below is the information for the published web service. You will need information from this page to call the web service from NodeJS. One piece of information that you will need is the API key. You can also test out your web service here. Under the REQUEST/RESPONSE link is more information that you will need like the POST Request URI.

 

Below you will see the post request URI. You will need this. Also on this page are code examples in C#, Python and R. Now that we have created an experiment published it as a web service and have the Post Request URI and API Key we can start to write the code in NodeJS to call the web service and get a prediction.

 

I am assuming you are somewhat familiar with NodeJS and have downloaded it and installed it. Here is the download link Download NodeJS and web site. You can create the maml-server.js file in notepad or your favorite IDE.

From the POST Request URI you can get the information for the host and path variables. The headers will need the API key. The options variable has this information and is created in the getPred function. In the buildFeatureInput function, I just hard coded the new feature data, but you could easily get this information from a web form or a RDMS call. I wanted to keep the example simple.

 

//maml-server.js

var http = require("http");

var https = require("https");

var querystring = require("querystring");

var fs = require('fs');

 

function getPred(data) {

console.log('===getPred()===');

var dataString = JSON.stringify(data)

var host = 'ussouthcentral.services.azureml.net'

var path = '/workspaces/fda91d2e52b74ee2ae68b1aac4dba8b9/services/1b2f5e6f99574756a8fde751def19a0a/execute?api-version=2.0&details=true'

var method = 'POST'

var api_key = 'vKKR78dSdQeSc9qdMaDmu2Z5bcFqb4TfkZdNgSxzcIjGV9p5OP2uy4k1HfJes1T4Ws3St+EBgQTX/N8vqCs4zg=='

var headers = {'Content-Type':'application/json', 'Authorization':'Bearer ' + api_key};

 

var options = {

host: host,

port: 443,

path: path,

method: 'POST',

headers: headers

};

 

console.log('data: ' + data);

console.log('method: ' + method);

console.log('api_key: ' + api_key);

console.log('headers: ' + headers);

console.log('options: ' + options);

 

var reqPost = https.request(options, function (res) {

console.log('===reqPost()===');

console.log('StatusCode: ', res.statusCode);

console.log('headers: ', res.headers);

 

res.on('data', function(d) {

process.stdout.write(d);

});

});

 

// Would need more parsing out of prediction from the result

reqPost.write(dataString);

reqPost.end();

reqPost.on('error', function(e){

console.error(e);

});

 

}

 

//Could build feature inputs from web form or RDMS. This is the new data that needs to be passed to the web service.

function buildFeatureInput(){

console.log('===performRequest()===');

var data = {

"Inputs": {

"input1": {

"ColumnNames": ["gl10", "roc20", "uo", "ppo", "ppos", "macd", "macds", "sstok", "sstod", "pmo", "pmos", "wmpr"],

"Values": [ [ "0", "-1.3351", "50.2268", "-0.2693", "-0.2831", "-5.5310", "-5.8120", "61.9220", "45.3998", "-0.0653", "-0.0659", "-30.3005" ], ]

},

},

"GlobalParameters": {}

}

getPred(data);

}

 

 

function send404Reponse(response) {

response.writeHead(404, {"Context-Type": "text/plain"});

response.write("Error 404: Page not Found!");

response.end();

}

 

function onRequest(request, response) {

if(request.method == 'GET' && request.url == '/' ){

response.writeHead(200, {"Context-Type": "text/plain"});

fs.createReadStream("./index.html").pipe(response);

}else {

send404Reponse(response);

}

}

 

http.createServer(onRequest).listen(8050);

console.log("Server is now running on port 8050");

buildFeatureInput();

 

 

Once you have saved the code to disk you can run it in NodeJS by issuing, "Node <path to maml-server.js>". I am also displaying the status code from the POST and the results from calling the web service. You would need to parse out the JSON to get the "Scored Labels" column that has the prediction. The model is predicting that the SP 500 will be down 4.71% ten days from now. The SP 500 closed yesterday (2016-02-17) at 1926.82. We can check back on 2016-03-04 and see how well the model did! Machine learning is a big topic but Azure machine learning makes it easy to embed analytics in your applications.

 

 

I hope this helps some one. I will have changed my API key, so if you try to run the example you might get an authorization error.

Bill