Skip to content

Commit c667f3c

Browse files
authored
Merge pull request #311 from FluxML/levels
Bump compat for CategoricalArrays to 1.0
2 parents 8e02638 + 108508e commit c667f3c

File tree

6 files changed

+13
-14
lines changed

6 files changed

+13
-14
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MLJFlux"
22
uuid = "094fc8d1-fd35-5302-93ea-dabda2abf845"
33
authors = ["Anthony D. Blaom <[email protected]>", "Ayush Shridhar <[email protected]>"]
4-
version = "0.6.6"
4+
version = "0.6.7"
55

66
[deps]
77
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
@@ -17,15 +17,15 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1717
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1818

1919
[compat]
20-
CategoricalArrays = "0.10"
20+
CategoricalArrays = "1"
2121
ColorTypes = "0.10.3, 0.11, 0.12"
2222
ComputationalResources = "0.3.2"
2323
Flux = "0.14, 0.15, 0.16"
2424
MLJModelInterface = "1.11"
2525
Metalhead = "0.9.3"
2626
Optimisers = "0.3.2, 0.4"
2727
ProgressMeter = "1.7.1"
28-
StatisticalMeasures = "0.1"
28+
StatisticalMeasures = "0.3"
2929
Statistics = "<0.0.1, 1"
3030
Tables = "1.0"
3131
julia = "1.10"

src/classifier.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ data `X` and `y`.
88
"""
99
function MLJFlux.shape(model::NeuralNetworkClassifier, X, y)
1010
X = X isa Matrix ? Tables.table(X) : X
11-
levels = MLJModelInterface.classes(y[1])
11+
levels = CategoricalArrays.levels(y[1])
1212
n_output = length(levels)
1313
n_input = Tables.schema(X).names |> length
1414
return (n_input, n_output)
@@ -31,7 +31,7 @@ MLJFlux.fitresult(
3131
y,
3232
ordinal_mappings = nothing,
3333
embedding_matrices = nothing,
34-
) = (chain, MLJModelInterface.classes(y[1]), ordinal_mappings, embedding_matrices)
34+
) = (chain, levels(y[1]), ordinal_mappings, embedding_matrices)
3535

3636
function MLJModelInterface.predict(
3737
model::NeuralNetworkClassifier,

src/core.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ tomat(y::Vector) = reshape(y, size(y, 1), 1)
253253
reformat(y, ::Type{<:AbstractVector{<:Union{Continuous,Count}}}) =
254254
reshape(y, 1, length(y))
255255
function reformat(y, ::Type{<:AbstractVector{<:Finite}})
256-
levels = y |> first |> MLJModelInterface.classes
256+
levels = y |> first |> CategoricalArrays.levels
257257
return Flux.onehotbatch(y, levels)
258258
end
259259

@@ -285,7 +285,7 @@ end
285285
function collate(model::NeuralNetworkBinaryClassifier, X, y, verbosity)
286286
row_batches = Base.Iterators.partition(1:nrows(y), model.batch_size)
287287
Xmatrix = _f32(reformat(X), verbosity)
288-
yvec = (y .== classes(y)[2])' # convert to boolean
288+
yvec = (y .== levels(y)[2])' # convert to boolean
289289
return [_get(Xmatrix, b) for b in row_batches], [_get(yvec, b) for b in row_batches]
290290
end
291291

@@ -294,4 +294,3 @@ function _f32(x::AbstractArray, verbosity)
294294
verbosity > 0 && @info "MLJFlux: converting input data to Float32"
295295
return Float32.(x)
296296
end
297-

src/encoders.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function ordinal_encoder_fit(X; featinds)
1717
# 2. Use feature mapper to compute the mapping of each level in each column
1818
for i in featinds
1919
feat_col = Tables.getcolumn(Tables.columns(X), i)
20-
feat_levels = levels(feat_col)
20+
feat_levels = CategoricalArrays.unwrap.(levels(feat_col))
2121
# Check if feat levels is already ordinal encoded in which case we skip
2222
(Set([Float32(i) for i in 1:length(feat_levels)]) == Set(feat_levels)) && continue
2323
# Compute the dict using the given feature_mapper function
@@ -64,10 +64,10 @@ function ordinal_encoder_transform(X, mapping_matrix)
6464
# Create the transformation function for each column
6565
if ind in keys(mapping_matrix)
6666
train_levels = keys(mapping_matrix[ind])
67-
test_levels = levels(col)
67+
test_levels = CategoricalArrays.unwrap.(levels(col))
6868
check_unkown_levels(train_levels, test_levels)
6969
level2scalar = mapping_matrix[ind]
70-
new_col = unwrap.(recode(col, level2scalar...))
70+
new_col = CategoricalArrays.unwrap.(recode(col, level2scalar...))
7171
push!(new_feats, new_col)
7272
else
7373
push!(new_feats, col)

src/image.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function shape(model::ImageClassifier, X, y)
2-
levels = MLJModelInterface.classes(y[1])
2+
levels = CategoricalArrays.levels(y)
33
n_output = length(levels)
44
n_input = size(X[1])
55

@@ -18,7 +18,7 @@ build(model::ImageClassifier, rng, shape) =
1818
model.finaliser)
1919

2020
fitresult(model::ImageClassifier, chain, y, ::Any, ::Any) =
21-
(chain, MLJModelInterface.classes(y[1]))
21+
(chain, levels(y))
2222

2323
function MLJModelInterface.predict(model::ImageClassifier, fitresult, Xnew)
2424
chain, levels = fitresult

test/entity_embedding.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ entityprops = [
3636
EE1 = Flux.trainables(embedder.embedders[2])[1] # (newdim, levels) = (5, 10)
3737
EE2 = Flux.trainables(embedder.embedders[4])[1] # (newdim, levels) = (2, 2)
3838

39-
## One-hot encoding
39+
## One-hot encoding
4040
z2_hot = Flux.onehotbatch(z2, levels(z2))
4141
z4_hot = Flux.onehotbatch(z4, levels(z4))
4242

0 commit comments

Comments
 (0)