Search code examples
rknnmlr

Retrieving distance matrix from kknn model


Is it possible to retrieve the distance matrix from the kknn model when using mlr package in R and cross validation?

library("mlr")

data(iris)

task = makeClassifTask(data = iris, target = "Species")

lnr = makeLearner(
  cl = "classif.kknn",
  predict.type = "prob",
  k = 5,
  kernel = "gaussian",
  scale = TRUE
)

cv = crossval(
  learner = lnr,
  task = task,
  iters = 4,
  stratify = TRUE,
  measures = acc,
  show.info = FALSE,
  model = TRUE
)

str(cv$models[1])

I can't see anything related in cv$models or cv$pred.


Solution

  • The return value of crossval is a ResampleResult, which contains the models fitted in the individual iterations in the $models member (note that this is a list). The models are the objects returned by the underlying learner, so in each model there should be a member $D$ that contains the distance matrix.

    See the tutorial for more information.

    Edit: In this particular case, you don't get the learner models in the usual place because kknn is a (model-less) clusterer and the kknn function doesn't actually get called by mlr until you predict. The "model" returned by train is just the training data (with a few additional bits).

    The predict function returns just the predictions and not the model, so unfortunately in this particular case you can't get to the distance matrices directly. However, you can get the learner model from mlr and call kknn on that to get the distance matrices:

     kknn(getTaskFormula(cv$models[[1]]$task.desc),
      train = cv$models[[1]]$learner.model$data,
      test = iris)$D