R语言 randomForest 函数

randomForest函数

randomForest在RStudio中的Documentation:

randomForest {randomForest}    R Documentation
Classification and Regression with Random Forest
Description
randomForest implements Breiman's random forest algorithm (based on Breiman and Cutler's original Fortran code) for classification and regression. It can also be used in unsupervised mode for assessing proximities among data points.

Usage
## S3 method for class 'formula'
randomForest(formula, data=NULL, ..., subset, na.action=na.fail)
## Default S3 method:
randomForest(x, y=NULL,  xtest=NULL, ytest=NULL, ntree=500,
             mtry=if (!is.null(y) && !is.factor(y))
             max(floor(ncol(x)/3), 1) else floor(sqrt(ncol(x))),
             replace=TRUE, classwt=NULL, cutoff, strata,
             sampsize = if (replace) nrow(x) else ceiling(.632*nrow(x)),
             nodesize = if (!is.null(y) && !is.factor(y)) 5 else 1,
             maxnodes = NULL,
             importance=FALSE, localImp=FALSE, nPerm=1,
             proximity, oob.prox=proximity,
             norm.votes=TRUE, do.trace=FALSE,
             keep.forest=!is.null(y) && is.null(xtest), corr.bias=FALSE,
             keep.inbag=FALSE, ...)
## S3 method for class 'randomForest'
print(x, ...)
Arguments
data    
an optional data frame containing the variables in the model. By default the variables are taken from the environment which randomForest is called from.

subset    
an index vector indicating which rows should be used. (NOTE: If given, this argument must be named.)

na.action    
A function to specify the action to be taken if NAs are found. (NOTE: If given, this argument must be named.)

默认na.action = na.fail,即不允许有na存在。如果数据集中没有missing value,就不需要修改此argument.

实际应用时若数据集中有missing value,则可修改为na.action = na.omit(忽略NA)或者na.action = na.roughfix(简单填充缺失值)。

用rfImpute()能够获得更优的拟合填充值。 

x, formula    
a data frame or a matrix of predictors, or a formula describing the model to be fitted (for the print method, an randomForest object).

y    
A response vector. If a factor, classification is assumed, otherwise regression is assumed. If omitted, randomForest will run in unsupervised mode.

xtest    
a data frame or matrix (like x) containing predictors for the test set.

ytest    
response for the test set.

ntree    
Number of trees to grow. This should not be set to too small a number, to ensure that every input row gets predicted at least a few times.

注意到这里的ntree即是tree总数,即B的值。R中默认是500,Python中默认为100.

mtry    
Number of variables randomly sampled as candidates at each split. Note that the default values are different for classification (sqrt(p) where p is number of variables in x) and regression (p/3)

这里的mtry即是RSF size, 也就是M的取值。

对Categorical Y默认取值为RSF size = floor(sqrt(M));对Continuous Y默认取值为RSF size = floor(M/3) 

replace    
Should sampling of cases be done with or

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随机森林(Random Forest)是一种机器学习算法,它可以用于分类和回归问题。在R语言中,可以使用randomForest包来实现随机森林算法。下面是一些基本的使用方法: 1. 安装和加载randomForest包: ```R install.packages("randomForest") # 安装包 library(randomForest) # 加载包 ``` 2. 准备数据集,将数据集分成训练集和测试集: ```R data(iris) # 导入iris数据集 trainIndex <- sample(1:nrow(iris), 0.7*nrow(iris)) # 随机选取70%的数据作为训练集 trainData <- iris[trainIndex, ] # 训练集 testData <- iris[-trainIndex, ] # 测试集 ``` 3. 构建随机森林模型: ```R rfModel <- randomForest(Species ~ ., data = trainData, ntree = 500, importance = TRUE) ``` 其中,Species是目标变量,trainData是训练数据集,ntree是指定随机森林中树的数量,importance参数用于计算变量重要性。 4. 预测新数据: ```R pred <- predict(rfModel, testData) ``` 其中,rfModel是已经训练好的随机森林模型,testData是测试数据集。 5. 评估模型性能: 可以使用混淆矩阵、准确率、召回率等指标来评估模型性能。 ```R table(pred, testData$Species) # 混淆矩阵 ``` 可以使用caret包中的函数来计算准确率、召回率等指标。 ```R library(caret) confusionMatrix(data = pred, reference = testData$Species) # 计算混淆矩阵等指标 ``` 以上是randomForest包的基本使用方法,随机森林还有一些高级功能,比如out-of-bag误差估计、变量重要性等,可以参考包的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值