gpt4 book ai didi

r - Caret::train - 未估算的值

转载 作者:行者123 更新时间:2023-12-04 02:23:06 33 4
gpt4 key购买 nike

我试图通过将“knnImpute”传递给 Caret 的 train() 方法的 preProcess 参数来估算值。基于以下示例,这些值似乎没有被估算,保留为 NA,然后被忽略。我究竟做错了什么?

任何帮助深表感谢。

library("caret")

set.seed(1234)
data(iris)

# mark 8 of the cells as NA, so they can be imputed
row <- sample (1:nrow (iris), 8)
iris [row, 1] <- NA

# split test vs training
train.index <- createDataPartition (y = iris[,5], p = 0.80, list = F)
train <- iris [ train.index, ]
test <- iris [-train.index, ]

# train the model after imputing the missing data
fit <- train (Species ~ .,
train,
preProcess = c("knnImpute"),
na.action = na.pass,
method = "rpart" )
test$species.hat <- predict (fit, test)

# there is 1 obs. (of 30) in the test set equal to NA
# this 1 obs. was not returned from predict
Error in `$<-.data.frame`(`*tmp*`, "species.hat", value = c(1L, 1L, 1L, :
replacement has 29 rows, data has 30

更新 :我已经能够直接使用 preProcess 函数来估算值。我仍然不明白为什么这在 train 函数中似乎没有发生。
# attempt to impute using nearest neighbors
x <- iris [, 1:4]
pp <- preProcess (x, method = c("knnImpute"))
x.imputed <- predict (pp, newdata = x)

# expect all NAs were populated with an imputed value
stopifnot( all (!is.na (x.imputed)))
stopifnot( length (x) == length (x.imputed))

最佳答案

?predict.train :

 ## S3 method for class 'train'
predict(object, newdata = NULL, type = "raw", na.action = na.omit, ...)

有一个 na.omit这里也:
 > length(predict (fit, test))
[1] 29
> length(predict (fit, test, na.action = na.pass))
[1] 30

最大限度

关于r - Caret::train - 未估算的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20054906/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com