Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Machine fit! from the model not working #187

Closed
davidbp opened this issue Jul 28, 2019 · 3 comments
Closed

Machine fit! from the model not working #187

davidbp opened this issue Jul 28, 2019 · 3 comments

Comments

@davidbp
Copy link
Contributor

davidbp commented Jul 28, 2019

Hello,

While trying to replicate the current the "getting started" tutorial in the docs I get an error trying to fit a machine:

This is the code I tried

using MLJ
using Pkg
using RDatasets
using DecisionTree

iris = dataset("datasets", "iris");
X, y = iris[:,1:4], iris[:,5];

#show((Pkg.status("MLJ"))

Pkg.status("MLJ")
println("size(X) = ", size(X))


## Define machine and train it
println("Define tree model")
tree_model = DecisionTree.DecisionTreeClassifier(max_depth=4)
println("Train model")
tree_machine = machine(tree_model, X, y)

This is what I get

    Status `~/.julia/environments/v1.1/Project.toml`
  [add582a8] MLJ v0.2.3
size(X) = (150, 4)
Define tree model
Train model
ERROR: LoadError: MethodError: no method matching machine(::DecisionTreeClassifier, ::DataFrame, ::CategoricalArray{String,1,UInt8,String,CategoricalString{UInt8},Union{}})
Closest candidates are:
  machine(!Matched::MLJBase.Model, ::Any...) at /Users/david/.julia/packages/MLJ/HXakA/src/machines.jl:165
  machine(!Matched::MLJBase.Model, ::Any, !Matched::AbstractNode) at /Users/david/.julia/packages/MLJ/HXakA/src/networks.jl:353
  machine(!Matched::MLJBase.Model, !Matched::AbstractNode, ::Any) at /Users/david/.julia/packages/MLJ/HXakA/src/networks.jl:354
Stacktrace:
 [1] top-level scope at none:0
 [2] include at ./boot.jl:326 [inlined]
 [3] include_relative(::Module, ::String) at ./loading.jl:1038
 [4] include(::Module, ::String) at ./sysimg.jl:29
 [5] exec_options(::Base.JLOptions) at ./client.jl:267
 [6] _start() at ./client.jl:436
in expression starting at /Users/david/Documents/git_stuff/julia_tutorials/MLJ/minimal_working_example_problem.jl:19```

@ablaom
Copy link
Member

ablaom commented Jul 28, 2019

tree_model = DecisionTree.DecisionTreeClassifier(max_depth=4)

This is not an MLJ Model but an unwrapped struct from the DecisionTree package with the same name that you have exposed to Main. To use the MLJ model, delete using DecisionTree - which you do not need (or replace with import DecisionTree) and replace the above line with

@load DecisionTreeClassifier
tree_model = DecisionTreeClassifier(max_depth=4)

@ablaom ablaom closed this as completed Jul 28, 2019
@davidbp
Copy link
Contributor Author

davidbp commented Jul 28, 2019

Hello Anthony,

You are right, I was trying to create a machine with a type that is not a MLJBase.Model.

In any case the fit method returns an error

This

using MLJ
using RDatasets

iris = dataset("datasets", "iris");
X, y = iris[:,1:4], iris[:,5];
println("size(X) = ", size(X))

## Define machine and train it 
println("Define tree model")
@load DecisionTreeClassifier
tree_model = DecisionTreeClassifier(max_depth=2)

#tree_model = DecisionTree.DecisionTreeClassifier(max_depth=4)
println("Define Machine")
tree_machine = machine(tree_model, X, y)

println("Train model")
train_ind, test_ind = partition(eachindex(y), 0.7, shuffle=true);
fit!(tree_machine, rows=train_ind)

prints

size(X) = (150, 4)
Define tree model
import MLJModels ✔
import DecisionTree ✔
import MLJModels.DecisionTree_.DecisionTreeClassifier ✔
Define Machine
Train model
[ Info: Training Machine{DecisionTreeClassifier} @ 1…72.
ERROR: LoadError: MethodError: no method matching build_tree(::CategoricalArray{String,1,UInt8,String,CategoricalString{UInt8},Union{}}, ::Array{Float64,2}, ::Float64, ::Int64, ::Int64, ::Int64, ::Float64)
Closest candidates are:
  build_tree(!Matched::Array{T<:Float64,1}, ::Array{S,2}, ::Any, ::Any, ::Any, ::Any, ::Any; rng) where {S, T<:Float64} at /Users/david/.julia/packages/DecisionTree/nfB7O/src/regression/main.jl:27
  build_tree(!Matched::Array{T,1}, ::Array{S,2}, ::Any, ::Any, ::Any, ::Any, ::Any; rng) where {S, T} at /Users/david/.julia/packages/DecisionTree/nfB7O/src/classification/main.jl:83
  build_tree(!Matched::Array{T<:Float64,1}, ::Array{S,2}, ::Any, ::Any, ::Any, ::Any) where {S, T<:Float64} at /Users/david/.julia/packages/DecisionTree/nfB7O/src/regression/main.jl:27
  ...
Stacktrace:
 [1] fit(::DecisionTreeClassifier, ::Int64, ::DataFrame, ::CategoricalArray{String,1,UInt8,String,CategoricalString{UInt8},Union{}}) at /Users/david/.julia/packages/MLJModels/p7doS/src/DecisionTree.jl:110
 [2] #fit!#3(::Array{Int64,1}, ::Int64, ::Bool, ::Function, ::Machine{DecisionTreeClassifier}) at /Users/david/.julia/packages/MLJ/HXakA/src/machines.jl:131
 [3] (::getfield(StatsBase, Symbol("#kw##fit!")))(::NamedTuple{(:rows,),Tuple{Array{Int64,1}}}, ::typeof(fit!), ::Machine{DecisionTreeClassifier}) at ./none:0
 [4] top-level scope at none:0
 [5] include at ./boot.jl:326 [inlined]
 [6] include_relative(::Module, ::String) at ./loading.jl:1038
 [7] include(::Module, ::String) at ./sysimg.jl:29
 [8] exec_options(::Base.JLOptions) at ./client.jl:267
 [9] _start() at ./client.jl:436
in expression starting at /Users/david/Documents/git_stuff/julia_tutorials/MLJ/minimal_working_example_problem.jl:26

@ablaom
Copy link
Member

ablaom commented Jul 29, 2019

I'm pretty sure this will go away if you update MLJ to the latest tagged release. It is caused by a compatibility problem with CategoricalArrays.

Let me know if it doesn't

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants