-
Notifications
You must be signed in to change notification settings - Fork 13
/
Trees_MLJ.jl
200 lines (182 loc) · 8.42 KB
/
Trees_MLJ.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
# MLJ interface for Decision Trees/Random Forests models
import MLJModelInterface # It seems that having done this in the top module is not enought
const MMI = MLJModelInterface # We need to repoeat it here
export DecisionTreeRegressor, RandomForestRegressor, DecisionTreeClassifier, RandomForestClassifier
# ------------------------------------------------------------------------------
# Model Structure declarations..
mutable struct DecisionTreeRegressor <: MMI.Deterministic
maxDepth::Int64
minGain::Float64
minRecords::Int64
maxFeatures::Int64
splittingCriterion::Function
rng::AbstractRNG
end
DecisionTreeRegressor(;
maxDepth=0, #typemax(Int)
minGain=0.0,
minRecords=2,
maxFeatures=0,
splittingCriterion=variance,
rng = Random.GLOBAL_RNG,
) = DecisionTreeRegressor(maxDepth,minGain,minRecords,maxFeatures,splittingCriterion,rng)
mutable struct DecisionTreeClassifier <: MMI.Probabilistic
maxDepth::Int64
minGain::Float64
minRecords::Int64
maxFeatures::Int64
splittingCriterion::Function
rng::AbstractRNG
end
DecisionTreeClassifier(;
maxDepth=0,
minGain=0.0,
minRecords=2,
maxFeatures=0,
splittingCriterion=gini,
rng = Random.GLOBAL_RNG,
) = DecisionTreeClassifier(maxDepth,minGain,minRecords,maxFeatures,splittingCriterion,rng)
mutable struct RandomForestRegressor <: MMI.Deterministic
nTrees::Int64
maxDepth::Int64
minGain::Float64
minRecords::Int64
maxFeatures::Int64
splittingCriterion::Function
β::Float64
rng::AbstractRNG
end
RandomForestRegressor(;
nTrees=30,
maxDepth=0,
minGain=0.0,
minRecords=2,
maxFeatures=0,
splittingCriterion=variance,
β=0.0,
rng = Random.GLOBAL_RNG,
) = RandomForestRegressor(nTrees,maxDepth,minGain,minRecords,maxFeatures,splittingCriterion,β,rng)
mutable struct RandomForestClassifier <: MMI.Probabilistic
nTrees::Int64
maxDepth::Int64
minGain::Float64
minRecords::Int64
maxFeatures::Int64
splittingCriterion::Function
β::Float64
rng::AbstractRNG
end
RandomForestClassifier(;
nTrees=30,
maxDepth=0,
minGain=0.0,
minRecords=2,
maxFeatures=0,
splittingCriterion=gini,
β=0.0,
rng = Random.GLOBAL_RNG,
) = RandomForestClassifier(nTrees,maxDepth,minGain,minRecords,maxFeatures,splittingCriterion,β,rng)
#=
# skipped for now..
# ------------------------------------------------------------------------------
# Hyperparameters ranges definition (for automatic tuning)
MMI.hyperparameter_ranges(::Type{<:DecisionTreeRegressor}) = (
# (range(Float64, :alpha, lower=0, upper=1, scale=:log),
# range(Int, :beta, lower=1, upper=Inf, origin=100, unit=50, scale=:log),
# nothing)
range(Int64,:maxDepth,lower=0,upper=Inf,scale=:log),
range(Float64,:minGain,lower=0,upper=Inf,scale=:log),
range(Int64,:minRecords,lower=0,upper=Inf,scale=:log),
range(Int64,:maxFeatures,lower=0,upper=Inf,scale=:log),
nothing
)
=#
# ------------------------------------------------------------------------------
# Fit functions...
function MMI.fit(model::Union{DecisionTreeRegressor,RandomForestRegressor}, verbosity, X, y)
x = MMI.matrix(X) # convert table to matrix
maxDepth = model.maxDepth == 0 ? size(x,1) : model.maxDepth
if (typeof(model) == DecisionTreeRegressor)
maxFeatures = model.maxFeatures == 0 ? size(x,2) : model.maxFeatures
fitresult = buildTree(x, y, maxDepth=maxDepth, minGain=model.minGain, minRecords=model.minRecords, maxFeatures=maxFeatures, splittingCriterion=model.splittingCriterion,rng=model.rng)
else
maxFeatures = model.maxFeatures == 0 ? Int(round(sqrt(size(x,2)))) : model.maxFeatures
fitresult = buildForest(x, y, model.nTrees, maxDepth=maxDepth, minGain=model.minGain, minRecords=model.minRecords, maxFeatures=maxFeatures, splittingCriterion=model.splittingCriterion, β=model.β,rng=model.rng)
end
cache=nothing
report=nothing
return fitresult, cache, report
end
function MMI.fit(model::Union{DecisionTreeClassifier,RandomForestClassifier}, verbosity, X, y)
x = MMI.matrix(X) # convert table to matrix
a_target_element = y[1] # a CategoricalValue or CategoricalString
#y_plain = MMI.int(y) .- 1 # integer relabeling should start at 0
yarray = convert(Vector{eltype(levels(y))},y) # convert to a simple Array{T}
maxDepth = model.maxDepth == 0 ? size(x,1) : model.maxDepth
if (typeof(model) == DecisionTreeClassifier)
maxFeatures = model.maxFeatures == 0 ? size(x,2) : model.maxFeatures
fittedmodel = buildTree(x, yarray, maxDepth=maxDepth, minGain=model.minGain, minRecords=model.minRecords, maxFeatures=maxFeatures, splittingCriterion=model.splittingCriterion, forceClassification=true,rng=model.rng)
else
maxFeatures = model.maxFeatures == 0 ? Int(round(sqrt(size(x,2)))) : model.maxFeatures
fittedmodel = buildForest(x, yarray, model.nTrees, maxDepth=maxDepth, minGain=model.minGain, minRecords=model.minRecords, maxFeatures=maxFeatures, splittingCriterion=model.splittingCriterion, forceClassification=true, β=model.β,rng=model.rng)
end
cache = nothing
report = nothing
fitresult = (fittedmodel,a_target_element)
return (fitresult, cache, report)
end
# ------------------------------------------------------------------------------
# Predict functions....
MMI.predict(model::Union{DecisionTreeRegressor,RandomForestRegressor}, fitresult, Xnew) = Trees.predict(fitresult, MMI.matrix(Xnew))
function MMI.predict(model::Union{DecisionTreeClassifier,RandomForestClassifier}, fitresult, Xnew)
fittedModel = fitresult[1]
a_target_element = fitresult[2]
decode = MMI.decoder(a_target_element)
classes = MMI.classes(a_target_element)
#println(typeof(classes))
nLevels = length(classes)
nRecords = MMI.nrows(Xnew)
treePredictions = Trees.predict(fittedModel, MMI.matrix(Xnew),rng=model.rng)
predMatrix = zeros(Float64,(nRecords,nLevels))
# Transform the predictions from a vector of dictionaries to a matrix
# where the rows are the PMF of each record
for n in 1:nRecords
for (c,cl) in enumerate(classes)
predMatrix[n,c] = get(treePredictions[n],cl,0.0)
end
end
#predictions = [MMI.UnivariateFinite(classes, predMatrix[i,:])
# for i in 1:nRecords]
predictions = MMI.UnivariateFinite(classes, predMatrix)
return predictions
end
# ------------------------------------------------------------------------------
# Model metadata for registration in MLJ...
MMI.metadata_model(DecisionTreeRegressor,
input_scitype = MMI.Table(MMI.Missing, MMI.Known), # also ok: MMI.Table(Union{MMI.Missing, MMI.Known}),
target_scitype = AbstractVector{<: MMI.Continuous}, # for a supervised model, what target?
supports_weights = false, # does the model support sample weights?
descr = "A simple Decision Tree for regression with support for Missing data, from the Beta Machine Learning Toolkit (BetaML).",
load_path = "BetaML.Trees.DecisionTreeRegressor"
)
MMI.metadata_model(RandomForestRegressor,
input_scitype = MMI.Table(MMI.Missing, MMI.Known),
target_scitype = AbstractVector{<: MMI.Continuous},
supports_weights = false,
descr = "A simple Random Forest ensemble for regression with support for Missing data, from the Beta Machine Learning Toolkit (BetaML).",
load_path = "BetaML.Trees.RandomForestRegressor"
)
MMI.metadata_model(DecisionTreeClassifier,
input_scitype = MMI.Table(MMI.Missing, MMI.Known),
target_scitype = AbstractVector{<: Union{MMI.Missing,MMI.Finite}},
supports_weights = false,
descr = "A simple Decision Tree for classification with support for Missing data, from the Beta Machine Learning Toolkit (BetaML).",
load_path = "BetaML.Trees.DecisionTreeClassifier"
)
MMI.metadata_model(RandomForestClassifier,
input_scitype = MMI.Table(MMI.Missing, MMI.Known),
target_scitype = AbstractVector{<: Union{MMI.Missing,MMI.Finite}},
supports_weights = false,
descr = "A simple Random Forest ensemble for classification with support for Missing data, from the Beta Machine Learning Toolkit (BetaML).",
load_path = "BetaML.Trees.RandomForestClassifier"
)