-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.jl
More file actions
84 lines (68 loc) · 1.84 KB
/
Copy pathmain.jl
File metadata and controls
84 lines (68 loc) · 1.84 KB
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
module classifier
include("classifier/main.jl")
end
module regressor
include("regressor/main.jl")
end
const REGRESSING = false
const lib = REGRESSING ? regressor : classifier
X, Y = lib.util.loaddata()
X = Float32.(X)
if REGRESSING
Y = Float32.(Y) # convert things to the right type
else
Y = UInt32.(Y) # convert things to the right type
end
n_samples, n_features = size(X)
# number of features to
# subselect for each split
# in this case, we are using all available features
max_features = 64
# the number of classes in the label dataset
# TODO: autodetect the number of classes
n_classes = 10
# these follow directly from the
# meaning of the same keyword arguments
# in scikit learn
max_depth = typemax(UInt32)
min_samples_leaf = UInt32(1)
min_samples_split = UInt32(2)
# this is the same is min impurity decrease
min_purity_increase = Float32(0.0)
# this is not yet supported
# but will be supported soon
max_leaf_nodes = UInt32(0)
meta = lib.TreeMeta(n_classes, max_features)
stop = lib.StopCondition(
max_depth,
max_leaf_nodes,
min_samples_leaf,
min_samples_split,
min_purity_increase)
# this is a "burn in" run to have everything
# be compiled first
tree = lib.build_tree(X, Y, meta, stop)
@time lib.build_tree(X, Y, meta, stop)
@time lib.build_tree(X, Y, meta, stop)
@time for i in 1:100
lib.build_tree(X, Y, meta, stop)
end
function predict(tree, v :: Array{Float32})
node = tree.root
while !node.is_leaf
node = if v[node.feature] <= node.threshold
node.l
else
node.r
end
end
return tree.list[node.label]
end
# for classification
pred = [predict(tree, X[i, 1:n_features]) for i in 1:n_samples]
loss = if REGRESSING
sum((pred - Y).^2)
else
1 - sum(pred .== Y) / n_samples
end
println("Loss: ", loss)