Foro de elhacker.net

Programación => Desarrollo Web => Mensaje iniciado por: bengy en 15 Septiembre 2016, 05:04 am



Título: enviar variable de un modulo al servidor appjs en node
Publicado por: bengy en 15 Septiembre 2016, 05:04 am
hola buenas, quiero enviar una variable de un modulo muy sencillo de svm(machine learning) al servidor app.js, estoy trabajando con nodejs

este es mi codigo

Código
  1. 'use strict';
  2. var so = require('stringify-object');
  3. var svm = require('../lib');
  4.  
  5. var xor = [
  6.    [[0, 0], 0],
  7.    [[0, 1], 1],
  8.    [[1, 0], 1],
  9.    [[1, 1], 0]
  10. ];
  11.  
  12. // initialize predictor
  13. var clf = new svm.CSVC({
  14.    kFold: 1
  15. });
  16.  
  17. clf.train(xor)
  18.    .progress(function(progress){
  19.        console.log('training progress: %d%', Math.round(progress*100));
  20.    })
  21.    .spread(function (model, report) {
  22.        console.log('training report: %s\nPredictions:', so(report));
  23.        xor.forEach(function(ex){
  24.            var prediction = clf.predictSync(ex[0]);
  25.            console.log('   %d XOR %d => %d', ex[0][0], ex[0][1], prediction);
  26.        });
  27.    });


como enviar la variable prediccion?

lo modifique asi
Código
  1. 'use strict';
  2. var so = require('stringify-object');
  3. var svm = require('../lib');
  4.  
  5. var xor = [
  6.    [[0, 0], 0],
  7.    [[0, 1], 1],
  8.    [[1, 0], 1],
  9.    [[1, 1], 0]
  10. ];
  11. var prediction;
  12. // initialize predictor
  13. var clf = new svm.CSVC({
  14.    kFold: 1
  15. });
  16. clf.train(xor)
  17.    .progress(function(progress){
  18.        console.log('training progress: %d%', Math.round(progress*100));
  19.    })
  20.    .spread(function (model, report) {
  21.        console.log('training report: %s\nPredictions:', so(report));
  22.        xor.forEach(function(ex){
  23.            prediction = clf.predictSync(ex[0]);
  24.            console.log('   %d XOR %d => %d', ex[0][0], ex[0][1], prediction);
  25.            prediction="prediciendo desde cbba";
  26.            return {
  27.            prediction:1
  28.            };
  29.        });
  30.    });
  31. exports.prediction =prediction;

pero no logro mostrar en el app.js, la variable llega undefined

Código:
var ror = require('./node_modules/node-svm/examples/evaluation-example');console.log(ror.prediction);

ayuda porfavor





Título: Re: enviar variable de un modulo al servidor appjs en node
Publicado por: Jeferi en 17 Septiembre 2016, 01:04 am
Prueba ésto:

Código
  1. 'use strict';
  2. var so = require('stringify-object');
  3. var svm = require('../lib');
  4.  
  5. var exports = module.exports = {};
  6.  
  7. var xor = [
  8.    [[0, 0], 0],
  9.    [[0, 1], 1],
  10.    [[1, 0], 1],
  11.    [[1, 1], 0]
  12. ];
  13.  
  14. // inicializas prediction a -1
  15. exports.prediction = -1;
  16. // initialize predictor
  17. var clf = new svm.CSVC({
  18.    kFold: 1
  19. });
  20. clf.train(xor)
  21.    .progress(function(progress){
  22.        console.log('training progress: %d%', Math.round(progress*100));
  23.    })
  24.    .spread(function (model, report) {
  25.        console.log('training report: %s\nPredictions:', so(report));
  26.        xor.forEach(function(ex){
  27.            prediction = clf.predictSync(ex[0]);
  28.            console.log('   %d XOR %d => %d', ex[0][0], ex[0][1], prediction);
  29.            prediction="prediciendo desde cbba";
  30.            // exportas prediction calculada
  31.            exports.prediction = prediction;
  32.        });
  33.    });