|
| 1 | +""" |
| 2 | +================================ |
| 3 | +Sparse non-linear classification |
| 4 | +================================ |
| 5 | +
|
| 6 | +This examples demonstrates how to use `CDClassifier` with L1 penalty to do |
| 7 | +sparse non-linear classification. The trick simply consists in fitting the |
| 8 | +classifier with a kernel matrix (e.g., using an RBF kernel). |
| 9 | +
|
| 10 | +There are a few interesting differences with standard kernel SVMs: |
| 11 | +
|
| 12 | +1. the kernel matrix does not need to be positive semi-definite (hence the |
| 13 | +expression "kernel matrix" above is an abuse of terminology) |
| 14 | +
|
| 15 | +2. the number of "support vectors" will be typically smaller thanks to L1 |
| 16 | +regularization and can be adjusted by the regularization parameter C (the |
| 17 | +smaller C, the fewer the support vectors) |
| 18 | +
|
| 19 | +3. the "support vectors" need not be located at the margin |
| 20 | +""" |
| 21 | + |
| 22 | +import numpy as np |
| 23 | +import pylab as pl |
| 24 | + |
| 25 | +from sklearn.metrics.pairwise import rbf_kernel |
| 26 | + |
| 27 | +from lightning.classification import CDClassifier |
| 28 | +from lightning.classification import KernelSVC |
| 29 | + |
| 30 | +np.random.seed(0) |
| 31 | + |
| 32 | +class SparseNonlinearClassifier(CDClassifier): |
| 33 | + |
| 34 | + def __init__(self, gamma=1e-2, C=1, alpha=1): |
| 35 | + self.gamma = gamma |
| 36 | + super(SparseNonlinearClassifier, self).__init__(C=C, |
| 37 | + alpha=alpha, |
| 38 | + loss="squared_hinge", |
| 39 | + penalty="l1") |
| 40 | + |
| 41 | + def fit(self, X, y): |
| 42 | + K = rbf_kernel(X, gamma=self.gamma) |
| 43 | + self.X_train_ = X |
| 44 | + super(SparseNonlinearClassifier, self).fit(K, y) |
| 45 | + return self |
| 46 | + |
| 47 | + def decision_function(self, X): |
| 48 | + K = rbf_kernel(X, self.X_train_, gamma=self.gamma) |
| 49 | + return super(SparseNonlinearClassifier, self).decision_function(K) |
| 50 | + |
| 51 | + |
| 52 | +def gen_non_lin_separable_data(): |
| 53 | + mean1 = [-1, 2] |
| 54 | + mean2 = [1, -1] |
| 55 | + mean3 = [4, -4] |
| 56 | + mean4 = [-4, 4] |
| 57 | + cov = [[1.0,0.8], [0.8, 1.0]] |
| 58 | + X1 = np.random.multivariate_normal(mean1, cov, 50) |
| 59 | + X1 = np.vstack((X1, np.random.multivariate_normal(mean3, cov, 50))) |
| 60 | + y1 = np.ones(len(X1)) |
| 61 | + X2 = np.random.multivariate_normal(mean2, cov, 50) |
| 62 | + X2 = np.vstack((X2, np.random.multivariate_normal(mean4, cov, 50))) |
| 63 | + y2 = np.ones(len(X2)) * -1 |
| 64 | + return X1, y1, X2, y2 |
| 65 | + |
| 66 | +def plot_contour(X, X1, X2, clf, title): |
| 67 | + pl.figure() |
| 68 | + pl.title(title) |
| 69 | + |
| 70 | + # Plot instances of class 1. |
| 71 | + pl.plot(X1[:,0], X1[:,1], "ro") |
| 72 | + # Plot instances of class 2. |
| 73 | + pl.plot(X2[:,0], X2[:,1], "bo") |
| 74 | + |
| 75 | + # Select "support vectors". |
| 76 | + if hasattr(clf, "support_vectors_"): |
| 77 | + sv = clf.support_vectors_ |
| 78 | + else: |
| 79 | + sv = X[clf.coef_.ravel() != 0] |
| 80 | + |
| 81 | + # Plot support vectors. |
| 82 | + pl.scatter(sv[:, 0], sv[:, 1], s=100, c="g") |
| 83 | + |
| 84 | + # Plot decision surface. |
| 85 | + A, B = np.meshgrid(np.linspace(-6,6,50), np.linspace(-6,6,50)) |
| 86 | + C = np.array([[x1, x2] for x1, x2 in zip(np.ravel(A), np.ravel(B))]) |
| 87 | + Z = clf.decision_function(C).reshape(A.shape) |
| 88 | + pl.contour(A, B, Z, [0.0], colors='k', linewidths=1, origin='lower') |
| 89 | + |
| 90 | + pl.axis("tight") |
| 91 | + |
| 92 | +# Generate synthetic data from 2 classes. |
| 93 | +X1, y1, X2, y2 = gen_non_lin_separable_data() |
| 94 | + |
| 95 | +# Combine them to form a training set. |
| 96 | +X = np.vstack((X1, X2)) |
| 97 | +y = np.hstack((y1, y2)) |
| 98 | + |
| 99 | +# Train the classifiers. |
| 100 | +clf = SparseNonlinearClassifier(gamma=0.1, alpha=1./0.05) |
| 101 | +clf.fit(X, y) |
| 102 | + |
| 103 | +clf2 = KernelSVC(gamma=0.1, kernel="rbf", alpha=1e-2) |
| 104 | +clf2.fit(X, y) |
| 105 | + |
| 106 | +# Plot contours. |
| 107 | +plot_contour(X, X1, X2, clf, "Sparse") |
| 108 | +plot_contour(X, X1, X2, clf2, "Kernel SVM") |
| 109 | + |
| 110 | +pl.show() |
0 commit comments