Question: [PYTHON] Given the code below: # Data and Labels X = np.array([[1,8],[7,2],[6,-1],[-5,0], [-5,1], [-5,2],[6,3],[6,1],[5,2]]) y = np.array([1,-1,-1,1,-1,1,1,-1,-1]) # Support vector parameters w, b = np.array([-1/4,

[PYTHON] Given the code below:

# Data and Labels X = np.array([[1,8],[7,2],[6,-1],[-5,0], [-5,1], [-5,2],[6,3],[6,1],[5,2]]) y = np.array([1,-1,-1,1,-1,1,1,-1,-1])

# Support vector parameters w, b = np.array([-1/4, 1/4]), -1/4

# Plot the data and support vector boundaries linear_plot(X, y, w=w, b=b)

[PYTHON] Given the code below: # Data and Labels X = np.array([[1,8],[7,2],[6,-1],[-5,0],

ATTENTION: Do not change the original codes, just complete them.

TO DO:

1. What is the margin of this particular SVM?

margin = 0 # update margin to be a correct number

# your code here

2. Which training examples are the support vectors? Assign the coordinate in the list. e.g. support_vectors = [(1,0),(0,0)]

# your code here

# support_vectors=[(1,0),(0,0)]

3. Explain your answer for which training examples are support vectors. For the support vectors that you have identified, which are the in-bound support vectors and which are the out-bound support vectors?

4. Which training examples have nonzero slack? List their coordinates.

nonzero_slack=[] # your code here

5. How did you know which training examples had nonzero slack?

6. Compute the slack associated with the misclassified points.

# your code here

# compute the slack associated with the misclassified points # return a list slacks with the slack computations # slacks = [slack1, slack2] # slack1 = # slack2 = print(slacks)

-----------------------------

Note: Below are some helper functions which are used throughout this exercise. Do not modify.

import numpy as np from sklearn.datasets import make_blobs from matplotlib.colors import Normalize import matplotlib.pyplot as plt %matplotlib inline

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.figure(figsize=(10, 8)) plt.subplots_adjust(left=.2, right=0.95, bottom=0.15, top=0.95) plt.imshow(scores, interpolation='nearest', cmap=plt.cm.hot, norm=MidpointNormalize(vmin=0.2, midpoint=0.92)) plt.xlabel('gamma') plt.ylabel('C') plt.colorbar() plt.xticks(np.arange(len(grid.param_grid["gamma"])), grid.param_grid["gamma"], rotation=45) plt.yticks(np.arange(len(grid.param_grid["C"])), grid.param_grid["C"]) plt.title('Validation accuracy') plt.show()

from IPython.core.display import HTML HTML(""" """)

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 Databases Questions!