【问题标题】:H2O R How to handle levels not trained on when predicting?H2O R 如何处理预测时未训练的级别?
【发布时间】:2018-06-21 02:31:51
【问题描述】:

我有一组带有结果列的 CSV 要训练,还有一组没有结果列的测试 CSV。

library(h2o)
h2o.init()

train <- read.csv(train_file, header=T)
train.h2o <- as.h2o(train)
y <- "Result"
x <- setdiff(names(train.h2o), y)

model <- h2o.deeplearning(x = x,
                          y = y,
                          training_frame = train.h2o,
                          model_id = "my_model",
                          epochs = 5000,
                          hidden = c(50),
                          stopping_rounds=5,
                          stopping_metric="misclassification", 
                          stopping_tolerance=0.001,
                          seed = 1)



test <- read.csv(test_file, header=T)
test.h2o <- as.h2o(test)

pred <- h2o.predict(model,test.h2o)

当我尝试使用测试数据预测结果时,我得到了一堆错误,例如:

1: In doTryCatch(return(expr), name, parentenv, handler) :
Test/Validation dataset column 'ColumnName' has levels not trained on: [ABCD, BCDE]

H2O 过去能够处理测试中存在的数据,但不能处理训练期间的数据。我在网上找到了一些他们说的帖子。但这对我不起作用。

如何避免这些错误,并预测测试数据的值?

【问题讨论】:

  • 您能否通过使用公开可用的数据集将其转化为可重现的示例? H2O 应该忽略新的关卡,所以我不知道这里发生了什么(我之前没有看到过这个错误),如果它是一个错误,我们希望能够重现以便修复它。谢谢。

标签: r machine-learning classification h2o


【解决方案1】:

有两种方法你可以试试:

使用factorcharacter 相反

在将数据输入机器学习函数之前,您可以结合训练和测试数据,并将character 变量转换为factor

因此,即使您稍后拆分组合数据,唯一值也会被记录为级别信息。

library(h2o)

h2o.init()

#using dummy data as combined training and testing data
prostatePath = system.file("extdata", "prostate.csv", package = "h2o")
prostate.hex = h2o.importFile(path = prostatePath, destination_frame = "prostate.hex")

#assuming GLEASON is the character variable, and transform it to factor
prostate.hex$GLEASON <- h2o.asfactor(prostate.hex$GLEASON)

#split data such that 0,4,5,8 only in test set, and not in train set.
h2o.test <- prostate.hex[prostate.hex$GLEASON %in% c("0","4","5","8"),]
h2o.train <- prostate.hex[!prostate.hex$GLEASON %in% c("0","4","5","8"),]

#train model
model <- h2o.glm(y = "CAPSULE", x = c("AGE","RACE","PSA","DCAPS","GLEASON"), training_frame = h2o.train,
       family = "binomial", nfolds = 0)

#predict without error
pred <- predict(model,h2o.test)

明确使用one-hot-encoding

我知道h2o 机器学习函数提供了内部编码方法(通过categorical_encoding 参数),包括one-hot-encoding,它将字符变量转换为大量1/0 整数变量。

与隐式使用此技术相反,您可以显式使用它。因此,训练中不存在的那些级别将不会在模型中使用。测试中的新级别根本不用于预测。

【讨论】:

  • 可以使用它们,例如通过特征聚类,但这更像是一个统计/机器学习问题而不是编程问题。
  • 这是个好主意,它将个人级别信息隐藏在新变量后面。我们只需要注意一些/几个集群只存在于测试集中。
  • 理想情况下,您应该在训练集上使用某种聚类算法,并能够将相同的标准应用于测试集,以将变量/变量组映射到这些聚类。
  • 您可以根据一个常见的“特征的特征”进行聚类,即特定类/级别出现的频率,以及与之同时出现的某些数值变量的平均值,或者其他特征缺失变量的发生率。如果类别类似于 URL,则可能是字符串长度,但您也可以只使用字符串长度创建一个新变量。
  • @user7792598,将train和test放在一起的目的是将字符变量转换为因子,因此涉及到train和test中的所有级别。可以为结果列分配虚拟值(例如 0)。测试数据不会用于训练,它只是在训练数据中有用的级别,包括所有级别的因素(可能用于测试)。
猜你喜欢
  • 2017-08-20
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
相关资源
最近更新 更多