使用线性核创建 SVM 模型时出现的问题
Problems when creating SVM Model with Linear Kernel
我尝试在 R 中创建一个带有线性核的 SVM 模型
代码如下:
图书馆(e1071)
svm.narrow.margin <- svm(Diagnosis~.,
data = biomed,
type = "C-classification",
cost = 1.0,
kernel = "linear")
然而 returns 这个错误信息:
Error in if (any(as.integer(y) != y)) stop("dependent variable has to be of factor or integer type for classification mode.") :
missing value where TRUE/FALSE needed
In addition: Warning message:
In svm.default(x, y, scale = scale, ..., na.action = na.action) :
NAs introduced by coercion
我 运行 R Studio Cloud 上的同一组代码,它工作正常,令人困惑。
让我们尝试重现问题并浏览解决方案。
这个有效:
svm_works <- svm(Species~., data = iris, type = "C-classification", cost = 1.0,
kernel = "linear")
> svm_works
Call:
svm(formula = Species ~ ., data = iris, type = "C-classification", cost = 1,
kernel = "linear")
Parameters:
SVM-Type: C-classification
SVM-Kernel: linear
cost: 1
Number of Support Vectors: 29
SVM 的结果必须是一个分类器,或者是 R 项中的一个因子,例如物种。
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
让我们看看将预测变量更改为非因子变量时会发生什么。这将产生您的错误。
#Change predictor to non-factor, like Sepal.Length
> svm_not_work <- svm(Sepal.Length~., data = iris, type = "C-classification",
cost = 1.0, kernel = "linear")
Error in svm.default(x, y, scale = scale, ..., na.action = na.action) :
dependent variable has to be of factor or integer type for classification mode.
很可能您的分类器、预测器或公式 (y~., data=data)
中的 y(所有这些都是同义词)有问题。
我尝试在 R 中创建一个带有线性核的 SVM 模型
代码如下:
图书馆(e1071)
svm.narrow.margin <- svm(Diagnosis~.,
data = biomed,
type = "C-classification",
cost = 1.0,
kernel = "linear")
然而 returns 这个错误信息:
Error in if (any(as.integer(y) != y)) stop("dependent variable has to be of factor or integer type for classification mode.") : missing value where TRUE/FALSE needed In addition: Warning message: In svm.default(x, y, scale = scale, ..., na.action = na.action) : NAs introduced by coercion
我 运行 R Studio Cloud 上的同一组代码,它工作正常,令人困惑。
让我们尝试重现问题并浏览解决方案。
这个有效:
svm_works <- svm(Species~., data = iris, type = "C-classification", cost = 1.0,
kernel = "linear")
> svm_works
Call:
svm(formula = Species ~ ., data = iris, type = "C-classification", cost = 1,
kernel = "linear")
Parameters:
SVM-Type: C-classification
SVM-Kernel: linear
cost: 1
Number of Support Vectors: 29
SVM 的结果必须是一个分类器,或者是 R 项中的一个因子,例如物种。
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
让我们看看将预测变量更改为非因子变量时会发生什么。这将产生您的错误。
#Change predictor to non-factor, like Sepal.Length
> svm_not_work <- svm(Sepal.Length~., data = iris, type = "C-classification",
cost = 1.0, kernel = "linear")
Error in svm.default(x, y, scale = scale, ..., na.action = na.action) :
dependent variable has to be of factor or integer type for classification mode.
很可能您的分类器、预测器或公式 (y~., data=data)
中的 y(所有这些都是同义词)有问题。