简体   繁体   English

如何在R中为随机森林设置“分类”类型

[英]How to set “classification” type for a random forest in R

I have some training ( train ) and testing ( test ) data. 我有一些培训( train )和测试( test )数据。 This is the way I'm building a forest: 这是我建立森林的方式:

forest <- randomForest(y ~ . - y, data=train, nodesize=25, ntree=200, type="classification")

I wish to emphasize that train$y and test$y may take only integer values like 1, 2, 3, 4 , etc. But when I build my prediction like 我想强调一下, train$ytest$y只能采用整数值,例如1, 2, 3, 4等。但是当我建立预测时

pred = predict(forest, newdata=test, type="class")

And check pred contents with: 并检查pred内容:

pred[1:10]

I see such an output: 我看到这样的输出:

3        5       12       14       26       27       33       48       50 
2.863208 2.466447 2.476652 2.894254 4.726897 2.378187 2.034159 3.977969 2.264780  

Whereas I expected to see something like: 而我希望看到类似的东西:

3         5        12      14
1         3         1       7    # <- I wish to see integers here

So, what is wrong with that? 那么,这有什么问题呢?

Change your code as follows: 更改您的代码,如下所示:

forest <- randomForest(factor(y) ~., data=train, nodesize=25, ntree=200)

here using -y in the formula is redundant as has been already mentioned by 'LyzandeR' 这里在公式中使用-y是多余的,正如'LyzandeR'已经提到的那样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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