1
1
# # Support Vector Machine
2
2
#
3
+ # In this notebook we show how you can use KernelFunctions.jl to generate
4
+ # kernel matrices for classification with a support vector machine, as
5
+ # implemented by LIBSVM.
3
6
4
7
using Distributions
5
8
using KernelFunctions
@@ -16,21 +19,23 @@ Random.seed!(1234);
16
19
# Number of samples per class:
17
20
nin = nout = 50 ;
18
21
19
- # Generate data
20
- # # based on SciKit-Learn's sklearn.datasets.make_moons function
22
+ # We generate data based on SciKit-Learn's sklearn.datasets.make_moons function:
23
+
21
24
class1x = cos .(range (0 , π; length= nout))
22
25
class1y = sin .(range (0 , π; length= nout))
23
26
class2x = 1 .- cos .(range (0 , π; length= nin))
24
27
class2y = 1 .- sin .(range (0 , π; length= nin)) .- 0.5
25
28
X = hcat (vcat (class1x, class2x), vcat (class1y, class2y))
26
29
X .+ = 0.1 randn (size (X))
27
- x_train = RowVecs (X);
30
+ x_train = RowVecs (X)
28
31
y_train = vcat (fill (- 1 , nout), fill (1 , nin));
29
32
30
33
# Create a 100×100 2D grid for evaluation:
31
34
test_range = range (floor (Int, minimum (X)), ceil (Int, maximum (X)); length= 100 )
32
35
x_test = ColVecs (mapreduce (collect, hcat, Iterators. product (test_range, test_range)));
33
36
37
+ # ## SVM model
38
+ #
34
39
# Create kernel function:
35
40
k = SqExponentialKernel () ∘ ScaleTransform (1.5 )
36
41
@@ -43,12 +48,8 @@ model = svmtrain(kernelmatrix(k, x_train), y_train; kernel=LIBSVM.Kernel.Precomp
43
48
# Precomputed matrix for prediction
44
49
y_pred, _ = svmpredict (model, kernelmatrix (k, x_train, x_test));
45
50
46
- # Compute prediction on a grid:
47
- plot (; lim= extrema (test_range))
51
+ # Visualize prediction on a grid:
52
+ plot (; lim= extrema (test_range), aspect_ratio = 1 )
48
53
contourf! (test_range, test_range, y_pred; levels= 1 , color= cgrad (:redsblues ), alpha= 0.7 )
49
- scatter! (
50
- X[y_train .== - 1 , 1 ], X[y_train .== - 1 , 2 ]; color= :red , label= " class 1" , widen= false
51
- )
52
- scatter! (
53
- X[y_train .== + 1 , 1 ], X[y_train .== + 1 , 2 ]; color= :blue , label= " class 2" , widen= false
54
- )
54
+ scatter! (X[y_train .== - 1 , 1 ], X[y_train .== - 1 , 2 ]; color= :red , label= " class 1" )
55
+ scatter! (X[y_train .== + 1 , 1 ], X[y_train .== + 1 , 2 ]; color= :blue , label= " class 2" )
0 commit comments