r - 为插入符包中的多个列创建DataPartition

标签 r machine-learning cross-validation r-caret knn

我正在尝试使用 caret 包来运行 KNN 算法来找到最佳 k 值。我的数据看起来 like this (代码上的 datanet),"ACTIVITY_X""ACTIVITY_Y""ACTIVITY_Z" 是我的预测变量我想根据 B 列到 D 列的值对“事件” 进行分类。所有列都具有相同的行数。

为此,我首先需要拆分数据以进行交叉验证。这是我的代码示例:

# Split the data:

indxTrain <- createDataPartition(y = datanet$ACTIVITY_X,p = 0.8,list = FALSE)
training <- datanet[indxTrain,]
testing <- datanet[-indxTrain,]

# Run k-NN:
set.seed(400)
ctrl <- trainControl(method="repeatedcv",repeats = 3)
knnFit <- train(Event ~ ., data = training, method = "knn", trControl = ctrl, preProcess = c("center","scale"),tuneLength = 20)
knnFit

#Use plots to see optimal number of clusters:
#Plotting yields Number of Neighbours Vs accuracy (based on repeated cross validation)
plot(knnFit)

我的问题有两个:

1) 如果我在使用 caret 包时理解正确,则 createDataPartition 中的 y 参数需要是预测变量,对吗?

2) 如果是这样,我有前面提到的三个预测变量("ACTIVITY_X""ACTIVITY_Y""ACTIVITY_Z"),但如果我运行 createDataPartition(y = datanet$(ACTIVITY_X, ACTIVITY_Y, ACTIVITY_Z)p = 0.8,list = FALSE) 我会收到一条错误消息。

关于当预测变量数据包含多列时如何使用 createDataPartition 进行交叉验证有什么想法吗?

仅使用一个预测器(假设“ACTIVITY_X”)运行createDataPartition是否可以,因为分区随后将应用于其余列?

感谢任何帮助!

最佳答案

您需要根据目标变量而不是预测变量来分割数据。即:

indxTrain <- createDataPartition(y = datanet$Event,p = 0.8,list = FALSE)

以下是 ?createDataPartition 的原因:

y
a vector of outcomes. For createTimeSlices, these should be in chronological order.

关于r - 为插入符包中的多个列创建DataPartition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54477978/

相关文章:

R - 在数据框中查找所有序列及其频率

python - torch : "Model Weights not Changing"

python - Sklearn StratifiedKFold : ValueError: Supported target types are: ('binary' , 'multiclass' )。取而代之的是 'multilabel-indicator'

json - 如何在 R 中使用 Plumber 使用一组 JSON 对象

r - 将向量转换为列表,向量中的每个元素作为列表中的元素

r - R Catboost处理分类变量

r - R 中的文本分析 : How to add variables to my machine learning classifier in addition to the tokens?

Matlab交叉验证和K-NN

python-3.x - sklearn 中的交叉验证 : do I need to call fit() as well as cross_val_score()?

r - 如何在 R 中读取回溯