我有以下功能:
brt <- gbm.step(data = train,
gbm.x = 5:9, # column indices for covariates
gbm.y = 4, # column index for response (presence-absence)
family = "bernoulli",
tree.complexity = ifelse(prNum < 50, 1, 5),
learning.rate = 0.001,
bag.fraction = 0.75,
max.trees = ifelse(s == "Jacaranda.puberula" | s == "Cestrum.intermedium", 50, 10000),
n.trees = 50,
n.folds = 5, # 5-fold cross-validation
silent = TRUE) # avoid printing the cv results
我正在嘗試運行上述函式,但他們使用ifelse
內部max.trees
引數的方式回傳錯誤。這是正確的編碼方式嗎?我想要完成的只是max.trees
對這兩個物種使用 50,而不是 10000:Jacaranda.puberula或Cestrum.intermedium。提醒一下,該物件s
來自另一個名為 的向量sp
,它是一個物種名稱串列。每個s
都是我在這個函式中回圈的不同物種。
問題出在我撰寫陳述句的方式上,還是我使用 OR 作為邏輯的事實?
編輯:我認為這可能與功能有關。我在這里重新運行了分析,它回傳:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "NULL"
In addition: There were 50 or more warnings (use warnings() to see the first 50)
uj5u.com熱心網友回復:
廣義gbm
提升max.trees
回歸樹n.trees
(max_depth
n.trees = ifelse(s == "Jacaranda.puberula" | s == "Cestrum.intermedium", 50, 10000)
編輯:
如果無法訪問您的資料,就很難準確猜測出了什么問題。上次我假設你使用的包是gbm
. 但是,使用示例資料集(BostonHousing
來自 package mlbench
),下面的示例顯示了the和packages 的gbm
實作如何按預期作業:gbm
dismo
library(mlbench)
data(BostonHousing)
head(BostonHousing)
# crim zn indus chas nox rm age dis rad tax ptratio b lstat medv
#1 0.00632 18 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98 24.0
#2 0.02731 0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14 21.6
#3 0.02729 0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03 34.7
#4 0.03237 0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94 33.4
#5 0.06905 0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7 396.90 5.33 36.2
#6 0.02985 0 2.18 0 0.458 6.430 58.7 6.0622 3 222 18.7 394.12 5.21 28.7
library(gbm)
set.seed(102) # for reproducibility
# predict target medv given other predictors
gbm1 <- gbm(medv ~ ., data = BostonHousing, #var.monotone = c(0, 0, 0, 0, 0, 0),
distribution = "gaussian", shrinkage = 0.1,
n.trees = 100,
interaction.depth = 3, bag.fraction = 0.5, train.fraction = 0.5,
n.minobsinnode = 10, cv.folds = 5, keep.data = TRUE,
verbose = FALSE, n.cores = 1)
#predict(gbm1)
library(dismo)
# predict target medv given other predictors
gbm2 <- gbm.step(data=BostonHousing, gbm.x = 1:13, gbm.y = 14, family = "gaussian",
max.trees = 10000,
tree.complexity = 5, learning.rate = 0.01, bag.fraction = 0.5)
#predict(gbm2)
# compare predictions
library(tidyverse)
data.frame(index=1:nrow(BostonHousing),
actual=BostonHousing$medv,
pred.gbm1=predict(gbm1),
pred.gbm2=predict(gbm2)) %>%
gather('pred', 'medv', -index) %>%
ggplot(aes(index, medv, group=pred, color=pred)) geom_line()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/508267.html
上一篇:自動編碼器中用于硬碟驅動器影像的Tensorflow資料集問題
下一篇:向支持向量分類器訓練添加更多資料