R中所有列的排列组合
Permutations and combinations of all the columns in R
我想在 R 中选择模型时检查列的所有排列和组合。我的数据集中有 8 列,下面的代码可以让我检查一些模型,但不是全部。像 column 1+6, 1+2+5 这样的模型不会被这个循环覆盖。有没有更好的方法来完成这个?
best_model <- rep(0,3) #store the best model in this array
for(i in 1:8){
for(j in 1:8){
for(x in k){
diabetes_prediction <- knn(train = diabetes_training[, i:j], test = diabetes_test[, i:j], cl = diabetes_train_labels, k = x)
accuracy[x] <- 100 * sum(diabetes_test_labels == diabetes_prediction)/183
if( best_model[1] < accuracy[x] ){
best_model[1] = accuracy[x]
best_model[2] = i
best_model[3] = j
}
}
}
}
已解决,下面是带有注释的代码:
# find out the best model for this data
number_of_columns_to_model <- ncol(diabetes_training)-1
best_model <- c()
best_model_accuracy = 0
for(i in 2:2^number_of_columns_to_model-1){
# ignoring the first case i.e. i=1, as it doesn't represent any model
# convert the value of i to binary, e.g. i=5 will give combination = 0 0 0 0 0 1 0 1
combination = as.binary(i, n=number_of_columns_to_model) # from the binaryLogic package
model <- c()
for(i in 1:length(combination)){
# choose which columns to consider depending on the combination
if(combination[i])
model <- c(model, i)
}
for(x in k){
# for the columns decides by model, find out the accuracies of model for k=1:27
diabetes_prediction <- knn(train = diabetes_training[, model, with = FALSE], test = diabetes_test[, model, with = FALSE], cl = diabetes_train_labels, k = x)
accuracy[x] <- 100 * sum(diabetes_test_labels == diabetes_prediction)/length(diabetes_test_labels)
if( best_model_accuracy < accuracy[x] ){
best_model_accuracy = accuracy[x]
best_model = model
print(model)
}
}
}
好吧,这个答案并不完整,但也许可以帮助您入门。您希望能够按所有可能的列子集进行子集化。因此,您希望能够通过 c(1,6) 或 c(1,2,5) 等对 i:j 进行子集化,而不是对某些 i 和 j 使用 i:j。
使用sets包,你可以得到一个集合的幂集(所有子集的集合)。这是最简单的部分。我是 R 的新手,所以对我来说最困难的部分是理解集合、列表、向量等之间的区别。我习惯了 Mathematica,它们都是一样的。
library(sets)
my.set <- 1:8 # you want column indices from 1 to 8
my.power.set <- set_power(my.set) # this creates the set of all subsets of those indices
my.names <- c("a") #I don't know how to index into sets, so I created names (that are numbers, but of type characters)
for(i in 1:length(my.power.set)) {my.names[i] <- as.character(i)}
names(my.power.set) <- my.names
my.indices <- vector("list",length(my.power.set)-1)
for(i in 2:length(my.power.set)) {my.indices[i-1] <- as.vector(my.power.set[[my.names[i]]])} #this is the line I couldn't get to work
我想创建一个名为 my.indices 的列表列表,因此 my.indices[i] 是 {1,2,3,4,5,6,7,8 } 可以用来代替 i:j。然后,您的 for 循环必须 运行 from 1:length(my.indices).
但是,唉,我被 Mathematica 宠坏了,因此无法破译 R 数据类型极其复杂的世界。
我用 Pima.tr 训练并用 Pima.te 测试。预处理预测变量的 KNN 准确度在没有预处理的情况下为 78% 和 80%(这是因为某些变量的影响很大)。
80% 的性能与逻辑回归模型相当。您不需要在逻辑回归中预处理变量。
RandomForest 和 Logistic Regression 提供了关于要删除哪些变量的提示,因此您无需执行所有可能的组合。
另一种方法是查看矩阵散点图
当谈到 npreg、glu、bmi、年龄时,您会感觉到类型 0 和类型 1 之间存在差异
您还注意到高度偏斜的 ped 和年龄,并且您注意到皮肤和其他变量之间可能存在异常数据点(您可能需要在进一步操作之前删除该观察结果)
皮肤与类型箱形图显示,对于类型 Yes,存在极端异常值(尝试将其删除)
您还注意到大多数 Yes 类型的框都高于 No 类型 => 变量可能会向模型添加预测(您可以通过 Wilcoxon 秩和检验确认这一点)
皮肤和 bmi 之间的高度相关性意味着您可以使用其中之一或两者的相互作用。
另一种减少预测变量数量的方法是使用 PCA
我想在 R 中选择模型时检查列的所有排列和组合。我的数据集中有 8 列,下面的代码可以让我检查一些模型,但不是全部。像 column 1+6, 1+2+5 这样的模型不会被这个循环覆盖。有没有更好的方法来完成这个?
best_model <- rep(0,3) #store the best model in this array
for(i in 1:8){
for(j in 1:8){
for(x in k){
diabetes_prediction <- knn(train = diabetes_training[, i:j], test = diabetes_test[, i:j], cl = diabetes_train_labels, k = x)
accuracy[x] <- 100 * sum(diabetes_test_labels == diabetes_prediction)/183
if( best_model[1] < accuracy[x] ){
best_model[1] = accuracy[x]
best_model[2] = i
best_model[3] = j
}
}
}
}
已解决,下面是带有注释的代码:
# find out the best model for this data
number_of_columns_to_model <- ncol(diabetes_training)-1
best_model <- c()
best_model_accuracy = 0
for(i in 2:2^number_of_columns_to_model-1){
# ignoring the first case i.e. i=1, as it doesn't represent any model
# convert the value of i to binary, e.g. i=5 will give combination = 0 0 0 0 0 1 0 1
combination = as.binary(i, n=number_of_columns_to_model) # from the binaryLogic package
model <- c()
for(i in 1:length(combination)){
# choose which columns to consider depending on the combination
if(combination[i])
model <- c(model, i)
}
for(x in k){
# for the columns decides by model, find out the accuracies of model for k=1:27
diabetes_prediction <- knn(train = diabetes_training[, model, with = FALSE], test = diabetes_test[, model, with = FALSE], cl = diabetes_train_labels, k = x)
accuracy[x] <- 100 * sum(diabetes_test_labels == diabetes_prediction)/length(diabetes_test_labels)
if( best_model_accuracy < accuracy[x] ){
best_model_accuracy = accuracy[x]
best_model = model
print(model)
}
}
}
好吧,这个答案并不完整,但也许可以帮助您入门。您希望能够按所有可能的列子集进行子集化。因此,您希望能够通过 c(1,6) 或 c(1,2,5) 等对 i:j 进行子集化,而不是对某些 i 和 j 使用 i:j。
使用sets包,你可以得到一个集合的幂集(所有子集的集合)。这是最简单的部分。我是 R 的新手,所以对我来说最困难的部分是理解集合、列表、向量等之间的区别。我习惯了 Mathematica,它们都是一样的。
library(sets)
my.set <- 1:8 # you want column indices from 1 to 8
my.power.set <- set_power(my.set) # this creates the set of all subsets of those indices
my.names <- c("a") #I don't know how to index into sets, so I created names (that are numbers, but of type characters)
for(i in 1:length(my.power.set)) {my.names[i] <- as.character(i)}
names(my.power.set) <- my.names
my.indices <- vector("list",length(my.power.set)-1)
for(i in 2:length(my.power.set)) {my.indices[i-1] <- as.vector(my.power.set[[my.names[i]]])} #this is the line I couldn't get to work
我想创建一个名为 my.indices 的列表列表,因此 my.indices[i] 是 {1,2,3,4,5,6,7,8 } 可以用来代替 i:j。然后,您的 for 循环必须 运行 from 1:length(my.indices).
但是,唉,我被 Mathematica 宠坏了,因此无法破译 R 数据类型极其复杂的世界。
我用 Pima.tr 训练并用 Pima.te 测试。预处理预测变量的 KNN 准确度在没有预处理的情况下为 78% 和 80%(这是因为某些变量的影响很大)。
80% 的性能与逻辑回归模型相当。您不需要在逻辑回归中预处理变量。
RandomForest 和 Logistic Regression 提供了关于要删除哪些变量的提示,因此您无需执行所有可能的组合。
另一种方法是查看矩阵散点图
当谈到 npreg、glu、bmi、年龄时,您会感觉到类型 0 和类型 1 之间存在差异
您还注意到高度偏斜的 ped 和年龄,并且您注意到皮肤和其他变量之间可能存在异常数据点(您可能需要在进一步操作之前删除该观察结果)
皮肤与类型箱形图显示,对于类型 Yes,存在极端异常值(尝试将其删除)
您还注意到大多数 Yes 类型的框都高于 No 类型 => 变量可能会向模型添加预测(您可以通过 Wilcoxon 秩和检验确认这一点)
皮肤和 bmi 之间的高度相关性意味着您可以使用其中之一或两者的相互作用。
另一种减少预测变量数量的方法是使用 PCA