import numpy as np import matplotlib.pyplot as plt yhat = np.arange(-3, 3, 0.01) plt.figure(figsize=(12,5)) plt.subplot(121) plt.plot(yhat, (yhat>0), label='Neymann Pearson loss') plt.plot(yhat, np.maximum(1+yhat,0), alpha=0.5, linewidth=4, label='Hinge loss') plt.grid(), plt.legend(), plt.axis('equal'), plt.xlim(-3, 3) plt.xlabel(r'$\tilde{y}$'), plt.ylabel('Loss') plt.title('Loss for $y^{(i)}=-1$') plt.subplot(122) plt.plot(yhat, (yhat<0), label='Neymann Pearson loss') plt.plot(yhat, np.maximum(1-yhat,0), alpha=0.5, linewidth=4, label='Hinge loss') plt.grid(), plt.legend(), plt.axis('equal'), plt.xlim(-3, 3) plt.xlabel(r'$\tilde{y}$'), plt.ylabel('Loss') plt.title('Loss for $y^{(i)}=1$') plt.show() np.random.seed(7030) xp = np.random.randn(2,100) xp[0,:] += 0.2*xp[1,:] xp += 6*np.random.rand(2,1) yp = np.ones(xp.shape[1]) xn = np.random.randn(2,200) xn[0,:] -= 1*xn[1,:] xn += -6*np.random.rand(2,1) yn = -np.ones(xn.shape[1]) plt.figure(figsize=(8,8), dpi=100) plt.plot(xp[0,:], xp[1,:], 'o', alpha=0.5, label=r'$y=1$') plt.plot(xn[0,:], xn[1,:], 'o', alpha=0.5, label=r'$y=-1$') plt.xlabel(r'$x_1$'), plt.ylabel(r'$x_2$') plt.xlim([-6, 6]), plt.ylim([-6, 6]) plt.legend(), plt.grid() plt.show() import cvxpy as cp X = np.vstack((xp.T, xn.T)) y = np.hstack((yp, yn)) onev = np.ones(len(y)) w = cp.Variable(2) b = cp.Variable() obj = cp.sum(cp.pos(1-cp.multiply(y,(X@w-onev*b)))) reg = cp.sum_squares(w) problem = cp.Problem(cp.Minimize(obj + reg)) problem.solve() w_svm = w.value b_svm = b.value v1 = np.arange(-8, 8, 0.01) v2 = (b_svm - w_svm[0]*v1)/w_svm[1] vp = (b_svm + 1 - w_svm[0]*v1)/w_svm[1] vn = (b_svm - 1 - w_svm[0]*v1)/w_svm[1] plt.figure(figsize=(8,8), dpi=100) plt.plot(xp[0,:], xp[1,:], 'o', alpha=0.5, label=r'$y=1$') plt.plot(xn[0,:], xn[1,:], 'o', alpha=0.5, label=r'$y=-1$') plt.plot(v1, vp, '--', label=r'$w^Tx-b=1$') plt.plot(v1, v2, label=r'$w^Tx-b=0$') plt.plot(v1, vn, '--', label=r'$w^Tx-b=-1$') plt.xlabel(r'$x_1$'), plt.ylabel(r'$x_2$') plt.title('Support vector machine on a linearly separable set') plt.xlim([-6, 6]), plt.ylim([-6, 6]) plt.legend(), plt.grid() plt.show() print(f'Loss: {obj.value}') print(f'Margin: {1/np.sqrt(reg.value)}') np.random.seed(7030) xp = np.random.randn(2,100) xp[0,:] += 0.2*xp[1,:] xp += 3*np.random.rand(2,1) yp = np.ones(xp.shape[1]) xn = np.random.randn(2,200) xn[0,:] -= 1*xn[1,:] xn += -3*np.random.rand(2,1) yn = -np.ones(xn.shape[1]) import cvxpy as cp X = np.vstack((xp.T, xn.T)) y = np.hstack((yp, yn)) onev = np.ones(len(y)) w = cp.Variable(2) b = cp.Variable() obj = cp.sum(cp.pos(1-cp.multiply(y,(X@w-onev*b)))) reg = cp.sum_squares(w) problem = cp.Problem(cp.Minimize(obj + reg)) problem.solve() w_svm = w.value b_svm = b.value v1 = np.arange(-8, 8, 0.01) v2 = (b_svm - w_svm[0]*v1)/w_svm[1] vp = (b_svm + 1 - w_svm[0]*v1)/w_svm[1] vn = (b_svm - 1 - w_svm[0]*v1)/w_svm[1] plt.figure(figsize=(8,8), dpi=100) plt.plot(xp[0,:], xp[1,:], 'o', alpha=0.5, label=r'$y=1$') plt.plot(xn[0,:], xn[1,:], 'o', alpha=0.5, label=r'$y=-1$') plt.plot(v1, vp, '--', label=r'$w^Tx-b=1$') plt.plot(v1, v2, label=r'$w^Tx-b=0$') plt.plot(v1, vn, '--', label=r'$w^Tx-b=-1$') plt.xlabel(r'$x_1$'), plt.ylabel(r'$x_2$') plt.title('Support vector machine on a linearly non-separable set') plt.xlim([-6, 6]), plt.ylim([-6, 6]) plt.legend(), plt.grid() plt.show() print(f'Loss: {obj.value}') print(f'Margin: {1/np.sqrt(reg.value)}') import numpy as np import matplotlib.pyplot as plt ytilde = np.arange(-3, 3, 0.01) plt.figure(figsize=(14,8), dpi=100) plt.subplot(121) plt.plot(ytilde, (ytilde>0), label='Neymann Pearson loss') plt.plot(ytilde, np.maximum(1+ytilde,0), alpha=0.5, linewidth=4, label='Hinge loss') plt.plot(ytilde, np.log(1+np.exp(ytilde)), alpha=0.5, linewidth=4, label='Logistic loss') plt.grid(), plt.legend(), plt.axis('equal'), plt.xlim(-3, 3) plt.xlabel(r'$\tilde{y}$'), plt.ylabel('Loss') plt.title('Loss for $y^{(i)}=-1$') plt.subplot(122) plt.plot(ytilde, (ytilde<0), label='Neymann Pearson loss') plt.plot(ytilde, np.maximum(1-ytilde,0), alpha=0.5, linewidth=4, label='Hinge loss') plt.plot(ytilde, np.log(1+np.exp(-ytilde)), alpha=0.5, linewidth=4, label='Logistic loss') plt.grid(), plt.legend(), plt.axis('equal'), plt.xlim(-3, 3) plt.xlabel(r'$\tilde{y}$'), plt.ylabel('Loss') plt.title('Loss for $y^{(i)}=1$') plt.show() np.random.seed(7030) xp = np.random.randn(2,100) xp[0,:] += 0.5*xp[1,:] xp += 5*np.random.rand(2,1) yp = np.ones(xp.shape[1]) xn = np.random.randn(2,200) xn[0,:] -= xn[1,:] xn += -5*np.random.rand(2,1) yn = -np.ones(xn.shape[1]) X = np.vstack((xp.T, xn.T)) y = np.hstack((yp, yn)) onev = np.ones(len(y)) w = cp.Variable(2) b = cp.Variable() obj_svm = cp.sum(cp.pos(1-cp.multiply(y,(X@w-onev*b)))) obj_log = cp.sum(cp.logistic(-cp.multiply(y,(X@w-onev*b)))) reg = cp.sum_squares(w) problem = cp.Problem(cp.Minimize(obj_svm + reg)).solve() w_svm, b_svm = w.value, b.value problem = cp.Problem(cp.Minimize(obj_log + reg)).solve() w_log, b_log = w.value, b.value v1_svm = np.arange(-8, 8, 0.01) v2_svm = (b_svm - w_svm[0]*v1_svm)/w_svm[1] v1_log = np.arange(-8, 8, 0.01) v2_log = (b_log - w_log[0]*v1_log)/w_log[1] plt.figure(figsize=(8,8), dpi=100) plt.plot(xp[0,:], xp[1,:], 'o', alpha=0.5, label=r'$y=1$') plt.plot(xn[0,:], xn[1,:], 'o', alpha=0.5, label=r'$y=-1$') plt.plot(v1_svm, v2_svm, label='Support vector machine') plt.plot(v1_log, v2_log, label='Logistic regression') plt.xlabel(r'$x_1$'), plt.ylabel(r'$x_2$') plt.title('Binary classifiers') plt.xlim([-6, 6]), plt.ylim([-6, 6]) plt.legend(), plt.grid() plt.show()