ml.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Simple Javascript eXtension - 1.0, Machine Leanring Module
  3. * (c) 2011 - 2015 Steve L. Nyemba, steve@the-phi.com
  4. * License GPL version 3.0
  5. *
  6. * dependencies:
  7. * jx.utils collection of utilities and design patterns used
  8. * jx.math various math & statistical functions
  9. * This file implements a few reusable machine learning models/techniques
  10. *
  11. * jx.ml.mapreduce Performs a standard/basic mapreduce (single thread for now)
  12. * jx.ml.regression Will perform linear & logistic regressions
  13. */
  14. if(!jx){
  15. var jx = {} ;
  16. }
  17. jx.ml = {}
  18. /**
  19. * The function performs map/reduce and at the very least map i.e the reduce function is optional
  20. */
  21. jx.ml.mapreduce = function(data,fn_map,fn_reduce){
  22. //
  23. // insure that the mapping function has been provided
  24. //
  25. var __map = {}
  26. var emit = function(id,mvalue){
  27. if(__map[id] == null){
  28. __map[id] = []
  29. }
  30. __map[id].push(mvalue) ;
  31. }//-- end of the emitter
  32. if(data.constructor != Array){
  33. jx.utils.patterns.visitor(jx.utils.keys(data),function(id){
  34. fn_map(data[id],emit) ;
  35. });
  36. }else{
  37. jx.utils.patterns.visitor(data,function(row){
  38. fn_map(row,emit) ;
  39. });
  40. }
  41. if(fn_reduce != null){
  42. //
  43. // We will be performing a reduce operation at this point
  44. var ids = jx.utils.keys(__map) ;
  45. jx.utils.patterns.visitor(ids,function(id){
  46. return __map[id] = fn_reduce(id,__map[id]) ;
  47. });
  48. }
  49. return __map ;
  50. }//--
  51. /**
  52. * The modules developed below will perform linear regression and logistic regression
  53. */
  54. jx.ml.regression = {}