Guest User

Untitled

a guest
Jan 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION decision_tree_score(MODEL_OBJECT variant,ROW_DATA variant)
  2. RETURNS TABLE (SCORE FLOAT,MODEL variant)
  3. LANGUAGE JAVASCRIPT
  4. AS '{
  5.  
  6. predicateFunction: function (predicate){
  7. if (predicate=="="){
  8. return function(a,b){return a == b};
  9. }
  10. },
  11. predict: function (modelNode,row){
  12. if (JSON.stringify(modelNode)=="{}"){
  13. throw "theres no attributes on the model object!!";
  14. }
  15. if (modelNode.prediction!=null){
  16. return modelNode.prediction;
  17. }
  18. var selectedChild=null;
  19. if (typeof(modelNode.children)==="undefined"){
  20. throw JSON.stringify(modelNode);
  21. throw "No prediction value, but no children either";
  22. }
  23. for (var i=0;i<modelNode.children.length;i++){
  24. var child=modelNode.children[i];
  25. // evaluate this node to see if it matches
  26. if (typeof(row[child.selectionCriteriaAttribute])==="undefined"){
  27. throw "model contains an attribute "+child.selectionCriteriaAttribute+", but the selected table does not contain this column: "+JSON.stringify(Object.getOwnPropertyNames(row));
  28. }
  29. var func = this.predicateFunction(child.selectionCriteriaPredicate);
  30. if (func(row[child.selectionCriteriaAttribute],child.selectionCriteriaValue)){
  31. selectedChild=child;
  32. break;
  33. }
  34. }
  35. if (selectedChild==null){
  36. if (typeof(modelNode.children)==="undefined"){
  37. return null;
  38. }
  39. // none of the nodes matched, arbirarily choose the first one
  40. selectedChild=modelNode.children[0];
  41. }
  42. return this.predict(selectedChild,row);
  43. },
  44. processRow: function (row, rowWriter, context) {
  45.  
  46. this.ccount = this.ccount + 1;
  47. if (typeof(row.ROW_DATA)==="undefined" || typeof(row.MODEL_OBJECT)==="undefined"){
  48. return null;
  49. }
  50. rowWriter.writeRow({SCORE: this.predict(row.MODEL_OBJECT,row.ROW_DATA),MODEL:row.MODEL_OBJECT});
  51. },
  52. finalize: function (rowWriter, context) {
  53. rowWriter.writeRow({NUM: this.csum});
  54. },
  55. initialize: function(argumentInfo, context) {
  56. this.ccount = 0;
  57. this.csum = 0;
  58. }}';
Add Comment
Please, Sign In to add comment