Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically predict NA for rows w/ NAs and learners that don't support missings #2099

Merged
merged 6 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/generateHyperParsEffect.R
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ plotHyperParsEffect = function(hyperpars.effect.data, x = NULL, y = NULL,
regr.task = makeRegrTask(id = "interp", data = d.run[, c(x, y, z)],
target = z)
mod = train(lrn, regr.task)
prediction = predict(mod, newdata = grid)
prediction = predict(mod, newdata = grid[c(x, y)])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonus bugfix!

grid[, z] = prediction$data[, prediction$predict.type]
grid$learner_status = "Interpolated Point"
grid$iteration = NA
Expand Down
32 changes: 30 additions & 2 deletions R/predictLearner.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,37 @@ predictLearner2 = function(.learner, .model, .newdata, ...) {
.newdata[ns] = mapply(factor, x = .newdata[ns],
levels = fls, SIMPLIFY = FALSE)
}
p = predictLearner(.learner, .model, .newdata, ...)
if ("missings" %nin% getLearnerProperties(.learner))
no.na = removeNALines(.newdata)
else
no.na = list(newdata = .newdata, inserts = FALSE)
if (!nrow(no.na$newdata))
no.na = list(newdata = .newdata, inserts = FALSE) # no choice if all lines contain NA
p = predictLearner(.learner, .model, no.na$newdata, ...)
p = checkPredictLearnerOutput(.learner, .model, p)
return(p)
return(insertLines(p, no.na$inserts))
}

removeNALines = function(newdata) {
namat = is.na(newdata)
narows = apply(namat, 1, any)
return(list(newdata = newdata[!narows, , drop = FALSE], inserts = narows))
}

insertLines = function(prediction, inserts) {
# if (!any(inserts))
# return(prediction)
if (is.matrix(prediction)) {
ret = matrix(nrow = nrow(prediction) + sum(inserts), ncol = ncol(prediction))
ret[!inserts, ] = prediction
colnames(ret) = colnames(prediction)
} else {
ret = rep(NA, length(prediction) + sum(inserts))
ret[!inserts] = prediction
attributes(ret) = attributes(prediction)
names(ret) = NULL
}
return(ret)
}

checkPredictLearnerOutput = function(learner, model, p) {
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test_base_predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,17 @@ test_that("predict works with data.table as newdata", {
expect_warning(predict(mod, newdata = data.table(iris)), regexp = "Provided data for prediction is not a pure data.frame but from class data.table, hence it will be converted.")
})

test_that("predict with NA rows for learners that don't support missings automatically returns NA", {
modknn = train("classif.knn", pid.task)
modrf = train(makeLearner("classif.randomForest", mtry = 1), pid.task)
newdata = getTaskData(pid.task, target.extra = TRUE)$data
newdata.na = newdata
newdata.na[[1]][1] = NA
for (mod in list(modknn, modrf)) {
prediction = predict(mod, newdata = newdata)
prediction.na = predict(mod, newdata = newdata.na)
expect_equal(which(is.na(prediction.na$data$response[1])), 1)
expect_equal(prediction.na$data[-1, ], prediction$data[-1, ])
}
})