【问题标题】:R: Check Variables in Training DataR:检查训练数据中的变量
【发布时间】:2018-06-28 18:05:43
【问题描述】:

我正在使用在 RData 文件中提供给我的训练数据,以及我自己构建的数据框,其中包含我认为训练数据中包含的所有列。

args = commandArgs(trailingOnly=TRUE)

model = readRDS(args[1])
m = model[[1]]

infile = fread(newDataPath, header=T)
setDF(infile)
i = infile[,!colnames(infile) %in% c("chr", "pos", "end")]

predictions = predict(m, i)

不过,运行这个,我明白了 variables in the training data missing in newdata

使用colnames(i),我可以在newdata 中找到变量列表,但我怎样才能对训练数据做同样的事情——我认为这是randomForest 类的对象?

【问题讨论】:

  • 你现在在哪里?您是否已经运行过随机森林?如果你有一个randomForest 对象,那么我认为 somone 已经运行了它。无论如何,运行class(newdata)
  • @TimBiegeleisen 我自己没有运行随机森林——只是从一个名为 modelsList.3.RData 的文件中加载。还有class(m) --> randomForest, class(i) --> data.frame。感谢您的帮助。
  • 并不总是很容易看到用于训练模型的数据。取决于模型、他们使用的函数/包、交叉验证的类型等。如果幸运的话,您可以调用模型并查看可能具有变量名称的公式,或者它可能具有类似 randomForest(formula = Y~ ., data = ...) 的内容这对你没有用。
  • 除非您的 RData 文件中有训练数据。如果没有,也许您可​​以要求他们添加它。如果它是一个大数据集,他们可以添加一个小样本,或者只是列名。

标签: r machine-learning artificial-intelligence random-forest training-data


【解决方案1】:

您可以使用str查看模型的结构以查找列名的位置。

我假设您使用的是 randomForest 包,但对于其他模型来说也是一样的想法。

library('randomForest')

model <- randomForest(Species ~ ., data = iris, ntree=5)

str(model)
#> List of 19
#>  $ call           : language randomForest(formula = Species ~ ., data = iris, ntree = 5)
#>  $ type           : chr "classification"
#>  $ predicted      : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#>   ..- attr(*, "names")= chr [1:150] "1" "2" "3" "4" ...
#>  $ err.rate       : num [1:5, 1:4] 0.0862 0.0753 0.114 0.0714 0.0833 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : NULL
#>   .. ..$ : chr [1:4] "OOB" "setosa" "versicolor" "virginica"
#>  $ confusion      : num [1:3, 1:4] 45 0 0 0 41 8 0 3 35 0 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:3] "setosa" "versicolor" "virginica"
#>   .. ..$ : chr [1:4] "setosa" "versicolor" "virginica" "class.error"
#>  $ votes          : matrix [1:150, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:150] "1" "2" "3" "4" ...
#>   .. ..$ : chr [1:3] "setosa" "versicolor" "virginica"
#>  $ oob.times      : num [1:150] 1 2 1 1 3 1 2 2 2 2 ...
#>  $ classes        : chr [1:3] "setosa" "versicolor" "virginica"
#>  $ importance     : num [1:4, 1] 20.53 4.33 19.17 55.25
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
#>   .. ..$ : chr "MeanDecreaseGini"
#>  $ importanceSD   : NULL
#>  $ localImportance: NULL
#>  $ proximity      : NULL
#>  $ ntree          : num 5
#>  $ mtry           : num 2
#>  $ forest         :List of 14
#>   ..$ ndbigtree : int [1:5] 9 17 35 11 19
#>   ..$ nodestatus: int [1:35, 1:5] 1 1 -1 -1 1 1 -1 -1 -1 0 ...
#>   ..$ bestvar   : int [1:35, 1:5] 4 4 0 0 2 3 0 0 0 0 ...
#>   ..$ treemap   : int [1:35, 1:2, 1:5] 2 4 0 0 6 8 0 0 0 0 ...
#>   ..$ nodepred  : int [1:35, 1:5] 0 0 3 1 0 0 2 2 3 0 ...
#>   ..$ xbestsplit: num [1:35, 1:5] 1.65 0.8 0 0 2.25 4.75 0 0 0 0 ...
#>   ..$ pid       : num [1:3] 1 1 1
#>   ..$ cutoff    : num [1:3] 0.333 0.333 0.333
#>   ..$ ncat      : Named int [1:4] 1 1 1 1
#>   .. ..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
#>   ..$ maxcat    : int 1
#>   ..$ nrnodes   : int 35
#>   ..$ ntree     : num 5
#>   ..$ nclass    : int 3
#>   ..$ xlevels   :List of 4
#>   .. ..$ Sepal.Length: num 0
#>   .. ..$ Sepal.Width : num 0
#>   .. ..$ Petal.Length: num 0
#>   .. ..$ Petal.Width : num 0
#>  $ y              : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#>   ..- attr(*, "names")= chr [1:150] "1" "2" "3" "4" ...
#>  $ test           : NULL
#>  $ inbag          : NULL
#>  $ terms          :Classes 'terms', 'formula'  language Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
#>   .. ..- attr(*, "variables")= language list(Species, Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
#>   .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
#>   .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. ..$ : chr [1:5] "Species" "Sepal.Length" "Sepal.Width" "Petal.Length" ...
#>   .. .. .. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
#>   .. ..- attr(*, "term.labels")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
#>   .. ..- attr(*, "order")= int [1:4] 1 1 1 1
#>   .. ..- attr(*, "intercept")= num 0
#>   .. ..- attr(*, "response")= int 1
#>   .. ..- attr(*, ".Environment")=<environment: 0x7f9bed91f8d8> 
#>   .. ..- attr(*, "predvars")= language list(Species, Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
#>   .. ..- attr(*, "dataClasses")= Named chr [1:5] "factor" "numeric" "numeric" "numeric" ...
#>   .. .. ..- attr(*, "names")= chr [1:5] "Species" "Sepal.Length" "Sepal.Width" "Petal.Length" ...
#>  - attr(*, "class")= chr [1:2] "randomForest.formula" "randomForest"

attr(model$terms, 'term.labels')
#> [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"

attr(model$terms, 'dataClasses')
#>      Species Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>     "factor"    "numeric"    "numeric"    "numeric"    "numeric"

【讨论】:

  • 我似乎没有terms,但它们都列在xlevels 下!
【解决方案2】:

一般来说,您可以检查intersect(names(train), names(test))setdiff(names(train), names(test)) 来查找缺失的变量。

【讨论】:

  • intersect(...) --> character(0)setdiff(...) 似乎没有比较模型和新数据中的实际变量。 names(m) 提供 "importance" "importanceSD" "localImportance" "proximity" 之类的项目。
猜你喜欢
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 2013-08-17
相关资源
最近更新 更多