简体   繁体   中英

random forest text classification giving extra rows in prediction

I am using random forest for text classification. My input data is having 17197 rows.

> nrow(sparse_4testing)
[1] 17197

I am using

set.seed(123)
tweetRand = randomForest(label ~ ., data = train_sparse, importance=TRUE, nTree=500)

predicrRand_test=predict(tweetRand, data=sparse_4testing)
q1=data.frame(ifelse(predicrRand_test>0.5,1,0))

The issue is when I am doing a sanity check I am getting extra rows in q1

> nrow(q1)
[1] 22373  

I do not understand the issue. I am new to machine learning. Please help me out. I have run the model multiple time. Still getting the same issue.

> nrow(predicrRand_test)

NULL
> head(predicrRand_test)
            1             3             6             7             9            10 
 1.858321e-01 -8.326673e-17  1.321640e-01  2.222222e-04  2.345304e-02  1.651133e-01 
> head(q1)
   ifelse.predicrRand_test...0.05..1..0.
1                                      1
3                                      0
6                                      1
7                                      0
9                                      0
10                                     1

> length(predicrRand_test)
[1] 22373

The issue is due to wrong argument name in predict - it should be newdata , not data ( docs ):

predicrRand_test=predict(tweetRand, newdata=sparse_4testing)

As it is now, your code ignores the data argument, and simply returns the predictions on the training set in the predicrRand_test dataframe.

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