Skip to content

Confusion matrices as measures #168

@c42f

Description

@c42f

Hi, I'm doing some model building for small data classification using MLJ.

I would like to use the confusion matrix directly for evaluation by summing it over cross validation folds. (Personally I often like to use the confusion matrix directly to assess model performance as it avoids many of the caveats which come from trying to summarize it into a single number.)

With that in mind, I tried something like

labels, features = unpack(...)

leave_one_out_cv = CV(nfolds=length(labels)) # small data!
evaluate!(machine(LinearSVC(), features, labels),
          resampling=leave_one_out_cv,
          measure=confusion_matrix
          )

However, this fails with an error deep in the aggregation machinery (confusion matrices cannot be summed etc)

You seem to almost have the pieces in place for this to work, so for now I worked around this with some type piracy:

# Hacks to allow us to treat the confusion matrix as an MLJBase measure.

MLJBase.aggregation(::Type{typeof(confusion_matrix)}) = MLJBase.Sum()
Base.round(m::MLJBase.ConfusionMatrix; kws...) = m

function Base.:+(m1::MLJBase.ConfusionMatrix, m2::MLJBase.ConfusionMatrix)
    if m1.labels != m2.labels
        throw(ArgumentError("Confusion matrix labels must agree"))
    end
    MLJBase.ConfusionMatrix(m1.mat + m2.mat, m1.labels)
end

Is this questionable? Can it be made to "just work" using something like the above, or am I just abusing the machinery in some way?

(Side note - I'm finding MLJ/MLJBase fantastic so far. In particular I'm greatly enjoying the way that metadata about labels and features is naturally preserved throughout the training pipeline. Bravo, this is the way it should be!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions