Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
predict.svm不预测新数据_R_Svm_Libsvm_Predict - Fatal编程技术网

predict.svm不预测新数据

predict.svm不预测新数据,r,svm,libsvm,predict,R,Svm,Libsvm,Predict,不幸的是,我在以下简单示例中使用predict()时遇到问题: library(e1071) x <- c(1:10) y <- c(0,0,0,0,1,0,1,1,1,1) test <- c(11:15) mod <- svm(y ~ x, kernel = "linear", gamma = 1, cost = 2, type="C-classification") predict(mod, newdata = test) 库(e1071) x您需要新数据具

不幸的是,我在以下简单示例中使用predict()时遇到问题:

library(e1071)

x <- c(1:10)
y <- c(0,0,0,0,1,0,1,1,1,1)
test <- c(11:15)

mod <- svm(y ~ x, kernel = "linear", gamma = 1, cost = 2, type="C-classification")

predict(mod, newdata = test)
库(e1071)

x您需要新数据具有相同的形式,即使用data.frame帮助:

R> library(e1071)
Loading required package: class
R> df <- data.frame(x=1:10, y=sample(c(0,1), 10, rep=TRUE))
R> mod <- svm(y ~ x, kernel = "linear", gamma = 1, 
+             cost = 2, type="C-classification", data=df)
R> newdf <- data.frame(x=11:15)
R> predict(mod, newdata=newdf)
1 2 3 4 5
0 0 0 0 0
Levels: 0 1
R>

总之,使用公式接口并提供data.frame——这就是R中所有建模函数的基本工作原理。

看起来这是因为您将公式接口误用到了
svm()
。通常,用户提供一个数据框或类似对象,在其中搜索公式中的变量。如果您不这样做,即使这不是最佳实践,通常也无关紧要,但是当您想要预测时,不将变量放入数据框会让您陷入一片混乱。它返回训练数据的原因是,您没有提供包含名为
x
的组件的
newdata
对象。因此,它无法找到新数据
x
,因此返回拟合值。这在我所知道的大多数R
predict
方法中都很常见

然后,解决方案是i)将训练数据放在数据框中,并将
svm
作为
data
参数传递,ii)提供一个包含
x
(从
test
)的新数据框,以
predict()
。例如:

> DF <- data.frame(x = x, y = y)
> mod <- svm(y ~ x, data = DF, kernel = "linear", gamma = 1, cost = 2,
+ type="C-classification")
> predict(mod, newdata = data.frame(x = test))
1 2 3 4 5 
1 1 1 1 1 
Levels: 0 1
DF mod predict(mod,newdata=data.frame(x=test)) 1 2 3 4 5 1 1 1 1 1 级别:0 1
ps:使用测试非常感谢,你救了我一天!为什么要为线性svm定义gamma参数?这是e1071中线性支持向量机的标准实践吗?我只见过那些使用RBF支持向量机的人。我会去找我的时间机器,问我年轻的自己为什么我在九年前写了这篇文章,但从上下文来看,我只是引用了帮助页面,在这段时间里,它可能会改变,也可能不会改变。我不知道这是九年前的事。对不起。我看到用户也使用gamma。我只是好奇,因为我的问题是,为什么我们一般要为线性svm定义gamma。
 ## density-estimation

 # create 2-dim. normal with rho=0:
 X <- data.frame(a = rnorm(1000), b = rnorm(1000))
 attach(X)

 # traditional way:
 m <- svm(X, gamma = 0.1)

 # formula interface:
 m <- svm(~., data = X, gamma = 0.1)
 # or:
 m <- svm(~ a + b, gamma = 0.1)

 # test:
 newdata <- data.frame(a = c(0, 4), b = c(0, 4))
 predict (m, newdata)
> DF <- data.frame(x = x, y = y)
> mod <- svm(y ~ x, data = DF, kernel = "linear", gamma = 1, cost = 2,
+ type="C-classification")
> predict(mod, newdata = data.frame(x = test))
1 2 3 4 5 
1 1 1 1 1 
Levels: 0 1