forked from JuliaAI/DecisionTree.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
low_precision.jl
123 lines (105 loc) · 3.38 KB
/
low_precision.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
@testset "low_precision.jl" begin
Random.seed!(16)
n,m = 10^3, 5;
features = Array{Any}(undef, n, m);
features[:,:] = rand(n, m);
features[:,1] = round.(Int32, features[:,1]); # convert a column of 32bit integers
weights = rand(-1:1,m);
labels = round.(Int32, features * weights);
model = build_stump(labels, features)
preds = apply_tree(model, features)
@test typeof(preds) == Vector{Int32}
@test depth(model) == 1
n_subfeatures = Int32(0)
max_depth = Int32(-1)
min_samples_leaf = Int32(1)
min_samples_split = Int32(2)
min_purity_increase = 0.0
model = build_tree(
labels, features,
n_subfeatures, max_depth,
min_samples_leaf,
min_samples_split,
min_purity_increase)
preds = apply_tree(model, features)
cm = confusion_matrix(labels, preds)
@test typeof(preds) == Vector{Int32}
@test cm.accuracy > 0.9
n_subfeatures = Int32(0)
n_trees = Int32(10)
partial_sampling = 0.7
max_depth = Int32(-1)
model = build_forest(
labels, features,
n_subfeatures,
n_trees,
partial_sampling,
max_depth)
preds = apply_forest(model, features)
cm = confusion_matrix(labels, preds)
@test typeof(preds) == Vector{Int32}
@test cm.accuracy > 0.9
n_iterations = Int32(15)
model, coeffs = build_adaboost_stumps(labels, features, n_iterations);
preds = apply_adaboost_stumps(model, coeffs, features);
cm = confusion_matrix(labels, preds)
@test typeof(preds) == Vector{Int32}
@test cm.accuracy > 0.7
println("\n##### nfoldCV Classification Tree #####")
n_folds = Int32(3)
pruning_purity = 1.0
max_depth = Int32(-1)
min_samples_leaf = Int32(1)
min_samples_split = Int32(2)
min_purity_increase = 0.0
accuracy = nfoldCV_tree(
labels, features,
n_folds,
pruning_purity,
max_depth,
min_samples_leaf,
min_samples_split,
min_purity_increase)
@test mean(accuracy) > 0.7
println("\n##### nfoldCV Classification Forest #####")
n_trees = Int32(10)
n_subfeatures = Int32(2)
n_folds = Int32(3)
max_depth = Int32(-1)
min_samples_leaf = Int32(5)
min_samples_split = Int32(2)
min_purity_increase = 0.0
accuracy = nfoldCV_forest(
labels, features,
n_folds,
n_subfeatures,
n_trees,
partial_sampling,
max_depth,
min_samples_leaf,
min_samples_split,
min_purity_increase)
@test mean(accuracy) > 0.7
println("\n##### nfoldCV Adaboosted Stumps #####")
n_iterations = Int32(15)
accuracy = nfoldCV_stumps(labels, features, n_folds, n_iterations)
@test mean(accuracy) > 0.7
# Test Int8 labels, and Float16 features
features = Float16.(features)
labels = Int8.(labels)
model = build_stump(labels, features)
preds = apply_tree(model, features)
@test typeof(preds) == Vector{Int8}
model = build_tree(labels, features)
preds = apply_tree(model, features)
@test typeof(preds) == Vector{Int8}
model = build_forest(labels, features)
preds = apply_forest(model, features)
@test typeof(preds) == Vector{Int8}
model = build_tree(labels, features)
preds = apply_tree(model, features)
@test typeof(preds) == Vector{Int8}
model, coeffs = build_adaboost_stumps(labels, features, n_iterations);
preds = apply_adaboost_stumps(model, coeffs, features);
@test typeof(preds) == Vector{Int8}
end # @testset