简体   繁体   中英

Error in `$<-.data.frame`(`*tmp*`, prediction, value = c(`1` = 1L, `2` = 1L, : replacement has 3462 rows, data has 1538

I am getting the above error in R when i try to predict and score the Pruned train tree on Testing data set

> dim(test)
[1] 1538   13

> test$prediction = predict(ptrain_tree, data = test, type="class")

**Error in `$<-.data.frame`(`*tmp*`, prediction, value = c(`1` = 1L, `2` = 1L,  : 
  replacement has 3462 rows, data has 1538**

> test$prob1 = predict(ptrain_tree, data = test, type = "prob" )[,"1"]

**Error in `$<-.data.frame`(`*tmp*`, prob1, value = c(`1` = 0.004296875,  : 
  replacement has 3462 rows, data has 1538**

NOTE- ptrain_tree is my pruned tree created on train data set.

The predict function for the test dataset is a bit tricky. It only works if you omit the data= .

Try: test$prediction = predict(ptrain_tree, test, type="class")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM