Use Machine Learning to predict customers you might lose – Part 3

Cruising through our machine learning journey and starting from where we left in the previous instalment, the next step is to expose our machine learning model as a Web service so that it can be invoked from within Dynamics CRM.

Azure Machine Learning has this fantastic concept of converting an experiment into a trained model. Trained model is like a compiled version of your experiment that can be exposed via a web service, all from the click of just one button i.e. Setup Web Service

clip_image001

Azure ML takes care of the rest by deploying the model. Once deployed you can inspect its configuration by going to the Web Services section as shown here

image

In order to connect to this web service from within Dynamics CRM, we can use the below JavaScript. We can pass CRM objects to this service in JSON format and get prediction results back

sendRequest: function (avgIncome,overAge,leftOver,houseVal,handsetVal,longCalls) {

var service = AzureScript.getRequestObject();

var url = "https://xxxxxxxxx.services.azureml.net/workspaces/xxxxxxxxxxxxxxx/services/xxxxxx/execute?api-version=2.0&details=true";

var jsonObject =

{

"Inputs": {

"input1": {

"ColumnNames": [

"College",

"Avg_Income",

"Overage",

"Leftover",

"House_Value",

"Handset_Value",

"Over_15min_Calls_Per_Month",

"Average_Call_Duration",

"Reported_Satisfaction",

"Usage",

"Considering_Change_Of_Plan",

"Outcome"

],

"Values": [

[

null,

avgIncome,

overAge,

leftOver,

houseVal,

handsetVal,

longCalls,

null,

null,

null,

null,

null

]

]

}

},

"GlobalParameters": {}

};

var dataString = JSON.stringify(jsonObject);

if (service != null) {

service.open("POST", url, false);

service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");

service.setRequestHeader("Authorization", "Bearer xxxxxxxxx==");

service.setRequestHeader("Accept", "application/json");

service.setRequestHeader("Content-Type", "application/json; charset=utf-8");

service.setRequestHeader("Content-Length", dataString.length);

service.send(dataString);

//Recieve result

var requestResults = eval('(' + service.responseText + ')');

try {

resultOutput = requestResults.Results.output1.value.Values[0]

return resultOutput;

}

catch (err) {

console.log('Unable to interpret result');

}

}

}

Let us prepare Dynamics CRM to start consuming this web service. I have created an event onSave() of the Telecom Customer form which passes the relevant data to the Azure Service and gets the score. The Javascript for that is as below

function onFormSave() {

//Prepare data - only fields with high Information gain

var houseValue = Xrm.Page.getAttribute('manny_housevalue').getValue();

var income = Xrm.Page.getAttribute('manny_income').getValue();

var longcalls = Xrm.Page.getAttribute('manny_longcalls').getValue();

var overage = Xrm.Page.getAttribute('manny_overage').getValue();

var phonecost = Xrm.Page.getAttribute('manny_phonecost').getValue();

var leftOver = Xrm.Page.getAttribute('manny_leftover').getValue();

var valOutput = AzureScript.sendRequest(income, overage, leftOver, houseValue, phonecost, longcalls);

if (valOutput != null && valOutput[0]!=null && valOutput[1]!=null) {

if(valOutput[0]=="STAY")

Xrm.Page.getAttribute('manny_predictedchurnstatus').setValue(true)

else

Xrm.Page.getAttribute('manny_predictedchurnstatus').setValue(false)

var prob = parseFloat(valOutput[1]);

if(prob>=0 && prob<=1.0)

Xrm.Page.getAttribute('manny_predictionconfidencepercentage').setValue(prob*100)

}

}

getRequestObject: function () {
 ///<summary>
 /// Get an instance of XMLHttpRequest for all browsers
 ///</summary>
 if (XMLHttpRequest) {
 // Chrome, Firefox, IE7+, Opera, Safari
 // ReSharper disable InconsistentNaming
 return new XMLHttpRequest();
 // ReSharper restore InconsistentNaming
 }
 // IE6
 try {
 // The latest stable version. It has the best security, performance,
 // reliability, and W3C conformance. Ships with Vista, and available
 // with other OS's via downloads and updates.
 return new ActiveXObject('MSXML2.XMLHTTP.6.0');
 } catch (e) {
 try {
 // The fallback.
 return new ActiveXObject('MSXML2.XMLHTTP.3.0');
 } catch (e) {
 alertMessage('This browser is not AJAX enabled.');
 return null;
 }
 }
 },

 

These scripts are trivial and should be self-explanatory. Basically we are passing the highly correlated features to the prediction service and getting two outputs

Prediction score -> assigned to-> manny_predictedchurnstatus

Prediction confidence -> assigned to -> manny_predictionconfidencepercentage

And they are displayed on the form like this, it’s integrated i.e. the moment you change the data the score is updated.

clip_image001[5]

In the next blog post, we will touch upon the Insights that can be gained from a machine learning integration

Advertisements

5 thoughts on “Use Machine Learning to predict customers you might lose – Part 3

  1. Hi Manny,

    Can you please tell which reference file needs to be used to execute below code?
    var service = AzureScript.getRequestObject();

    Like

  2. Hi Ravi,
    Sorry for late reply but getRequestObject is just a helper method and AzureScript is the namespace for both methods. I have updated the code to getRequestObject now in this blog. Sorry for confusion.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s