Function `confusion_matrix ` in MLJ always fails

Hello,

I try to caculate a confusion matrix in Julia, but I meet some ERROR and it is hard for me to debug, any suggestion? Thanks you.

here is my code

using MLJ, CategoricalArrays 
#  Status `~/.julia/environments/v1.7/Project.toml`
#  [324d7699] CategoricalArrays v0.8.3
#  [add582a8] MLJ v0.16.5

yHat =  categorical(["A", "B", "A"], ordered=true)
yOrig = categorical(["A", "B", "B"], ordered=true)

confusion_matrix(yHat,yOrig)

but these code ouput error:

ERROR: TypeError: non-boolean (Nothing) used in boolean context
Stacktrace:
 [1] _confmat(Ε·::CategoricalVector{String, UInt32, String, CategoricalValue{String, UInt32}, Union{}}, y::CategoricalVector{String, UInt32, String, CategoricalValue{String, UInt32}, Union{}}; rev::Nothing, perm::Nothing, warn::Bool)
   @ MLJBase ~/.julia/packages/MLJBase/hLtde/src/measures/confusion_matrix.jl:98
 [2] (::ConfusionMatrix)(Ε·::CategoricalVector{String, UInt32, String, CategoricalValue{String, UInt32}, Union{}}, y::CategoricalVector{String, UInt32, String, CategoricalValue{String, UInt32}, Union{}})
   @ MLJBase ~/.julia/packages/MLJBase/hLtde/src/measures/confusion_matrix.jl:232
 [3] top-level scope
   @ REPL[9]:1

And I have try the similar solution in another related answer, and I got the same error. I meet this problem with both julia V1.6 and V1.7

Thank for you reading and help.

It works for me, but my package versions are a little bit newer:

(@v1.7) pkg> status CategoricalArrays MLJ
      Status `~/.julia/environments/v1.7/Project.toml`
  [324d7699] CategoricalArrays v0.10.2
  [add582a8] MLJ v0.18.2
3 Likes

I can reproduce the error with the versions you are using, so the key thing you should ask yourself is why you’re using such outdated versions of packages - CategoricalArrays 0.8.3 is from September 2020.

Given that you are showing us

#  Status `~/.julia/environments/v1.7/Project.toml`

I would venture a guess that you are dumping all of your packages into the default environment, thereby (in the long run) almost guaranteeing versioning conflicts. Don’t do this and work with project environments, limited to only the specific packages you need for a specific script, instead.

2 Likes

Thanks for the helpful tips.

I stupidly put all packages in the default env.

Not β€œstupid”… I did that for years before understanding environments in Julia and how useful and actually simple its usage is …

EDIT: BDW, there is also BetaML ConfusionMatrix

1 Like