Loading Model Code
Once the name of a model, and the package providing that model, have been identified (see Model Search) one can either import the model type interactively with @iload
, as shown under Installation, or use @load
as shown below. The @load
macro works from within a module, a package or a function, provided the relevant package providing the MLJ interface has been added to your package environment. It will attempt to load the model type into the global namespace of the module in which @load
is invoked (Main
if invoked at the REPL).
In general, the code providing core functionality for the model (living in a package you should consult for documentation) may be different from the package providing the MLJ interface. Since the core package is a dependency of the interface package, only the interface package needs to be added to your environment.
For instance, suppose you have activated a Julia package environment my_env
that you wish to use for your MLJ project; for example, you have run:
using Pkg
Pkg.activate("my_env", shared=true)
Furthermore, suppose you want to use DecisionTreeClassifier
, provided by the DecisionTree.jl package. Then, to determine which package provides the MLJ interface you call load_path
:
julia> load_path("DecisionTreeClassifier", pkg="DecisionTree")
"MLJDecisionTreeInterface.DecisionTreeClassifier"
In this case, we see that the package required is MLJDecisionTreeInterface.jl. If this package is not in my_env
(do Pkg.status()
to check) you add it by running
julia> Pkg.add("MLJDecisionTreeInterface")
So long as my_env
is the active environment, this action need never be repeated (unless you run Pkg.rm("MLJDecisionTreeInterface")
). You are now ready to instantiate a decision tree classifier:
julia> Tree = @load DecisionTree pkg=DecisionTree
julia> tree = Tree()
which is equivalent to
julia> import MLJDecisionTreeInterface.DecisionTreeClassifier
julia> Tree = MLJDecisionTreeInterface.DecisionTreeClassifier
julia> tree = Tree()
Tip. The specification pkg=...
above can be dropped for the many models that are provided by only a single package.
API
StatisticalTraits.load_path
— Functionload_path(model_name::String, pkg=nothing)
Return the load path for model type with name model_name
, specifying the algorithm=providing package name pkg
to resolve name conflicts, if necessary.
load_path(proxy::NamedTuple)
Return the load path for the model whose name is proxy.name
and whose algorithm-providing package has name proxy.package_name
. For example, proxy
could be any element of the vector returned by models()
.
load_path(model)
Return the load path of a model
instance or type. Usually requires necessary model code to have been separately loaded. Supply strings as above if code is not loaded.
MLJModels.@load
— Macro@load ModelName pkg=nothing verbosity=0 add=false
Import the model type the model named in the first argument into the calling module, specfying pkg
in the case of an ambiguous name (to packages providing a model type with the same name). Returns the model type.
Warning In older versions of MLJ/MLJModels, @load
returned an instance instead.
To automatically add required interface packages to the current environment, specify add=true
. For interactive loading, use @iload
instead.
Examples
Tree = @load DecisionTreeRegressor
tree = Tree()
tree2 = Tree(min_samples_split=6)
SVM = @load SVC pkg=LIBSVM
svm = SVM()
See also @iload
MLJModels.@iload
— Macro@iload ModelName
Interactive alternative to @load. Provides user with an optioin to install (add) the required interface package to the current environment, and to choose the relevant model-providing package in ambiguous cases. See @load