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 make_blobs
from matplotlib.colors import Normalize
import matplotlib.pyplot as plt
def linear_plot(X, y, w=None, b=None):
mycolors ={"blue": "steelblue", "red": "#a76c6e", "green": "#6a9373"}
colors =[mycolors["red"] if yi==1 else mycolors["blue"] for yi in y]
# Plot data
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,8))
ax.scatter(X[:,0], X[:,1], color=colors, s=150, alpha=0.95, zorder=2)
# Plot boundaries
lower_left = np.min([np.min(X[:,0]), np.min(X[:,1])])
upper_right = np.max([np.max(X[:,0]), np.max(X[:,1])])
gap =.1*(upper_right-lower_left)
xplot = np.linspace(lower_left-gap, upper_right+gap, 20)
if w is not None and b is not None:
ax.plot(xplot,(-b - w[0]*xplot)/w[1], color="gray", lw=2, zorder=1)
ax.plot(xplot,(1-b - w[0]*xplot)/w[1], color="gray", lw=2, ls="--", zorder=1)
ax.plot(xplot,(-1-b - w[0]*xplot)/w[1], color="gray", lw=2, ls="--", zorder=1)
ax.set_xlim([lower_left-gap, upper_right+gap])
ax.set_ylim([lower_left-gap, upper_right+gap])
ax.grid(alpha=0.25)
def part2data():
np.random.seed(1239)
X = np.zeros((22,2))
X[0:10,0]=1.5*np.random.rand(10)
X[0:10,1]=1.5*np.random.rand(10)
X[10:20,0]=1.5*np.random.rand(10)+1.75
X[10:20,1]=1.5*np.random.rand(10)+1
X[20,0]=1.5
X[20,1]=2.25
X[21,0]=1.6
X[21,1]=0.25
y = np.ones(22)
y[10:20]=-1
y[20]=1
y[21]=-1
return X, y
def part3data(N=100, seed=1235):
np.random.seed(seed)
X = np.random.uniform(-1,1,(N,2))
y = np.array([1 if y-x >0 else -1 for (x,y) in zip(X[:,0]**2* np.sin(2*np.pi*X[:,0]), X[:,1])])
X = X + np.random.normal(0,.1,(N,2))
return X, y
def nonlinear_plot(X, y, clf=None):
mycolors ={"blue": "steelblue", "red": "#a76c6e", "green": "#6a9373"}
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10,10))
colors =[mycolors["red"] if yi==1 else mycolors["blue"] for yi in y]
ax.scatter(X[:,0],X[:,1], marker='o', color=colors, s=100, alpha=0.5)
ax.arrow(-1.25,0,2.5,0, head_length=0.05, head_width=0.05, fc="gray", ec="gray", lw=2, alpha=0.25)
ax.arrow(0,-1.25,0,2.5, head_length=0.05, head_width=0.05, fc="gray", ec="gray", lw=2, alpha=0.25)
z = np.linspace(0.25,3.5,10)
ax.set_xlim([-1.50,1.50])
ax.set_ylim([-1.50,1.50])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
plt.xticks([], fontsize=16)
plt.yticks([], fontsize=16)
if clf:
clf.fit(X,y)
x_min = X[:,0].min()+.00
x_max = X[:,0].max()-.00
y_min = X[:,1].min()+.00
y_max = X[:,1].max()-.00
colors =[mycolors["red"] if yi==1 else mycolors["blue"] for yi in y]
XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])
# Put the result into a color plot
Z = Z.reshape(XX.shape)
plt.contour(XX, YY, Z, colors=[mycolors["blue"], "gray", mycolors["red"]], linestyles=['--','-','--'],
levels=[-1.0,0,1.0], linewidths=[2,2,2], alpha=0.9)
class MidpointNormalize(Normalize):
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
self.midpoint = midpoint
Normalize.__init__(self, vmin, vmax, clip)
def __call__(self, value, clip=None):
x, y =[self.vmin, self.midpoint, self.vmax],[0,0.5,1]
return np.ma.masked_array(np.interp(value, x, y))
def plotSearchGrid(grid):
scores =[x for x in grid.cv_results_["mean_test_score"]]
scores = np.array(scores).reshape(len(grid.param_grid["C"]), len(grid.param_grid["gamma"]))
plt.figur

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!