說明:
spark.randomForest
在 SparkDataFrame 上擬合隨機森林回歸模型或分類模型。用戶可以調用summary
獲取擬合隨機森林模型的摘要,調用predict
對新數據進行預測,調用write.ml
/read.ml
保存/加載擬合模型。有關詳細信息,請參閱 Random Forest Regression 和 Random Forest Classification
用法:
spark.randomForest(data, formula, ...)
## S4 method for signature 'SparkDataFrame,formula'
spark.randomForest(
data,
formula,
type = c("regression", "classification"),
maxDepth = 5,
maxBins = 32,
numTrees = 20,
impurity = NULL,
featureSubsetStrategy = "auto",
seed = NULL,
subsamplingRate = 1,
minInstancesPerNode = 1,
minInfoGain = 0,
checkpointInterval = 10,
maxMemoryInMB = 256,
cacheNodeIds = FALSE,
handleInvalid = c("error", "keep", "skip"),
bootstrap = TRUE
)
## S4 method for signature 'RandomForestRegressionModel'
summary(object)
## S3 method for class 'summary.RandomForestRegressionModel'
print(x, ...)
## S4 method for signature 'RandomForestClassificationModel'
summary(object)
## S3 method for class 'summary.RandomForestClassificationModel'
print(x, ...)
## S4 method for signature 'RandomForestRegressionModel'
predict(object, newData)
## S4 method for signature 'RandomForestClassificationModel'
predict(object, newData)
## S4 method for signature 'RandomForestRegressionModel,character'
write.ml(object, path, overwrite = FALSE)
## S4 method for signature 'RandomForestClassificationModel,character'
write.ml(object, path, overwrite = FALSE)
參數:
data
用於訓練的 SparkDataFrame。formula
要擬合的模型的符號說明。目前僅支持少數公式運算符,包括'~'、':'、'+'和'-'。...
傳遞給該方法的附加參數。type
模型類型,"regression" 或 "classification" 之一,以適合maxDepth
樹的最大深度 (>= 0)。maxBins
用於離散連續特征和選擇如何在每個節點上分割特征的最大 bin 數。更多的 bin 提供更高的粒度。必須 >= 2 且 >= 任何類別特征中的類別數。numTrees
要訓練的樹數 (>= 1)。impurity
用於信息增益計算的標準。對於回歸,必須是"variance"。對於分類,必須是"entropy"和"gini"之一,默認為"gini"。featureSubsetStrategy
在每個樹節點處拆分要考慮的特征數量。支持的選項:"auto"(為任務自動選擇:如果 numTrees == 1,設置為 "all." 如果 numTrees > 1(森林),設置為 "sqrt" 用於分類,設置為 "onethird" 用於回歸),"all" (使用所有特征),"onethird"(使用 1/3 的特征),"sqrt"(使用 sqrt(特征數量)),"log2"(使用 log2(特征數量)),"n":(當 n 在 (0, 1.0] 範圍內時,使用 n * 特征數。當 n 在 (1,特征數) 範圍內時,使用 n 個特征。默認為"auto"。seed
用於隨機數生成的整數種子。subsamplingRate
用於學習每個決策樹的訓練數據的分數,在 (0, 1] 範圍內。minInstancesPerNode
拆分後每個孩子必須擁有的最小實例數。minInfoGain
在樹節點處考慮的拆分的最小信息增益。checkpointInterval
設置檢查點間隔 (>= 1) 或禁用檢查點 (-1) 的參數。注意:如果未設置檢查點目錄,此設置將被忽略。maxMemoryInMB
分配給直方圖聚合的最大內存(以 MiB 為單位)。cacheNodeIds
如果為 FALSE,算法會將樹傳遞給執行器以將實例與節點匹配。如果為 TRUE,算法將為每個實例緩存節點 ID。緩存可以加快對更深層次樹的訓練。用戶可以通過設置 checkpointInterval 來設置緩存檢查點的頻率或禁用它。handleInvalid
如何處理分類模型中字符串類型的特征和標簽列中的無效數據(看不見的標簽或NULL值)。支持的選項:"skip"(過濾掉包含無效數據的行)、"error"(拋出錯誤)、"keep"(將無效數據放入特殊的附加存儲桶中,索引為 numLabels)。默認為"error"。bootstrap
構建樹時是否使用引導樣本。object
擬合的隨機森林回歸模型或分類模型。x
summary
返回的隨機森林回歸模型或分類模型的摘要對象。newData
用於測試的 SparkDataFrame。path
保存模型的目錄。overwrite
如果輸出路徑已經存在,是否覆蓋。默認為 FALSE,這意味著如果輸出路徑存在則拋出異常。
返回:
spark.randomForest
返回一個擬合的隨機森林模型。
summary
返回擬合模型的匯總信息,是一個列表。組件列表包括formula
(公式)、numFeatures
(特征數量)、features
(特征列表)、featureImportances
(特征重要性)、maxDepth
(樹的最大深度)、numTrees
(樹的數量)和treeWeights
(樹的權重)。
predict
返回一個 SparkDataFrame,其中包含在名為 "prediction" 的列中標記的預測值。
注意:
spark.randomForest 自 2.1.0 起
摘要(RandomForestRegressionModel)自 2.1.0 起
從 2.1.0 開始的 print.summary.RandomForestRegressionModel
摘要(隨機森林分類模型)自 2.1.0 起
從 2.1.0 開始的 print.summary.RandomForestClassificationModel
從 2.1.0 開始預測(RandomForestRegressionModel)
從 2.1.0 開始預測(隨機森林分類模型)
write.ml(RandomForestRegressionModel, character) 自 2.1.0
write.ml(RandomForestClassificationModel, character) 自 2.1.0
例子:
# fit a Random Forest Regression Model
df <- createDataFrame(longley)
model <- spark.randomForest(df, Employed ~ ., type = "regression", maxDepth = 5, maxBins = 16)
# get the summary of the model
summary(model)
# make predictions
predictions <- predict(model, df)
# save and load the model
path <- "path/to/model"
write.ml(model, path)
savedModel <- read.ml(path)
summary(savedModel)
# fit a Random Forest Classification Model
t <- as.data.frame(Titanic)
df <- createDataFrame(t)
model <- spark.randomForest(df, Survived ~ Freq + Age, "classification")
相關用法
- R SparkR spark.decisionTree用法及代碼示例
- R SparkR spark.powerIterationClustering用法及代碼示例
- R SparkR spark.svmLinear用法及代碼示例
- R SparkR spark.gaussianMixture用法及代碼示例
- R SparkR spark.naiveBayes用法及代碼示例
- R SparkR spark.getSparkFiles用法及代碼示例
- R SparkR spark.survreg用法及代碼示例
- R SparkR spark.lm用法及代碼示例
- R SparkR spark.mlp用法及代碼示例
- R SparkR spark.fmClassifier用法及代碼示例
- R SparkR spark.gbt用法及代碼示例
- R SparkR spark.getSparkFilesRootDirectory用法及代碼示例
- R SparkR spark.logit用法及代碼示例
- R SparkR spark.addFile用法及代碼示例
- R SparkR spark.isoreg用法及代碼示例
- R SparkR spark.als用法及代碼示例
- R SparkR spark.glm用法及代碼示例
- R SparkR spark.lapply用法及代碼示例
- R SparkR spark.fmRegressor用法及代碼示例
- R SparkR spark.kmeans用法及代碼示例
注:本文由純淨天空篩選整理自spark.apache.org大神的英文原創作品 Random Forest Model for Regression and Classification。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。