R:将 dgCMatrix 拆分为训练和测试矩阵,用于 XGBoost 训练
R: Splitting dgCMatrix into train and test matrices, to use for XGBoost training
首先,我是 XGBoost 的新手。所以请原谅我的愚蠢。
这里是问题:
如何将 dgCMatrix 拆分为两个矩阵(例如,训练和测试)?我的目标是使用这些矩阵进行 XGBoost 训练。当我使用单热编码将所有分类变量转换为数值变量时,我得到了 dgCMatrix。我可以在训练数据集和测试数据集上分别进行one-hot编码吗?
我已经尝试使用 dummyVars(来自包 caret)进行单热编码,但我的 R 会话由于某种我不知道的原因而中止。
在这里添加 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.
首先,我是 XGBoost 的新手。所以请原谅我的愚蠢。
这里是问题:
如何将 dgCMatrix 拆分为两个矩阵(例如,训练和测试)?我的目标是使用这些矩阵进行 XGBoost 训练。当我使用单热编码将所有分类变量转换为数值变量时,我得到了 dgCMatrix。我可以在训练数据集和测试数据集上分别进行one-hot编码吗?
我已经尝试使用 dummyVars(来自包 caret)进行单热编码,但我的 R 会话由于某种我不知道的原因而中止。
在这里添加 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.