forked from JuliaAI/DecisionTree.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.jl
368 lines (321 loc) · 12.5 KB
/
main.jl
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# Utilities
include("tree.jl")
# Returns a dict ("Label1" => 1, "Label2" => 2, "Label3" => 3, ...)
label_index(labels) = Dict(v => k for (k, v) in enumerate(labels))
## Helper function. Counts the votes.
## Returns a vector of probabilities (eg. [0.2, 0.6, 0.2]) which is in the same
## order as get_labels(classifier) (eg. ["versicolor", "setosa", "virginica"])
function compute_probabilities(labels::AbstractVector, votes::AbstractVector, weights=1.0)
label2ind = label_index(labels)
counts = zeros(Float64, length(label2ind))
for (i, label) in enumerate(votes)
if isa(weights, Number)
counts[label2ind[label]] += weights
else
counts[label2ind[label]] += weights[i]
end
end
return counts / sum(counts) # normalize to get probabilities
end
# Applies `row_fun(X_row)::AbstractVector` to each row in X
# and returns a matrix containing the resulting vectors, stacked vertically
function stack_function_results(row_fun::Function, X::AbstractMatrix)
N = size(X, 1)
N_cols = length(row_fun(X[1, :])) # gets the number of columns
out = Array{Float64}(undef, N, N_cols)
for i in 1:N
out[i, :] = row_fun(X[i, :])
end
return out
end
function _convert(
node :: treeclassifier.NodeMeta{S},
list :: AbstractVector{T},
labels :: AbstractVector{T}) where {S, T}
if node.is_leaf
return Leaf{T}(list[node.label], labels[node.region])
else
left = _convert(node.l, list, labels)
right = _convert(node.r, list, labels)
return Node{S, T}(node.feature, node.threshold, left, right)
end
end
################################################################################
function build_stump(
labels :: AbstractVector{T},
features :: AbstractMatrix{S},
weights = nothing;
rng = Random.GLOBAL_RNG) where {S, T}
t = treeclassifier.fit(
X = features,
Y = labels,
W = weights,
loss = treeclassifier.util.zero_one,
max_features = size(features, 2),
max_depth = 1,
min_samples_leaf = 1,
min_samples_split = 2,
min_purity_increase = 0.0;
rng = rng)
return _convert(t.root, t.list, labels[t.labels])
end
function build_tree(
labels :: AbstractVector{T},
features :: AbstractMatrix{S},
n_subfeatures = 0,
max_depth = -1,
min_samples_leaf = 1,
min_samples_split = 2,
min_purity_increase = 0.0;
loss = util.entropy :: Function,
rng = Random.GLOBAL_RNG) where {S, T}
if max_depth == -1
max_depth = typemax(Int)
end
if n_subfeatures == 0
n_subfeatures = size(features, 2)
end
rng = mk_rng(rng)::Random.AbstractRNG
t = treeclassifier.fit(
X = features,
Y = labels,
W = nothing,
loss = loss,
max_features = Int(n_subfeatures),
max_depth = Int(max_depth),
min_samples_leaf = Int(min_samples_leaf),
min_samples_split = Int(min_samples_split),
min_purity_increase = Float64(min_purity_increase),
rng = rng)
return _convert(t.root, t.list, labels[t.labels])
end
function prune_tree(tree::LeafOrNode{S, T}, purity_thresh=1.0) where {S, T}
if purity_thresh >= 1.0
return tree
end
function _prune_run(tree::LeafOrNode{S, T}, purity_thresh::Real) where {S, T}
N = length(tree)
if N == 1 ## a Leaf
return tree
elseif N == 2 ## a stump
all_labels = [tree.left.values; tree.right.values]
majority = majority_vote(all_labels)
matches = findall(all_labels .== majority)
purity = length(matches) / length(all_labels)
if purity >= purity_thresh
return Leaf{T}(majority, all_labels)
else
return tree
end
else
return Node{S, T}(tree.featid, tree.featval,
_prune_run(tree.left, purity_thresh),
_prune_run(tree.right, purity_thresh))
end
end
pruned = _prune_run(tree, purity_thresh)
while length(pruned) < length(tree)
tree = pruned
pruned = _prune_run(tree, purity_thresh)
end
return pruned
end
apply_tree(leaf::Leaf{T}, feature::AbstractVector{S}) where {S, T} = leaf.majority
function apply_tree(tree::Node{S, T}, features::AbstractVector{S}) where {S, T}
if tree.featid == 0
return apply_tree(tree.left, features)
elseif features[tree.featid] < tree.featval
return apply_tree(tree.left, features)
else
return apply_tree(tree.right, features)
end
end
function apply_tree(tree::LeafOrNode{S, T}, features::AbstractMatrix{S}) where {S, T}
N = size(features,1)
predictions = Array{T}(undef, N)
for i in 1:N
predictions[i] = apply_tree(tree, features[i, :])
end
if T <: Float64
return Float64.(predictions)
else
return predictions
end
end
""" apply_tree_proba(::Node, features, col_labels::AbstractVector)
computes P(L=label|X) for each row in `features`. It returns a `N_row x
n_labels` matrix of probabilities, each row summing up to 1.
`col_labels` is a vector containing the distinct labels
(eg. ["versicolor", "virginica", "setosa"]). It specifies the column ordering
of the output matrix. """
apply_tree_proba(leaf::Leaf{T}, features::AbstractVector{S}, labels) where {S, T} =
compute_probabilities(labels, leaf.values)
function apply_tree_proba(tree::Node{S, T}, features::AbstractVector{S}, labels) where {S, T}
if tree.featval === nothing
return apply_tree_proba(tree.left, features, labels)
elseif features[tree.featid] < tree.featval
return apply_tree_proba(tree.left, features, labels)
else
return apply_tree_proba(tree.right, features, labels)
end
end
apply_tree_proba(tree::LeafOrNode{S, T}, features::AbstractMatrix{S}, labels) where {S, T} =
stack_function_results(row->apply_tree_proba(tree, row, labels), features)
function build_forest(
labels :: AbstractVector{T},
features :: AbstractMatrix{S},
n_subfeatures = -1,
n_trees = 10,
partial_sampling = 0.7,
max_depth = -1,
min_samples_leaf = 1,
min_samples_split = 2,
min_purity_increase = 0.0;
rng = Random.GLOBAL_RNG) where {S, T}
if n_trees < 1
throw("the number of trees must be >= 1")
end
if !(0.0 < partial_sampling <= 1.0)
throw("partial_sampling must be in the range (0,1]")
end
if n_subfeatures == -1
n_features = size(features, 2)
n_subfeatures = round(Int, sqrt(n_features))
end
t_samples = length(labels)
n_samples = floor(Int, partial_sampling * t_samples)
forest = Vector{LeafOrNode{S, T}}(undef, n_trees)
entropy_terms = util.compute_entropy_terms(n_samples)
loss = (ns, n) -> util.entropy(ns, n, entropy_terms)
if rng isa Random.AbstractRNG
Threads.@threads for i in 1:n_trees
inds = rand(rng, 1:t_samples, n_samples)
forest[i] = build_tree(
labels[inds],
features[inds,:],
n_subfeatures,
max_depth,
min_samples_leaf,
min_samples_split,
min_purity_increase,
loss = loss,
rng = rng)
end
elseif rng isa Integer # each thread gets its own seeded rng
Threads.@threads for i in 1:n_trees
Random.seed!(rng + i)
inds = rand(1:t_samples, n_samples)
forest[i] = build_tree(
labels[inds],
features[inds,:],
n_subfeatures,
max_depth,
min_samples_leaf,
min_samples_split,
min_purity_increase,
loss = loss)
end
else
throw("rng must of be type Integer or Random.AbstractRNG")
end
return Ensemble{S, T}(forest)
end
function apply_forest(forest::Ensemble{S, T}, features::AbstractVector{S}) where {S, T}
n_trees = length(forest)
votes = Array{T}(undef, n_trees)
for i in 1:n_trees
votes[i] = apply_tree(forest.trees[i], features)
end
if T <: Float64
return mean(votes)
else
return majority_vote(votes)
end
end
function apply_forest(forest::Ensemble{S, T}, features::AbstractMatrix{S}) where {S, T}
N = size(features,1)
predictions = Array{T}(undef, N)
for i in 1:N
predictions[i] = apply_forest(forest, features[i, :])
end
return predictions
end
""" apply_forest_proba(forest::Ensemble, features, col_labels::AbstractVector)
computes P(L=label|X) for each row in `features`. It returns a `N_row x
n_labels` matrix of probabilities, each row summing up to 1.
`col_labels` is a vector containing the distinct labels
(eg. ["versicolor", "virginica", "setosa"]). It specifies the column ordering
of the output matrix. """
function apply_forest_proba(forest::Ensemble{S, T}, features::AbstractVector{S}, labels) where {S, T}
votes = [apply_tree(tree, features) for tree in forest.trees]
return compute_probabilities(labels, votes)
end
apply_forest_proba(forest::Ensemble{S, T}, features::AbstractMatrix{S}, labels) where {S, T} =
stack_function_results(row->apply_forest_proba(forest, row, labels),
features)
function build_adaboost_stumps(
labels :: AbstractVector{T},
features :: AbstractMatrix{S},
n_iterations :: Integer;
rng = Random.GLOBAL_RNG) where {S, T}
N = length(labels)
weights = ones(N) / N
stumps = Node{S, T}[]
coeffs = Float64[]
for i in 1:n_iterations
new_stump = build_stump(labels, features, weights; rng=rng)
predictions = apply_tree(new_stump, features)
err = _weighted_error(labels, predictions, weights)
new_coeff = 0.5 * log((1.0 + err) / (1.0 - err))
matches = labels .== predictions
weights[(!).(matches)] *= exp(new_coeff)
weights[matches] *= exp(-new_coeff)
weights /= sum(weights)
push!(coeffs, new_coeff)
push!(stumps, new_stump)
if err < 1e-6
break
end
end
return (Ensemble{S, T}(stumps), coeffs)
end
function apply_adaboost_stumps(stumps::Ensemble{S, T}, coeffs::AbstractVector{Float64}, features::AbstractVector{S}) where {S, T}
n_stumps = length(stumps)
counts = Dict()
for i in 1:n_stumps
prediction = apply_tree(stumps.trees[i], features)
counts[prediction] = get(counts, prediction, 0.0) + coeffs[i]
end
top_prediction = stumps.trees[1].left.majority
top_count = -Inf
for (k,v) in counts
if v > top_count
top_prediction = k
top_count = v
end
end
return top_prediction
end
function apply_adaboost_stumps(stumps::Ensemble{S, T}, coeffs::AbstractVector{Float64}, features::AbstractMatrix{S}) where {S, T}
n_samples = size(features, 1)
predictions = Array{T}(undef, n_samples)
for i in 1:n_samples
predictions[i] = apply_adaboost_stumps(stumps, coeffs, features[i,:])
end
return predictions
end
""" apply_adaboost_stumps_proba(stumps::Ensemble, coeffs, features, labels::AbstractVector)
computes P(L=label|X) for each row in `features`. It returns a `N_row x
n_labels` matrix of probabilities, each row summing up to 1.
`col_labels` is a vector containing the distinct labels
(eg. ["versicolor", "virginica", "setosa"]). It specifies the column ordering
of the output matrix. """
function apply_adaboost_stumps_proba(stumps::Ensemble{S, T}, coeffs::AbstractVector{Float64},
features::AbstractVector{S}, labels::AbstractVector{T}) where {S, T}
votes = [apply_tree(stump, features) for stump in stumps.trees]
compute_probabilities(labels, votes, coeffs)
end
function apply_adaboost_stumps_proba(stumps::Ensemble{S, T}, coeffs::AbstractVector{Float64},
features::AbstractMatrix{S}, labels::AbstractVector{T}) where {S, T}
stack_function_results(row->apply_adaboost_stumps_proba(stumps, coeffs, row, labels), features)
end