Question: Support Vector Machines import numpy as np from sklearn.datasets import make _ blobs from matplotlib.colors import Normalize import matplotlib.pyplot as plt def linear _ plot
Support Vector Machines
import numpy as np
from sklearn.datasets import makeblobs
from matplotlib.colors import Normalize
import matplotlib.pyplot as plt
def linearplotX y wNone, bNone:
mycolors blue: "steelblue", "red": #ace "green": #a
colors mycolorsred if yi else mycolorsblue for yi in y
# Plot data
fig, ax pltsubplotsnrows ncols figsize
axscatterX: X: colorcolors, s alpha zorder
# Plot boundaries
lowerleft npminnpminX: npminX:
upperright npmaxnpmaxX: npmaxX:
gap upperrightlowerleft
xplot nplinspacelowerleftgap, upperrightgap,
if w is not None and b is not None:
axplotxplotb wxplotw color"gray", lw zorder
axplotxplotb wxplotw color"gray", lw ls zorder
axplotxplotb wxplotw color"gray", lw ls zorder
axsetxlimlowerleftgap, upperrightgap
axsetylimlowerleftgap, upperrightgap
axgridalpha
def partdata:
nprandom.seed
X npzeros
X:nprandom.rand
X:nprandom.rand
X:nprandom.rand
X:nprandom.rand
X
X
X
X
y npones
y:
y
y
return X y
def partdataN seed:
nprandom.seedseed
X nprandom.uniformN
y nparray if yx else for xy in zipX: npsinnppiX: X:
X X nprandom.normalN
return X y
def nonlinearplotX y clfNone:
mycolors blue: "steelblue", "red": #ace "green": #a
fig, ax pltsubplotsnrows ncols figsize
colors mycolorsred if yi else mycolorsblue for yi in y
axscatterX:X: markero colorcolors, s alpha
axarrow headlength headwidth fc"gray", ec"gray", lw alpha
axarrow headlength headwidth fc"gray", ec"gray", lw alpha
z nplinspace
axsetxlim
axsetylim
axspinestopsetvisibleFalse
axspinesrightsetvisibleFalse
axspinesbottomsetvisibleFalse
axspinesleftsetvisibleFalse
pltxticks fontsize
pltyticks fontsize
if clf:
clffitXy
xmin X:min
xmax X:max
ymin X:min
ymax X:max
colors mycolorsred if yi else mycolorsblue for yi in y
XX YY npmgridxmin:xmax:j ymin:ymax:j
Z clfdecisionfunctionnpcXXravel YYravel
# Put the result into a color plot
Z ZreshapeXXshape
pltcontourXX YY Z colorsmycolorsblue "gray", mycolorsred linestyles
levels linewidths alpha
class MidpointNormalizeNormalize:
def initself vminNone, vmaxNone, midpointNone, clipFalse:
self.midpoint midpoint
Normalize.initself vmin, vmax, clip
def callself value, clipNone:
x y selfvmin, self.midpoint, self.vmax
return npmamaskedarraynpinterpvalue x y
def plotSearchGridgrid:
scores x for x in grid.cvresultsmeantestscore"
scores nparrayscoresreshapelengridparamgridC lengridparamgridgamma
pltfigur
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
