首先,我是 XGBoost 的新手。所以请原谅我的愚蠢。
问题是:
如何将 dgCMatrix 分成两个矩阵(例如,训练和测试)?我的目标是使用这些矩阵进行 XGBoost 训练。当我使用 one-hot 编码将所有分类变量转换为数值变量时,我得到了 dgCMatrix。可以对训练数据集和测试数据集分别进行one-hot编码吗?
我已尝试使用 dummyVars(来自包插入符)进行 one-hot 编码,但我的 R session 由于某种我不知道的原因而中止。
最佳答案
在此处添加 DexGroves 的评论作为答案,因为它回答了问题。
Even if you split your dataset into two (say, A and B), the information about all levels of a factor will be stored in both A and B even if some of the levels are not present in either A or B. So when you do one hot encoding on a subset, it encodes all the levels irrespective of whether the levels are present in the subset or not. And it uses the same encoding on the next subset.
关于R:将 dgCMatrix 拆分为训练矩阵和测试矩阵,用于 XGBoost 训练,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39330087/