gpt4 book ai didi

r - R 中的级别 - 针对新数据集正确设置

转载 作者:行者123 更新时间:2023-12-02 04:56:09 24 4
gpt4 key购买 nike

我在 R 中使用 randomForest。

我训练一组包含因子变量的数据。该变量具有以下级别:

[1] "Economics"    "Engineering"   "Medicine"
[4] "Accounting" "Biology" "Computer Science"
[7] "Physics" "Law" "Chemistry"

我的评估集包含这些级别的子集:

[1] "Law"          "Medicine"

randomForest 包要求级别相同,所以我试过:

levels(evaluationSet$course) <- levels(trainingSet$course)

但是当我检查评估集中的行时,值发生了变化:

evaluationSet[1:3,c('course')]
# Gives "[1] Economics Engineering Economics", should give "[1] Law Medicine Law"

我是 R 的新手,但我认为这里发生的事情是因子是一个枚举集。在评估集中,“法律”和“医学”在因子中以数字表示(分别为 1 和 2)。当我应用新级别时,它会更改这些索引映射到的值。

我在 SO 上找到了一些类似的主题并尝试了他们的建议,但没有成功:

evaluationSet <- droplevels(evaluationSet)
levels(evaluationSet$course) <- levels(trainingSet$course)
evaluationSet$course <- factor(evaluationSet$course)

如何在保留数据值的同时将级别设置为与训练集相同?

编辑:在 levels(evaluationSet$course) <- levels(trainingSet$course) 之前和之后添加 head(evaluationSet) 结果:

   timestamp score age takenBefore   course
1 1374910975 0.87 18 0 law
2 1374910975 0.81 21 0 medicine
3 1374910975 0.88 21 0 law
4 1374910975 0.88 21 0 law
5 1374910975 0.74 22 0 law
6 1374910975 0.76 23 1 medicine

timestamp score age takenBefore course
1 1374910975 0.87 18 0 economics
2 1374910975 0.81 21 0 engineering
3 1374910975 0.88 21 0 economics
4 1374910975 0.88 21 0 economics
5 1374910975 0.74 22 0 economics
6 1374910975 0.76 23 1 engineering

最佳答案

你的直觉基本上是正确的。问题的症结在于级别的顺序很重要。它们不是一个集合,更像是一个映射。

这是一个例子:

f <- factor(sample(letters[4:6],20,replace = TRUE))
> f
[1] d e e d e e f d d f e e d d e e f e d d
Levels: d e f
> levels(f)
[1] "d" "e" "f"
> levels(f) <- letters[1:6]
> f
[1] a b b a b b c a a c b b a a b b c b a a
Levels: a b c d e f

请注意,当我们添加级别时,“前”三个级别已被替换。相反,

> f <- factor(sample(letters[4:6],20,replace = TRUE))
> f
[1] d f f e e d d f d d f d d e e e e f d e
Levels: d e f
> levels(f) <- c(letters[4:6],letters[1:3])
> f
[1] d f f e e d d f d d f d d e e e e f d e
Levels: d e f a b c

因此,您只需要尊重评估集中当前的级别顺序即可。

考虑这一点的一种方法是,因子实际上只是一个整数向量。凡是 R 编码为 1 的地方,都将对应于第一 级别。由于它会按字母顺序对它们进行排序,因此当您添加级别时,您可能会弄乱该映射。

关于r - R 中的级别 - 针对新数据集正确设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17936553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com