# Function \`confusion\_matrix \` in MLJ always fails

**URL:** <https://discourse.julialang.org/t/function-confusion-matrix-in-mlj-always-fails/81521>\
**Category:** General Usage\
**Tags:** mlj\
**Created:** [May 23, 2022, 3:39pm UTC](https://discourse.julialang.org/t/function-confusion-matrix-in-mlj-always-fails/81521 "2022-05-23T15:39:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiz/32/211800_2.png) [@tiZ](https://discourse.julialang.org/u/tiZ)\
**Post date:** [May 23, 2022, 3:39pm UTC](https://discourse.julialang.org/t/function-confusion-matrix-in-mlj-always-fails/81521/1 "2022-05-23T15:39:19Z")

</div>

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

```julia
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:

```julia
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

> [@Using measure in MLJ to evaluate binary classifier](https://discourse.julialang.org/t/using-measure-in-mlj-to-evaluate-binary-classifier/67025/2):
>
> You can make your categorical arrays ordered: using MLJ predStr = categorical( ["fast", "fast", "slow"]; ordered = true, levels = ["slow", "fast"] ) gtStr = categorical( ["slow", "fast", "slow"]; ordered = true, levels = ["slow", "fast"] ) julia\> confusion\_matrix(predStr, gtStr) ┌───────────────────────────┐ │ Ground Truth │ ┌─────────────┼─────────────┬─────────────┤ │ Predicted │ slow │ fast │ ├─────────────┼─…

Thank for you reading and help.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [May 23, 2022, 4:05pm UTC](https://discourse.julialang.org/t/function-confusion-matrix-in-mlj-always-fails/81521/2 "2022-05-23T16:05:23Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 23, 2022, 4:14pm UTC](https://discourse.julialang.org/t/function-confusion-matrix-in-mlj-always-fails/81521/3 "2022-05-23T16:14:03Z")

</div>

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

```julia
# 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](https://discourse.julialang.org/t/should-we-have-a-package-update-psa/61923) and work with project environments, limited to only the specific packages you need for a specific script, instead.

---

<div class="post-metadata">

**Author:** ![tiZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiz/32/211800_2.png) [@tiZ](https://discourse.julialang.org/u/tiZ)\
**Post date:** [May 23, 2022, 5:02pm UTC](https://discourse.julialang.org/t/function-confusion-matrix-in-mlj-always-fails/81521/4 "2022-05-23T17:02:55Z")

</div>

Thanks for the helpful tips.

I stupidly put all packages in the default env.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 23, 2022, 7:57pm UTC](https://discourse.julialang.org/t/function-confusion-matrix-in-mlj-always-fails/81521/5 "2022-05-23T19:57:49Z")

</div>

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](https://sylvaticus.github.io/BetaML.jl/stable/Utils.html#BetaML.Utils.ConfusionMatrix)
