我正在尝试通过一个示例来说明如何使用e1071软件包中的支持向量机功能对数据集进行分类。
我在几个数据集中遇到了相同的基本问题。我现在使用的是UCI机器学习存储库中的乳腺癌-威斯康星州。
代码如下:
> library(e1071)
> library(kernlab)
> library(readr)
> BC <- read_csv("BC.csv")
> BC <- read.table("breast-cancer-wisconsin.data", sep=",")
> colnames(BC) <- c("Sample Code Number", "Clump Thickness", "Uniformity of Cell Size", "Uniformity of Cell Shape", "Marginal Adhesion", "Single Epithelial Cell Size", "Bare Nuclei", "Bland Chromatin", "Normal Nucleoli", "Mitoses", "Class")
> str(BC)
'data.frame': 699 obs. of 11 variables:
$ Sample Code Number : int 1000025 1002945 1015425 1016277 1017023 1017122 1018099 1018561 1033078 1033078 ...
$ Clump Thickness : int 5 5 3 6 4 8 1 2 2 4 ...
$ Uniformity of Cell Size : int 1 4 1 8 1 10 1 1 1 2 ...
$ Uniformity of Cell Shape : int 1 4 1 8 1 10 1 2 1 1 ...
$ Marginal Adhesion : int 1 5 1 1 3 8 1 1 1 1 ...
$ Single Epithelial Cell Size: int 2 7 2 3 2 7 2 2 2 2 ...
$ Bare Nuclei : Factor w/ 11 levels "?","1","10","2",..: 2 3 4 6 2 3 3 2 2 2 ...
$ Bland Chromatin : int 3 3 3 3 3 9 3 3 1 2 ...
$ Normal Nucleoli : int 1 2 1 7 1 7 1 1 1 1 ...
$ Mitoses : int 1 1 1 1 1 1 1 1 5 1 ...
$ Class : int 2 2 2 2 2 4 2 2 2 2 ...
> BC$Class <- as.factor(BC$Class)
> plot(BC$`Uniformity of Cell Size`, BC$`Marginal Adhesion`, pch=as.numeric(BC$Class),
+ xlab="Uniformity of Cell Size",ylab="Marginal Adhesion",col=as.numeric(BC$Class))
> BC1 <- BC[,c(3,5,11)]
> str(BC1)
'data.frame': 699 obs. of 3 variables:
$ Uniformity of Cell Size: int 1 4 1 8 1 10 1 1 1 2 ...
$ Marginal Adhesion : int 1 5 1 1 3 8 1 1 1 1 ...
$ Class : Factor w/ 2 levels "2","4": 1 1 1 1 1 2 1 1 1 1 ...
> SVM1 <- svm(Class ~ ., data = BC1, cost=1)
> Ta1 <- table(fitted(SVM1), BC1[,3])
> Ta1
2 4
2 440 25
4 18 216
> sum(diag(Ta1))/sum(Ta1)
[1] 0.9384835
> plot(SVM1, BC1, BC$`Uniformity of Cell Size`~BC$`Marginal Adhesion`)
Error in `[.data.frame`(expand.grid(lis), , labels(terms(x))) :
undefined columns selected
我遇到同样的错误,
> plot(SVM1, BC1, BC1$A~BC1$B)
Error in `[.data.frame`(expand.grid(lis), , labels(terms(x))) :
undefined columns selected
我想我应该包括原始说明。我正在从第1页开始的第一个示例中。 https://drive.google.com/open?id=1_e2bQXf5Htw8HHTQ2ekEXmnHQzLNhrzM
让我知道是否需要更多信息。我在第三个输入中尝试了几种不同的文本变体,但一无所获。