r - 在 R 中使用 SVM 进行一类分类

标签 r classification svm libsvm

我在 R 中使用包 e1071 来构建一类 SVM 模型。我不知道该怎么做,我也没有在互联网上找到任何例子。

有人可以给出一个示例代码来表征,例如,“鸢尾花”数据集中的“setosa”类具有一类分类模型,然后测试同一数据集中的所有示例(以检查哪些示例属于“setosa”类的特征和哪些例子不是)?

最佳答案

我认为这就是你想要的:

library(e1071)
data(iris)
df <- iris

df <- subset(df ,  Species=='setosa')  #choose only one of the classes

x <- subset(df, select = -Species) #make x variables
y <- df$Species #make y variable(dependent)
model <- svm(x, y,type='one-classification') #train an one-classification model 


print(model)
summary(model) #print summary

# test on the whole set
pred <- predict(model, subset(iris, select=-Species)) #create predictions

输出:

-概括:
> summary(model)

Call:
svm.default(x = x, y = y, type = "one-classification")


Parameters:
   SVM-Type:  one-classification 
 SVM-Kernel:  radial 
      gamma:  0.25 
         nu:  0.5 

Number of Support Vectors:  27




Number of Classes: 1

- 预测(出于视觉原因,此处仅显示了一些预测(其中 Species=='setosa'):
> pred
    1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16    17    18    19    20    21    22 
 TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE 
   23    24    25    26    27    28    29    30    31    32    33    34    35    36    37    38    39    40    41    42    43    44 
FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE 
   45    46    47    48    49    50 
FALSE  TRUE  TRUE  TRUE  TRUE  TRUE 

关于r - 在 R 中使用 SVM 进行一类分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27375517/

相关文章:

machine-learning - SGD 型号 "overconfidence"

python - 在sklearn中运行10倍交叉验证后如何运行SVC分类器?

c++ - Opencv 3 SVM trainAuto 是否也缩放标签?

python - 非线性决策边界的 SVM 图

r - ggplot2 - 关闭与另一个几何图形具有相同美感的几何图形的图例

r - ggplot2:将多个箱线图作为时间序列排列

读取多个 xlsx 文件,每个文件都有多个工作表 - purrr

r - 显示 R Markdown 中的 Seaborn 绘图 (flexdashboard)

python-3.x - 将高斯噪声添加到 float 据集并保存(python)

c# - 为 Twitter 情绪分析项目寻找 C# 中的开源朴素贝叶斯分类器