Plotting decision boundary/regions for classifier

Imagine i have some classifier, which given some point on the plane produces the label for this point. I want to plot decision regions such as : svm

I’ve ended up with something like this:

function decision_region(clf)
    xgrid = collect(-2 : 0.05 : 3)
    ygrid = collect(-5 : 0.05 : 3)
    bound(u, v) = encodemap(predict(clf, Matrix([u v])))
    contour(xgrid, ygrid, bound)
end

which produces very wiggly boundary, also there are no colors for regions:
decision_4
decision_reg

How can i produce more pleasant pictures with decision regions colored according to label?

3 Likes

Seems like you’re on the right track. You can fill the contour regions by passing the f=true flag. To make the contour less wiggly, one option is to increase the resolution of the grid. Below is an example which might serve as inspiration:

using Plots

Random.seed!(1)
p = rand(40,3) + rand(40,3)*im
r = 0:.002:1

# classifier: returns 1, 2, or 3 for any given (x, y)
f(x, y) = findmin(sum(abs.(x + y*im .- p); dims=1)[:])[2]

# colors and markers for the 3 regions
ccol = cgrad([RGB(1,.3,.3), RGB(.4,1,.4), RGB(.3,.3,1)])
mcol = [RGB(1,.1,.1) RGB(.3,1,.3) RGB(.1,.1,1)]
m = [:rect :circle :utriangle]

anim = @animate for d = 0:.03:2π
    contour(r, r, f, f=true, nlev=3, c=ccol, leg=:none)
    scatter!(real(p), imag(p), m=m, c=mcol, lims=(0,1))
    gui()
    c = [cis(d)sin(d+.5π) cis(d+2π/3)cos(d) cis(d-2π/3)]
    global p += .003(c .+ .5(1+im) .- p)
end

gif(anim, "contour.gif", fps = 30)

This code produces the following output:

contour

16 Likes

It looks really awesome, why don’t they have this example at the Plots main page?

1 Like

Beautiful!

I’m completely hooked on contour now, thanks for bringing this up! This is what happened when I changed the number of regions to 50, just had to share :slight_smile:

using Plots

Random.seed!(0)
N = 50; r = 0:.002:1; p = rand(ComplexF64, 40, N)
f(x, y) = findmin(sum(abs.(x + y*im .- p); dims=1)[:])[2]
c = [cgrad(:lighttest)[z] for z=0:1/(N-1):1]'

gif(@animate(for d = 0:.03:5π
    contour(r, r, f, f=true, nlev=N, c=:lighttest, leg=:none)
    display(scatter!(reim(p)..., c=c, lims=(0,1)))
    global p += .005([cis(4sin(.3d)+2n*π/N) for n=1:N]'.+.5(1+im).-p)
end), "contour.gif", fps = 30)
13 Likes

Now that’s f*n cool!

I’ll echo the sentiment above, why don’t we have this in the Plots docs? :slight_smile: Feel like a PR to PlotDocs?

2 Likes