-
Notifications
You must be signed in to change notification settings - Fork 0
/
criteria.lua
86 lines (71 loc) · 2.03 KB
/
criteria.lua
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
local math = require("math")
local ffi = require("ffi")
local bitop = require("bit")
require("luarocks.loader")
local array = require("ljarray.array")
local helpers = require("ljarray.helpers")
local operator = helpers.operator
module(..., package.seeall) -- export all local functions
Gini = {}
Gini.__index = Gini
Gini.create = function(n_classes)
local gini = {}
setmetatable(gini, Gini)
gini.max_n_classes = n_classes
gini.n_samples = 0
gini.count_left = helpers.zeros(n_classes)
gini.count_right = helpers.zeros(n_classes)
gini.n_left = 0
gini.n_right = 0
gini.H_left = 0
gini.H_right = 0
gini.position = 0
return gini
end
Gini.init = function(self, y, start, stop)
self.y = y
self.start = start or 0
self.stop = stop or y.shape[0]
self.n_samples = stop - start
self.count_left = helpers.zeros(self.max_n_classes+1)
self.count_right = helpers.zeros(self.max_n_classes+1)
self.n_left = 0
self.n_right = self.n_samples
-- count classes, initially all samples are to the right
for i = self.start, self.stop-1 do
local c = self.y.data[i]
self.count_right[c] = self.count_right[c] + 1
end
self.n_classes = 0
for c = 0, self.max_n_classes do
if self.count_right[c] > 0 then
self.n_classes = self.n_classes + 1
end
end
end
Gini.move = function(self, delta)
local c = self.y.data[self.start + self.n_left]
self.n_left = self.n_left + 1
self.n_right = self.n_right - 1
self.count_left[c] = self.count_left[c] +1
self.count_right[c] = self.count_right[c] - 1
end
Gini.eval = function(self)
local H_left = self.n_left * self.n_left
local H_right = self.n_right * self.n_right
for c = 0,self.max_n_classes do
H_left = H_left - (self.count_left[c] * self.count_left[c])
H_right = H_right - (self.count_right[c] * self.count_right[c])
end
if self.n_left == 0 then
H_left = 0
else
H_left = H_left / self.n_left
end
if self.n_right == 0 then
H_right = 0
else
H_right = H_right / self.n_right
end
return (H_left + H_right) / self.n_samples
end