當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R SparkR spark.gbt用法及代碼示例

說明:

spark.gbt 在 SparkDataFrame 上擬合梯度增強樹回歸模型或分類模型。用戶可以調用summary獲取擬合梯度提升樹模型的摘要,調用predict對新數據進行預測,調用write.ml/read.ml保存/加載擬合模型。有關詳細信息,請參閱 GBT RegressionGBT Classification

用法:

spark.gbt(data, formula, ...)

## S4 method for signature 'SparkDataFrame,formula'
spark.gbt(
  data,
  formula,
  type = c("regression", "classification"),
  maxDepth = 5,
  maxBins = 32,
  maxIter = 20,
  stepSize = 0.1,
  lossType = NULL,
  seed = NULL,
  subsamplingRate = 1,
  minInstancesPerNode = 1,
  minInfoGain = 0,
  checkpointInterval = 10,
  maxMemoryInMB = 256,
  cacheNodeIds = FALSE,
  handleInvalid = c("error", "keep", "skip")
)

## S4 method for signature 'GBTRegressionModel'
summary(object)

## S3 method for class 'summary.GBTRegressionModel'
print(x, ...)

## S4 method for signature 'GBTClassificationModel'
summary(object)

## S3 method for class 'summary.GBTClassificationModel'
print(x, ...)

## S4 method for signature 'GBTRegressionModel'
predict(object, newData)

## S4 method for signature 'GBTClassificationModel'
predict(object, newData)

## S4 method for signature 'GBTRegressionModel,character'
write.ml(object, path, overwrite = FALSE)

## S4 method for signature 'GBTClassificationModel,character'
write.ml(object, path, overwrite = FALSE)

參數:

  • data 用於訓練的 SparkDataFrame。
  • formula 要擬合的模型的符號說明。目前僅支持少數公式運算符,包括'~'、':'、'+'、'-'、'*'和'^'。
  • ... 傳遞給該方法的附加參數。
  • type 模型類型,"regression" 或 "classification" 之一,以適合
  • maxDepth 樹的最大深度 (>= 0)。
  • maxBins 用於離散連續特征和選擇如何在每個節點上分割特征的最大 bin 數。更多的 bin 提供更高的粒度。必須 >= 2 且 >= 任何類別特征中的類別數。
  • maxIter 最大迭代次數的參數 (>= 0)。
  • stepSize 用於每次優化迭代的步長參數。
  • lossType GBT 試圖最小化的損失函數。對於分類,必須是"logistic"。對於回歸,必須是"squared" (L2) 和"absolute" (L1) 之一,默認為"squared"。
  • seed 用於隨機數生成的整數種子。
  • subsamplingRate 用於學習每個決策樹的訓練數據的分數,在 (0, 1] 範圍內。
  • minInstancesPerNode 拆分後每個孩子必須擁有的最小實例數。如果拆分導致左或右子節點的數量少於 minInstancesPerNode,則拆分將被視為無效而丟棄。應該 >= 1。
  • minInfoGain 在樹節點處考慮的拆分的最小信息增益。
  • checkpointInterval 設置檢查點間隔 (>= 1) 或禁用檢查點 (-1) 的參數。注意:如果未設置檢查點目錄,此設置將被忽略。
  • maxMemoryInMB 分配給直方圖聚合的最大內存(以 MiB 為單位)。
  • cacheNodeIds 如果為 FALSE,算法會將樹傳遞給執行器以將實例與節點匹配。如果為 TRUE,算法將為每個實例緩存節點 ID。緩存可以加快對更深層次樹的訓練。用戶可以通過設置 checkpointInterval 來設置緩存檢查點的頻率或禁用它。
  • handleInvalid 如何處理分類模型中字符串類型的特征和標簽列中的無效數據(看不見的標簽或NULL值)。支持的選項:"skip"(過濾掉包含無效數據的行)、"error"(拋出錯誤)、"keep"(將無效數據放入特殊的附加存儲桶中,索引為 numLabels)。默認為"error"。
  • object 擬合梯度增強樹回歸模型或分類模型。
  • x summary 返回的梯度提升樹回歸模型或分類模型的摘要對象。
  • newData 用於測試的 SparkDataFrame。
  • path 保存模型的目錄。
  • overwrite 如果輸出路徑已經存在,是否覆蓋。默認為 FALSE,這意味著如果輸出路徑存在則拋出異常。

返回:

spark.gbt 返回擬合的梯度提升樹模型。

summary 返回擬合模型的匯總信息,是一個列表。組件列表包括formula(公式)、numFeatures(特征數量)、features(特征列表)、featureImportances(特征重要性)、maxDepth(樹的最大深度)、numTrees(樹的數量)和treeWeights(樹的權重)。

predict 返回一個 SparkDataFrame,其中包含在名為 "prediction" 的列中標記的預測值。

注意:

spark.gbt 自 2.1.0

摘要(GBTRegressionModel)自 2.1.0 起

從 2.1.0 開始的 print.summary.GBTRegressionModel

摘要(GBTClassificationModel)自 2.1.0 起

從 2.1.0 開始的 print.summary.GBTClassificationModel

從 2.1.0 開始預測(GBTRegressionModel)

從 2.1.0 開始預測(GBTClassificationModel)

write.ml(GBTRegressionModel, character) 自 2.1.0 起

write.ml(GBTClassificationModel, character) 自 2.1.0 起

例子:

# fit a Gradient Boosted Tree Regression Model
df <- createDataFrame(longley)
model <- spark.gbt(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 Gradient Boosted Tree Classification Model
# label must be binary - Only binary classification is supported for GBT.
t <- as.data.frame(Titanic)
df <- createDataFrame(t)
model <- spark.gbt(df, Survived ~ Age + Freq, "classification")

# numeric label is also supported
t2 <- as.data.frame(Titanic)
t2$NumericGender <- ifelse(t2$Sex == "Male", 0, 1)
df <- createDataFrame(t2)
model <- spark.gbt(df, NumericGender ~ ., type = "classification")

相關用法


注:本文由純淨天空篩選整理自spark.apache.org大神的英文原創作品 Gradient Boosted Tree Model for Regression and Classification。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。