I can not get the variable of a module in the app.js file (Node.js)

1

I want to send a very simple module variable from svm (machine learning) to server app.js , I'm working with NodeJS .

This is my code:

'use strict'; 
var so = require('stringify-object'); 
var svm = require('../lib');   
var xor = [
    [[0, 0], 0],
    [[0, 1], 1],
    [[1, 0], 1],
    [[1, 1], 0] ];   
// initialize predictor 
var clf = new svm.CSVC({ kFold: 1 });   
clf.train(xor)
    .progress(function(progress){
        console.log('training progress: %d%', Math.round(progress*100));
    })
    .spread(function (model, report) {
        console.log('training report: %s\nPredictions:', so(report));
        xor.forEach(function(ex){
            var prediction = clf.predictSync(ex[0]);
            console.log('   %d XOR %d => %d', ex[0][0], ex[0][1], prediction);
        });
    });

How to send variable prediction ?

I modified it like this:

'use strict';
var so = require('stringify-object');
var svm = require('../lib');

var xor = [
    [[0, 0], 0],
    [[0, 1], 1],
    [[1, 0], 1],
    [[1, 1], 0]
];
var prediction;
// initialize predictor
var clf = new svm.CSVC({
    kFold: 1
});
clf.train(xor)
    .progress(function(progress){
        console.log('training progress: %d%', Math.round(progress*100));
    })
    .spread(function (model, report) {
        console.log('training report: %s\nPredictions:', so(report));
        xor.forEach(function(ex){
            prediction = clf.predictSync(ex[0]);
            console.log('   %d XOR %d => %d', ex[0][0], ex[0][1], prediction);
            prediction="prediciendo desde cbba";
            return {
            prediction:1    
            };
        });
    });
exports.prediction =prediction;

But I can not show in app.js . The variable reaches undefined .

Code:

var ror = require('./node_modules/node-svm/examples/evaluation-example');
console.log(ror.prediction);
    
asked by hubman 15.09.2016 в 06:45
source

1 answer

1

The value that you export is obtained asynchronously and you are exporting it when the value has not yet been calculated, which is why it reaches you as undefined .

The solution is to export in your module a function that accepts callback and send in it the value when it is calculated.

'use strict';
var so = require('stringify-object');
var svm = require('../lib');

var xor = [
    [[0, 0], 0],
    [[0, 1], 1],
    [[1, 0], 1],
    [[1, 1], 0]
];

function prediction(callback) {
  // initialize predictor
  var clf = new svm.CSVC({
    kFold: 1
  });
  clf.train(xor)
    .progress(function(progress) {
      console.log('training progress: %d%', Math.round(progress * 100));
    })
    .spread(function(model, report) {
      console.log('training report: %s\nPredictions:', so(report));
      xor.forEach(function(ex) {
        // No puedes usar la variable prediction 
        // porque ocultarías el nombre de la función
        var pred = clf.predictSync(ex[0]);
        console.log('   %d XOR %d => %d', ex[0][0], ex[0][1], pred);
        // Esto eliminaría el valor por eso lo comento
        //prediction = "prediciendo desde cbba";

        // Invocas el callback con los datos
        callback(null, pred);
        return {
          prediction: 1
        };
      });
    });
}

// Exportas la función
exports.prediction = prediction;

To use it you should call it this way

var ror = require('./node_modules/node-svm/examples/evaluation-example').prediction;

ror(function(error, prediction) {
    console.log(prediction);
});

This function will be called several times since your prediction value is within a cycle forEach .

If you wonder why I use (error, valor) reads

link

If you do not handle the error, simply delete the parameter.

    
answered by 15.09.2016 / 14:44
source