如何使用svm为R中的多个类别创建分类模型?

SVM是一种监督型机器学习算法,可用于分类或回归挑战,但大多数情况下我们将其用于分类。使用svm的分类也可以针对两个或更多类别进行。在R中,我们可以简单地使用e1071包的svm函数。

示例

考虑虹膜数据-

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 ...

示例

head(iris,20)

输出结果

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species

1    5.1          3.5          1.4          0.2    setosa

2    4.9          3.0          1.4          0.2    setosa

3    4.7          3.2          1.3          0.2    setosa

4    4.6          3.1          1.5          0.2    setosa

5    5.0          3.6          1.4          0.2    setosa

6    5.4          3.9          1.7          0.4    setosa

7    4.6          3.4          1.4          0.3    setosa

8    5.0          3.4          1.5          0.2    setosa

9    4.4          2.9          1.4          0.2    setosa

10   4.9          3.1          1.5          0.1    setosa

11   5.4          3.7          1.5          0.2    setosa

12   4.8          3.4          1.6          0.2    setosa

13   4.8          3.0          1.4          0.1    setosa

14   4.3          3.0          1.1          0.1    setosa

15   5.8          4.0          1.2          0.2    setosa

16   5.7          4.4          1.5          0.4    setosa

17   5.4          3.9          1.3          0.4    setosa

18   5.1          3.5          1.4          0.3    setosa

19   5.7          3.8          1.7          0.3    setosa

20   5.1          3.8          1.5          0.3    setosa

加载e1071软件包并创建svm模型来预测物种-

示例

library(e1071)

model_1<-svm(iris$Species~.,iris)

model_1

输出结果

Call:

svm(formula = iris$Species ~ ., data = iris)

Parameters:

   SVM-Type: C-classification

   SVM-Kernel: radial

      cost: 1

Number of Support Vectors: 51

示例

Consider the below data frame:

x1<-rnorm(20,1,1.05)

x2<-rnorm(20,1,1.05)

x3<-rnorm(20,1,1.05)

y1<-factor(sample(LETTERS[1:4],20,replace=TRUE))

df1<-data.frame(x1,x2,x3,y1)

df1

输出结果

      x1          x2       x3        y1

1 -0.16972931 0.7246676 1.45289129    D

2 0.70684500 2.2078975 1.64698238     D

3 0.75542931 1.7193236 1.31461683     A

4 -0.01975337 0.6848992 0.80361117    D

5 0.86139532 1.3101784 0.35196665     C

6 -0.53543129 -0.1596975 1.06723416   B

7 -0.81283371 2.1653334 1.93182228    A

8 -0.31556364 -0.4410462 1.61967614   A

9 1.52678513 1.9356670 0.04359926     D

10 1.24594463 0.6215577 0.71009713    A

11 1.53888275 0.7491438 2.08191985    D

12 1.19568488 0.6597553 2.40080721    C

13 -0.18610407 0.3972270 2.23357076   D

14 0.56453388 0.5964609 0.94534907    D

15 1.98699347 0.8026872 -0.68205488   D

16 2.00788377 0.9093129 3.24888927    B

17 1.69652350 0.5379913 0.67402105    A

18 1.28221388 1.7807587 2.06529243    B

19 0.17814671 -0.4299207 0.47859582   D

20 2.82514461 1.9284933 1.59796618    D

创建svm模型以预测y1-

示例

model_2<-svm(df1$y1~.,df1)

model_2

输出结果

Call:

svm(formula = df1$y1 ~ ., data = df1)

Parameters:

   SVM-Type: C-classification

   SVM-Kernel: radial

      cost: 1

Number of Support Vectors: 20

以上是 如何使用svm为R中的多个类别创建分类模型? 的全部内容, 来源链接: utcz.com/z/326430.html

回到顶部