import

addutils.palette

as

pal

import

seaborn

as

sns

from

bokeh.models.mappers

import

LinearColorMapper

x_min

,

x_max

=

X

[:,

0

]

.

min

()

-

1

,

X

[:,

0

]

.

max

()

+

1

y_min

,

y_max

=

X

[:,

1

]

.

min

()

-

1

,

X

[:,

1

]

.

max

()

+

1

xs

=

np

.

arange

(

x_min

,

x_max

,

h

*

2

)

ys

=

np

.

arange

(

y_min

,

y_max

,

h

*

2

)

xx

,

yy

=

np

.

meshgrid

(

xs

,

ys

)

xx

=

xx

.

ravel

()

yy

=

yy

.

ravel

()

titles

=

[

'SVC with linear kernel'

,

'SVC with RBF kernel'

,

'SVC with polynomial (degree 3) kernel'

,

'LinearSVC (linear kernel)'

]

grid

=

[]

palette

=

map

(

pal

.

to_hex

,

sns

.

color_palette

(

'Paired'

,

6

))

palette_dots

=

palette

[

1

::

2

]

# colours in odd indices

palette_patches

=

palette

[::

2

]

# colours in even indices

# Bokeh understands the mapping interval for the colors as open on the high side.

# So we have to customize the color mapper, and raise the upper limit a bit

# to prevent Bokeh from discarding any colours.

# max(Z) is 2, so 2.1 is fine for the upper limit.

patches_cmapper

=

LinearColorMapper

(

low

=

0

,

high

=

2.1

,

palette

=

palette_patches

)

for

i

,

clf

in

enumerate

((

std_svc

,

rbf_svc

,

pol_svc

,

lin_svc

)):

# Plot the decision boundary assigning a color to each point in the mesh

Z

=

clf

.

predict

(

np

.

c_

[

xx

,

yy

])

Z

=

Z

.

reshape

((

ys

.

size

,

xs

.

size

))

# Plot the training points

fig

=

bk

.

figure

(

plot_width

=

390

,

plot_height

=

250

,

title

=

titles

[

i

],

x_range

=

(

x_min

,

x_max

),

y_range

=

(

y_min

,

y_max

))

fig

.

title_text_font_size

=

'10pt'

fig

.

axis

.

major_label_text_color

=

None

fig

.

axis

.

major_tick_line_color

=

None

fig

.

axis

.

minor_tick_line_color

=

None

fig

.

image

(

image

=

[

Z

*

0.99

+

0.1

],

rows

=

[

xs

.

size

],

cols

=

[

ys

.

size

],

x

=

[

x_min

],

y

=

[

y_min

],

dw

=

[

x_max

-

x_min

],

dh

=

[

y_max

-

y_min

],

color_mapper

=

patches_cmapper

)

fig

.

scatter

(

X

[:,

0

],

X

[:,

1

],

size

=

6

,

line_color

=

'black'

,

line_alpha

=

0.5

,

fill_color

=

[

palette_dots

[

j

]

for

j

in

y

])

grid

.

append

(

fig

)

#plt.show()

bk

.

show

(

bk

.

gridplot

([[

grid

[

0

],

grid

[

1

]],

[

grid

[

2

],

grid

[

3

]]]))