Skip to content

Commit d4ec9fc

Browse files
committed
Make explain predictions work for new data again
1 parent f2e69ec commit d4ec9fc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

R/commonMachineLearningRegression.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,14 @@
714714
purpose <- "classification"
715715
}
716716
predictors <- options[["predictors"]][which(decodeColNames(options[["predictors"]]) %in% model[["jaspVars"]][["decoded"]]$predictors)]
717+
# Ensure same encoding in prediction data as in training data
718+
for (i in seq_along(predictors)) {
719+
for (j in seq_along(model[["jaspVars"]][["decoded"]]$predictors)) {
720+
if (decodeColNames(predictors[i]) == model[["jaspVars"]][["decoded"]]$predictors[j]) {
721+
predictors[i] <- model[["jaspVars"]][["encoded"]]$predictors[j]
722+
}
723+
}
724+
}
717725
} else {
718726
predictors <- options[["predictors"]]
719727
}
@@ -746,6 +754,14 @@
746754
x_test <- result[["test"]][, predictors]
747755
} else {
748756
explainer <- model[["explainer"]]
757+
# Ensure same encoding in prediction data as in training data
758+
for (i in seq_along(colnames(dataset))) {
759+
for (j in seq_along(model[["jaspVars"]][["decoded"]]$predictors)) {
760+
if (decodeColNames(colnames(dataset)[i]) == model[["jaspVars"]][["decoded"]]$predictors[j]) {
761+
colnames(dataset)[i] <- model[["jaspVars"]][["encoded"]]$predictors[j]
762+
}
763+
}
764+
}
749765
x_test <- dataset[, predictors]
750766
predictions <- .mlPredictionsState(model, dataset, options, jaspResults, ready)[options[["fromIndex"]]:options[["toIndex"]]]
751767
}

0 commit comments

Comments
 (0)