Predicting Hospital Length of Stay

Implemented with Microsoft Machine Learning Services

For the Web Developer

The example site is built with node.js. It uses tedius for communication with SQL Server.

Now that we know how to predict LOS, we might want to use this in real time when a patient is admitted to a hospital. This example webpage shows how you can connect to the SQL Server and perform native scoring to obtain a predicted length of stay for a patient.

Starting the Website

To start the sample webpage, type the following commands into a terminal window or PowerShell window.

    cd C:\Solutions\Hospital\Website
    npm start

You should see the following response:

    The website is running at http://localhost:3000
    Tedious-Connection-Pool: filling pool with 2
    Tedious-Connection-Pool: creating connection: 1

Now leave this window open and open the url http://localhost:3000 in your browser.

Or see below for accessing the website from a different computer

Prediction LOS

A connection to the Hospital_R database is set up in server.js. If you deployed the solution using the Deploy To Azure button, the user name and password you chose has been inserted as well. Otherwise, open the file and supply your SQL username and password.

    var connectionConfig = {
    userName: 'XXYOURSQLUSER',
    password: 'XXYOURSQLPW)rd12',
    server: 'localhost',
    options: { encrypt: true, database: 'Hospital_R' }

The predict function then calls the do_native_predict stored procedure with the patient id and receives back a predicted length of stay for that patient.

    app.get('/predict', function (req, res) {
    pool.acquire(function (err, con) {
    if (err) {

    var request = new Request('do_native_predict', function (err, rowCount) {
      if (err) {

    var eid = req.query.eid;
    console.log('Patient ID: ' + eid)
    request.on('row', function (col) {
      if (col[0].value === null) {
        console.log('NULL result');
      } else {
        // value to return - the predicted LOS
        value = col[0].value;
      res.json({ pred: value });
      console.log("Prediction: " + value)

    // pass the eid to the stored procedure
    request.addParameter('eid', TYPES.VarChar, eid);


Finally, the function in public/js/predLOS.js uses this prediction to display a message to the user based on the value:

function predLOS (id) {
    // call /predict to get res.pred, the predicted LOS
        url: '/predict',
        type: 'GET',
        data: { eid: id },
        contentType: "application/json; charset=utf-8",
        error: function (xhr, error) {
            console.log(xhr); console.log(error);
        success: function (res) {
            console.log("PatientID: " + id)
            console.log("Predicted LOS: " + res.pred)
            // now display the result
            los = Math.round(res.pred);



Admitting a Patient

This site is set up to mimic a hospital dashboard. Click on one of the first two patients to view their details. Select the Admit Patient button to trigger the LOS prediction.

You can view the model values by opening the Console window on your browser.

  • For Edge or Internet Explorer: Press F12 to open Developer Tools, then click on the Console tab.
  • For FireFox or Chome: Press Ctrl-Shift-i to open Developer Tools, then click on the Console tab.

Remote Access to Website

If you wish to access this website from another computer, perform the following steps;
  • Open the firewall for port 3000:
         netsh advfirewall firewall add rule name="website" dir=in action=allow protocol=tcp localport=3000 
  • Then start the web server:
        cd C:\Solutions\Fraud\Website
        npm start
  • On other computers, use the Public IP Address in place of localhost in the address http://localhost:3000. The Public IP Address can be found in the Azure Portal under the "Network interfaces" section.
  • Make sure to leave the terminal window in which you started the server open on your VM.