MLJ unpack example from MLJ getting started not working

I am trying to run the most basic example from MLJ getting started tutorial and I am getting an error when trying to unpack the iris dataset

y,x = unpack(iris, ==(:target); rng=123)

I get

ERROR: BoundsError: attempt to access Tuple{CategoricalArrays.CategoricalVector{String, UInt32, String, CategoricalArrays.CategoricalValue{String, UInt32}, Union{}}} at index 

The error somehow relates to that there are two return values, if I leave only one (x = unpack…) it works

What’s going on ? I have just copied code from a tutorial how come it is failing to run ?
What am I meesing ?

I am using Julia 1.9.1 On windows 10, Here is the complete program:

import Random.seed!
using CSV, DataFrames, MLJ

iris = load_iris()

iris = DataFrame(iris)
describe(iris)

y,x = unpack(iris, ==(:target); rng=123)

Works fine for me:

julia> using MLJ, DataFrames

julia> iris = DataFrame(load_iris());

julia> y, x = unpack(iris, ==(:target));

julia> typeof(x)
DataFrame

julia> typeof(y)
CategoricalVector{String, UInt32, String, CategoricalValue{String, UInt32}, Union{}}

(I’m not showing the actual values of x and y here as they are large but they are as expected.

With these kinds of questions (“why is this code I copied not working as expected”) I recommend two things:

  • Actually link to where you found the code; and
  • Tell people the versions of all of the packages you are using (i.e. the output of ]st in the REPL) - 9 times out of 10 you are just using an outdated version of a package (or reading outdated docs while using a newer version of the package)

Getting Started · MLJ (alan-turing-institute.github.io)

What version of MLJ & Julia you use ?

The only way the code works for me is like this:

y,x = unpack(iris, ==(:target), !=(:traget); rng=123)

The Julia version should not matter (only to the extent that it restricts package versions), but I’m on 1.9.1.

(jl_adrQDT) pkg> st
Status `...\Temp\jl_adrQDT\Project.toml`
 [add582a8] MLJ v0.19.2

After looking at the Pkg status for some reason the version of MLJ was very very old

So I removed all Julia completely. and reinstalled

I have created a new project add MLJ and now every thing works fine

1 Like

In general I would recommend working in project environments - the problem you encountered most often stems from a crowded default environment, which can be easily avoided.