Skip to content

Commit 0e1a20d

Browse files
committed
Update code to Julia 0.6 and remove definitions that are no longer
needed. Remove Compat dependency since it is no longer used
1 parent 43a0ead commit 0e1a20d

File tree

6 files changed

+41
-51
lines changed

6 files changed

+41
-51
lines changed

REQUIRE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
julia 0.5
22
ScikitLearnBase 0.0.4
3-
Compat 0.18.0

src/DecisionTree.jl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ __precompile__()
22

33
module DecisionTree
44

5-
using Compat
6-
75
import Base: length, convert, promote_rule, show, start, next, done
86

97
export Leaf, Node, Ensemble, print_tree, depth, build_stump, build_tree,
@@ -24,14 +22,7 @@ export DecisionTreeClassifier, DecisionTreeRegressor, RandomForestClassifier,
2422
#####################################
2523
##### Compatilibity Corrections #####
2624

27-
const Range1 = Range
2825
_int(x) = map(y->round(Integer, y), x)
29-
float(x) = map(Float64, x)
30-
31-
_squeeze(m::Matrix, i::Int) = squeeze(m, i)
32-
_squeeze(v::Vector, i::Int) = v
33-
neg(arr) = map(!, arr)
34-
3526

3627
###########################
3728
########## Types ##########
@@ -50,7 +41,7 @@ immutable Node
5041
right::Union{Leaf,Node}
5142
end
5243

53-
@compat const LeafOrNode = Union{Leaf,Node}
44+
const LeafOrNode = Union{Leaf,Node}
5445

5546
immutable Ensemble
5647
trees::Vector{Node}

src/classification.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ end
2323
# and returns a Matrix containing the resulting vectors, stacked vertically
2424
function stack_function_results(row_fun::Function, X::Matrix)
2525
N = size(X, 1)
26-
N_cols = length(row_fun(_squeeze(X[1,:],1))) # gets the number of columns
26+
N_cols = length(row_fun(X[1, :])) # gets the number of columns
2727
out = Array{Float64}(N, N_cols)
2828
for i in 1:N
29-
out[i, :] = row_fun(_squeeze(X[i,:],1))
29+
out[i, :] = row_fun(X[i, :])
3030
end
3131
return out
3232
end
@@ -94,7 +94,7 @@ function _split_neg_z1_loss(labels::Vector, features::Matrix, weights::Vector)
9494
domain_i = sort(unique(features[:,i]))
9595
for thresh in domain_i[2:end]
9696
cur_split = features[:,i] .< thresh
97-
value = _neg_z1_loss(labels[cur_split], weights[cur_split]) + _neg_z1_loss(labels[neg(cur_split)], weights[neg(cur_split)])
97+
value = _neg_z1_loss(labels[cur_split], weights[cur_split]) + _neg_z1_loss(labels[(!).(cur_split)], weights[(!).(cur_split)])
9898
if value > best_val
9999
best_val = value
100100
best = (i, thresh)
@@ -114,7 +114,7 @@ function build_stump(labels::Vector, features::Matrix, weights=[0];
114114
split = features[:,id] .< thresh
115115
return Node(id, thresh,
116116
Leaf(majority_vote(labels[split]), labels[split]),
117-
Leaf(majority_vote(labels[neg(split)]), labels[neg(split)]))
117+
Leaf(majority_vote(labels[(!).(split)]), labels[(!).(split)]))
118118
end
119119

120120
function build_tree(labels::Vector, features::Matrix, nsubfeatures=0, maxdepth=-1; rng=Base.GLOBAL_RNG)
@@ -131,7 +131,7 @@ function build_tree(labels::Vector, features::Matrix, nsubfeatures=0, maxdepth=-
131131
id, thresh = S
132132
split = features[:,id] .< thresh
133133
labels_left = labels[split]
134-
labels_right = labels[neg(split)]
134+
labels_right = labels[(!).(split)]
135135
pure_left = all(labels_left .== labels_left[1])
136136
pure_right = all(labels_right .== labels_right[1])
137137
if pure_right && pure_left
@@ -141,7 +141,7 @@ function build_tree(labels::Vector, features::Matrix, nsubfeatures=0, maxdepth=-
141141
elseif pure_left
142142
return Node(id, thresh,
143143
Leaf(labels_left[1], labels_left),
144-
build_tree(labels_right,features[neg(split),:], nsubfeatures,
144+
build_tree(labels_right,features[(!).(split),:], nsubfeatures,
145145
max(maxdepth-1, -1); rng=rng))
146146
elseif pure_right
147147
return Node(id, thresh,
@@ -152,7 +152,7 @@ function build_tree(labels::Vector, features::Matrix, nsubfeatures=0, maxdepth=-
152152
return Node(id, thresh,
153153
build_tree(labels_left,features[split,:], nsubfeatures,
154154
max(maxdepth-1, -1); rng=rng),
155-
build_tree(labels_right,features[neg(split),:], nsubfeatures,
155+
build_tree(labels_right,features[(!).(split),:], nsubfeatures,
156156
max(maxdepth-1, -1); rng=rng))
157157
end
158158
end
@@ -202,10 +202,10 @@ function apply_tree(tree::LeafOrNode, features::Matrix)
202202
N = size(features,1)
203203
predictions = Array{Any}(N)
204204
for i in 1:N
205-
predictions[i] = apply_tree(tree, _squeeze(features[i,:],1))
205+
predictions[i] = apply_tree(tree, features[i, :])
206206
end
207207
if typeof(predictions[1]) <: Float64
208-
return float(predictions)
208+
return Float64.(predictions)
209209
else
210210
return predictions
211211
end
@@ -252,7 +252,7 @@ function apply_forest(forest::Ensemble, features::Vector)
252252
ntrees = length(forest)
253253
votes = Array{Any}(ntrees)
254254
for i in 1:ntrees
255-
votes[i] = apply_tree(forest.trees[i],features)
255+
votes[i] = apply_tree(forest.trees[i], features)
256256
end
257257
if typeof(votes[1]) <: Float64
258258
return mean(votes)
@@ -265,10 +265,10 @@ function apply_forest(forest::Ensemble, features::Matrix)
265265
N = size(features,1)
266266
predictions = Array{Any}(N)
267267
for i in 1:N
268-
predictions[i] = apply_forest(forest, _squeeze(features[i,:],1))
268+
predictions[i] = apply_forest(forest, features[i, :])
269269
end
270270
if typeof(predictions[1]) <: Float64
271-
return float(predictions)
271+
return Float64.(predictions)
272272
else
273273
return predictions
274274
end
@@ -302,7 +302,7 @@ function build_adaboost_stumps(labels::Vector, features::Matrix, niterations::In
302302
err = _weighted_error(labels, predictions, weights)
303303
new_coeff = 0.5 * log((1.0 + err) / (1.0 - err))
304304
matches = labels .== predictions
305-
weights[neg(matches)] *= exp(new_coeff)
305+
weights[(!).(matches)] *= exp(new_coeff)
306306
weights[matches] *= exp(-new_coeff)
307307
weights /= sum(weights)
308308
push!(coeffs, new_coeff)
@@ -336,7 +336,7 @@ function apply_adaboost_stumps(stumps::Ensemble, coeffs::Vector{Float64}, featur
336336
N = size(features,1)
337337
predictions = Array{Any}(N)
338338
for i in 1:N
339-
predictions[i] = apply_adaboost_stumps(stumps, coeffs, _squeeze(features[i,:],1))
339+
predictions[i] = apply_adaboost_stumps(stumps, coeffs, features[i,:])
340340
end
341341
return predictions
342342
end

src/measures.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ function show(io::IO, cm::ConfusionMatrix)
1616
show(io, cm.kappa)
1717
end
1818

19-
function _hist_add!{T}(counts::Dict{T,Int}, labels::Vector{T}, region::Range1{Int})
19+
function _hist_add!{T}(counts::Dict{T,Int}, labels::Vector{T}, region::UnitRange{Int})
2020
for i in region
2121
lbl = labels[i]
2222
counts[lbl] = get(counts, lbl, 0) + 1
2323
end
2424
return counts
2525
end
2626

27-
function _hist_sub!{T}(counts::Dict{T,Int}, labels::Vector{T}, region::Range1{Int})
27+
function _hist_sub!{T}(counts::Dict{T,Int}, labels::Vector{T}, region::UnitRange{Int})
2828
for i in region
2929
lbl = labels[i]
3030
counts[lbl] -= 1
3131
end
3232
return counts
3333
end
3434

35-
function _hist_shift!{T}(counts_from::Dict{T,Int}, counts_to::Dict{T,Int}, labels::Vector{T}, region::Range1{Int})
35+
function _hist_shift!{T}(counts_from::Dict{T,Int}, counts_to::Dict{T,Int}, labels::Vector{T}, region::UnitRange{Int})
3636
for i in region
3737
lbl = labels[i]
3838
counts_from[lbl] -= 1
@@ -41,7 +41,7 @@ function _hist_shift!{T}(counts_from::Dict{T,Int}, counts_to::Dict{T,Int}, label
4141
return nothing
4242
end
4343

44-
_hist{T}(labels::Vector{T}, region::Range1{Int} = 1:endof(labels)) =
44+
_hist{T}(labels::Vector{T}, region::UnitRange{Int} = 1:endof(labels)) =
4545
_hist_add!(Dict{T,Int}(), labels, region)
4646

4747
function _set_entropy{T}(counts::Dict{T,Int}, N::Int)
@@ -136,7 +136,7 @@ function _nfoldCV(classifier::Symbol, labels, features, args...)
136136
for i in 1:nfolds
137137
test_inds = falses(N)
138138
test_inds[(i - 1) * ntest + 1 : i * ntest] = true
139-
train_inds = neg(test_inds)
139+
train_inds = (!).(test_inds)
140140
test_features = features[inds[test_inds],:]
141141
test_labels = labels[inds[test_inds]]
142142
train_features = features[inds[train_inds],:]
@@ -201,7 +201,7 @@ function _nfoldCV{T<:Float64, U<:Real}(regressor::Symbol, labels::Vector{T}, fea
201201
for i in 1:nfolds
202202
test_inds = falses(N)
203203
test_inds[(i - 1) * ntest + 1 : i * ntest] = true
204-
train_inds = neg(test_inds)
204+
train_inds = (!).(test_inds)
205205
test_features = features[inds[test_inds],:]
206206
test_labels = labels[inds[test_inds]]
207207
train_features = features[inds[train_inds],:]

src/regression.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function build_stump{T<:Float64, U<:Real}(labels::Vector{T}, features::Matrix{U}
9797
split = features[:,id] .< thresh
9898
return Node(id, thresh,
9999
Leaf(mean(labels[split]), labels[split]),
100-
Leaf(mean(labels[neg(split)]), labels[neg(split)]))
100+
Leaf(mean(labels[(!).(split)]), labels[(!).(split)]))
101101
end
102102

103103
function build_tree{T<:Float64, U<:Real}(labels::Vector{T}, features::Matrix{U}, maxlabels=5, nsubfeatures=0, maxdepth=-1; rng=Base.GLOBAL_RNG)
@@ -115,7 +115,7 @@ function build_tree{T<:Float64, U<:Real}(labels::Vector{T}, features::Matrix{U},
115115
split = features[:,id] .< thresh
116116
return Node(id, thresh,
117117
build_tree(labels[split], features[split,:], maxlabels, nsubfeatures, max(maxdepth-1, -1); rng=rng),
118-
build_tree(labels[neg(split)], features[neg(split),:], maxlabels, nsubfeatures, max(maxdepth-1, -1); rng=rng))
118+
build_tree(labels[(!).(split)], features[(!).(split),:], maxlabels, nsubfeatures, max(maxdepth-1, -1); rng=rng))
119119
end
120120

121121
function build_forest{T<:Float64, U<:Real}(labels::Vector{T}, features::Matrix{U}, nsubfeatures::Integer, ntrees::Integer, maxlabels=5, partialsampling=0.7, maxdepth=-1; rng=Base.GLOBAL_RNG)

test/classification_hetero.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
using Base.Test
44
using DecisionTree
55

6-
m, n = 10^2, 5;
6+
m, n = 10^2, 5
77

8-
tf = [trues(Int(m/2)) falses(Int(m/2))];
9-
inds = randperm(m);
10-
labels = string.(tf[inds]);
8+
tf = [trues(Int(m/2)) falses(Int(m/2))]
9+
inds = randperm(m)
10+
labels = string.(tf[inds])
1111

12-
features = Array{Any}(m, n);
13-
features[:,:] = randn(m, n);
14-
features[:,2] = string.(tf[randperm(m)]);
15-
features[:,3] = round(Int, features[:,3]);
16-
features[:,4] = tf[inds];
12+
features = Array{Any}(m, n)
13+
features[:,:] = randn(m, n)
14+
features[:,2] = string.(tf[randperm(m)])
15+
features[:,3] = map(t -> round(Int, t), features[:,3]) # can just become round.(Int, features[:,3]) when Julia 0.5 support is dropped
16+
features[:,4] = tf[inds]
1717

18-
model = build_tree(labels, features);
19-
preds = apply_tree(model, features);
20-
cm = confusion_matrix(labels, preds);
18+
model = build_tree(labels, features)
19+
preds = apply_tree(model, features)
20+
cm = confusion_matrix(labels, preds)
2121
@test cm.accuracy > 0.7
2222

23-
model = build_forest(labels, features,2,3);
24-
preds = apply_forest(model, features);
25-
cm = confusion_matrix(labels, preds);
23+
model = build_forest(labels, features,2,3)
24+
preds = apply_forest(model, features)
25+
cm = confusion_matrix(labels, preds)
2626
@test cm.accuracy > 0.7
2727

28-
model, coeffs = build_adaboost_stumps(labels, features, 7);
29-
preds = apply_adaboost_stumps(model, coeffs, features);
30-
cm = confusion_matrix(labels, preds);
28+
model, coeffs = build_adaboost_stumps(labels, features, 7)
29+
preds = apply_adaboost_stumps(model, coeffs, features)
30+
cm = confusion_matrix(labels, preds)
3131
@test cm.accuracy > 0.7
3232

0 commit comments

Comments
 (0)