【发布时间】:2019-05-29 07:48:06
【问题描述】:
我有一个看起来像这样的数据集
ID 885038 885039 885040 885041 885042 885043 885044 Class
1267359 2 0 0 0 0 1 0 0
1295720 0 0 0 0 0 1 0 0
1295721 0 0 0 0 0 1 0 0
1295723 0 0 0 0 0 1 0 0
1295724 0 0 0 1 0 1 0 0
1295725 0 0 0 1 0 1 0 0
1295726 2 0 0 0 0 1 0 1
1295727 2 0 0 0 0 1 0 1
1295740 0 0 0 0 0 1 0 1
1295742 0 0 0 0 0 1 0 1
1295744 0 0 0 0 0 1 0 1
1295745 0 0 0 0 0 1 0 1
1295746 0 0 0 0 0 1 0 1
为了进行递归特征消除,我遵循了这些步骤
- 训练 SVM 分类器
- 计算所有特征的排名标准
- 移除排名值最小的特征
- 转到 1。
以下是我为执行相同操作而编写的 R 代码,但是,它没有显示任何错误,并且循环继续训练集的长度。
data <- read.csv("dummy - Copy.csv", header = TRUE)
rownames(data) <- data[,1]
data<-data[,-1]
for (k in 1:length(data)){
inTraining <- createDataPartition(data$Class, p = .70, list = FALSE)
training <- data[ inTraining,]
testing <- data[-inTraining,]
## Building the model ####
svm.model <- svm(Class ~ ., data = training, cross=10,metric="ROC",type="eps-regression",kernel="linear",na.action=na.omit,probability = TRUE)
###### auc measure #######
#prediction and ROC
svm.model$index
svm.pred <- predict(svm.model, testing, probability = TRUE)
#calculating auc
c <- as.numeric(svm.pred)
c = c - 1
pred <- prediction(c, testing$Class)
perf <- performance(pred,"tpr","fpr")
plot(perf,fpr.stop=0.1)
auc <- performance(pred, measure = "auc")
auc <- auc@y.values[[1]]
#compute the weight vector
w = t(svm.model$coefs)%*%svm.model$SV
#compute ranking criteria
weight_matrix = w * w
#rank the features
w_transpose <- t(weight_matrix)
w2 <- as.matrix(w_transpose[order(w_transpose[,1], decreasing = FALSE),])
a <- as.matrix(w2[which(w2 == min(w2)),]) #to get the rows with minimum values
row.names(a) -> remove
data<- data[,setdiff(colnames(data),remove)]
print(length(data))
length <- (length(data))
cols_names <- colnames(data)
print(auc)
output <- paste(length,auc,sep=";")
write(output, file = "output.txt",append = TRUE)
write(cols_names, file = paste(length,"cols_selected", ".txt", sep=""))
}
打印出来的样子
[1] 3
[1] 0.5
[1] 2
[1] 0.5
[1] 2
[1] 0.5
[1] 2
[1] 0.75
[1] 2
[1] 1
[1] 2
[1] 0.75
[1] 2
[1] 0.5
[1] 2
[1] 0.75
但是当我选择任何特征子集时,例如功能 3 并使用上面的代码(没有循环)构建 SVM 模型,我没有得到相同的 AUC 值 0.75。
data <- read.csv("3.csv", header = TRUE)
rownames(data) <- data[,1]
data<-data[,-1]
inTraining <- createDataPartition(data$Class, p = .70, list = FALSE)
training <- data[ inTraining,]
testing <- data[-inTraining,]
## Building the model ####
svm.model <- svm(Class ~ ., data = training, cross=10,metric="ROC",type="eps-regression",kernel="linear",na.action=na.omit,probability = TRUE)
###### auc measure #######
#prediction and ROC
svm.model$index
svm.pred <- predict(svm.model, testing, probability = TRUE)
#calculating auc
c <- as.numeric(svm.pred)
c = c - 1
pred <- prediction(c, testing$Class)
perf <- performance(pred,"tpr","fpr")
plot(perf,fpr.stop=0.1)
auc <- performance(pred, measure = "auc")
auc <- auc@y.values[[1]]
print(auc)
prints output
[1] 3
[1] 0.75 (instead of 0.5)
两个代码相同(一个带有递归循环,另一个没有任何递归循环)仍然存在相同特征子集的 AUC 值不同。
这两个代码的 3 个特征(885041、885043 和 Class)是相同的,但它给出了不同的 AUC 值。
【问题讨论】:
-
我不明白为什么这个问题被否决了。我还更新了我从头到尾尝试过的内容。我也做了一个快速搜索以确保它不重复......这个问题与编程有关......这就是为什么在 Stackoverflow 中问......
-
如果您能提供数据集的可重现示例,我相信这将是一篇好文章。
-
我喜欢你研究这个问题,因为很多很多问题都没有事先打扰。 @www 指的是您的图像 - 要重现您的错误,我们需要相同的数据(我们不必自己重新输入)来运行您的代码。此外,如果什么都没发生,通常提供比“它不起作用”更好 - 许多人实际上收到错误但只是说“它不起作用”。您对 www 的回答比再次告诉我它不起作用要好。
-
如果 SO 提供了一个自动图像阅读器,它可以获取表格位图图像并转换为测试,那么不会有问题,但是你暗示我们需要按顺序重新输入数据测试对您的编码的任何修改或改进。我们中的大多数人都没有足够的动力来重做您的数据输入。几天前有人(你?)发布类似标题的问题时,Rhelp 邮件列表的读者面临同样的问题。引用 csv 文件将无法重现,发布图像文件略有改进,但还不够好。
-
感谢您的建议,我已将其纳入帖子中。我希望现在代码是可重现的。
标签: r machine-learning svm feature-selection