randomForest 包提供了利用随机森林算法解决分类和回归问题的功能;我们这里只关注随机森林算法在分类问题中的应用
首先安装这个R包
install.packages("randomForest")
安装成功后,首先运行一下example
library(randomForset)
?randomForset
通过查看函数的帮助文档,可以看到对应的example
data(iris)
set.seed(71)
iris.rf
proximity=TRUE)
print(iris.rf)
代码很简单,全部的功能都封装在 randomForest 这个R包中,首先来看下用于分类的数据
> str(iris)
'data.frame':150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> head(iris)
Sepal.L