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