Skip to content

Commit

Permalink
Fix readme, fix MLJ sprinkle. Bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
giopaglia committed Jul 23, 2023
1 parent 284c058 commit 109140a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "e54bda2e-c571-11ec-9d64-0242ac120002"
license = "MIT"
desc = "Julia implementation of Modal Decision Trees and Random Forest algorithms"
authors = ["Giovanni PAGLIARINI"]
version = "0.1.2"
version = "0.1.3"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Additionally, these models:
Simply type the following commands in Julia's REPL:
```julia
using Pkg; Pkg.add(url="https://github.com/aclai-lab/ModalDecisionTrees.jl")
using Pkg; Pkg.add("ModalDecisionTrees");
```
-->

Expand All @@ -45,15 +45,16 @@ Simply type the following commands in Julia's REPL:

```julia
# Install package
using Pkg; Pkg.add(url="https://github.com/aclai-lab/ModalDecisionTrees.jl")
using Pkg; Pkg.add("MLJ");
using Pkg; Pkg.add("ModalDecisionTrees");

# Import packages
using MLJ
using ModalDecisionTrees
using Random

# Load an example dataset (a temporal one)
X, y = load_japanesevowels()
X, y = ModalDecisionTrees.load_japanesevowels()
N = length(y)

# Instantiate an MLJ machine based on a Modal Decision Tree with ≥ 4 samples at leaf
Expand All @@ -67,8 +68,8 @@ train_idxs, test_idxs = p[1:round(Int, N*.8)], p[round(Int, N*.8)+1:end]
fit!(mach, rows=train_idxs)

# Perform predictions, compute accuracy
yhat = predict(mach, X[test_idxs,:])
accuracy = sum(yhat .== y[test_idxs])/length(yhat)
yhat = predict_mode(mach, X[test_idxs,:])
accuracy = MLJ.accuracy(yhat, y[test_idxs])

# Print model
report(mach).printmodel(3)
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/MLJ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ function MMI.fit(m::SymbolicModel, verbosity::Integer, X, y, var_grouping, class
printmodel = ModelPrinter(m, model, solemodel, var_grouping),
sprinkle = (Xnew, ynew)->begin
(Xnew, ynew, var_grouping, classes_seen, w) = MMI.reformat(m, Xnew, ynew; passive_mode = true)
translate_function(ModalDecisionTrees.sprinkle(model, Xnew, ynew))
preds, m = ModalDecisionTrees.sprinkle(model, Xnew, ynew)
preds, translate_function(m)
end,
# TODO remove redundancy?
model = solemodel,
Expand Down
14 changes: 7 additions & 7 deletions src/interfaces/MLJ/docstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ using Random
tree = ModalDecisionTree(min_samples_leaf=4)
# Load an example dataset (a temporal one)
X, y = load_japanesevowels()
X, y = ModalDecisionTrees.load_japanesevowels()
N = length(y)
mach = machine(tree, X, y)
Expand All @@ -190,8 +190,8 @@ train_idxs, test_idxs = p[1:round(Int, N*.8)], p[round(Int, N*.8)+1:end]
fit!(mach, rows=train_idxs)
# Perform predictions, compute accuracy
yhat = predict(mach, X[test_idxs,:])
accuracy = sum(yhat .== y[test_idxs])/length(yhat)
yhat = predict_mode(mach, X[test_idxs,:])
accuracy = MLJ.accuracy(yhat, y[test_idxs])
# Access raw model
fitted_params(mach).model
Expand Down Expand Up @@ -277,7 +277,7 @@ using Random
forest = ModalRandomForest(ntrees = 50)
# Load an example dataset (a temporal one)
X, y = load_japanesevowels()
X, y = ModalDecisionTrees.load_japanesevowels()
N = length(y)
mach = machine(forest, X, y)
Expand All @@ -291,9 +291,9 @@ fit!(mach, rows=train_idxs)
# Perform predictions, compute accuracy
Xnew = X[test_idxs,:]
yhat = predict(mach, Xnew) # probabilistic predictions
ynew = predict_mode(mach, Xnew) # point predictions
accuracy = sum(ynew .== y[test_idxs])/length(yhat)
ynew = predict_mode(mach, Xnew) # point predictions
accuracy = MLJ.accuracy(ynew, y[test_idxs])
yhat = predict_mode(mach, Xnew) # probabilistic predictions
pdf.(yhat, "1") # probabilities for one of the classes ("1")
# Access raw model
Expand Down
2 changes: 1 addition & 1 deletion src/other/example-datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function load_japanesevowels()
minimum_n_points = minimum(collect(Iterators.flatten(eachrow(length.(X[:,Not([:speaker, :take, :utterance])])))))
new_X = (x->x[1:minimum_n_points]).(X[:,Not([:speaker, :take, :utterance])])

new_X, varnames = SoleData.dataframe2cube(new_X)
# new_X, varnames = SoleData.dataframe2cube(new_X)

new_X, Y
end
4 changes: 4 additions & 0 deletions test/classification/japanesevowels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ t = ModalDecisionTree(;
# Load an example dataset (a temporal one)
X, y = ModalDecisionTrees.load_japanesevowels()

X, varnames = SoleData.dataframe2cube(X)

p = randperm(Random.MersenneTwister(2), 100)
X, y = X[:, :, p], y[p]

Expand Down Expand Up @@ -93,6 +95,8 @@ end

X, y = ModalDecisionTrees.load_japanesevowels()

X, varnames = SoleData.dataframe2cube(X)

multilogiset, var_grouping = ModalDecisionTrees.wrapdataset(X, ModalDecisionTree(; min_samples_leaf = 1))

# A Modal Decision Tree
Expand Down

0 comments on commit 109140a

Please sign in to comment.