Skip to content

Commit 8e943b2

Browse files
committed
Add lightning documentation
1 parent 30c0e0b commit 8e943b2

File tree

178 files changed

+37771
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+37771
-0
lines changed

lightning/.buildinfo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Sphinx build info version 1
2+
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3+
config: 03f01e6df4c66de912deebb84e264bee
4+
tags: 645f666f9bcd5a90fca523b33c5a78b7
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
================================
3+
Classification of text documents
4+
================================
5+
6+
"""
7+
import numpy as np
8+
9+
from sklearn.datasets import fetch_20newsgroups_vectorized
10+
from sklearn.cross_validation import train_test_split
11+
12+
from lightning.classification import CDClassifier
13+
from lightning.classification import LinearSVC
14+
from lightning.classification import SGDClassifier
15+
16+
# Load News20 dataset from scikit-learn.
17+
bunch = fetch_20newsgroups_vectorized(subset="all")
18+
X = bunch.data
19+
y = bunch.target
20+
21+
# Select a subset of the classes for faster training.
22+
ind = np.arange(X.shape[0])
23+
subset = y < 5
24+
X = X[ind[subset]]
25+
y = y[subset]
26+
27+
# Train / test split.
28+
X_tr, X_te, y_tr, y_te = train_test_split(X, y,
29+
train_size=0.75,
30+
test_size=0.25,
31+
random_state=0)
32+
33+
clfs = (CDClassifier(loss="squared_hinge",
34+
penalty="l2",
35+
max_iter=20,
36+
random_state=0),
37+
38+
LinearSVC(max_iter=20,
39+
random_state=0),
40+
41+
SGDClassifier(learning_rate="constant",
42+
alpha=1e-3,
43+
max_iter=20,
44+
random_state=0))
45+
46+
for clf in clfs:
47+
print(clf.__class__.__name__)
48+
clf.fit(X_tr, y_tr)
49+
print(clf.score(X_te, y_te))
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"""
2+
=====================
3+
L2 solver comparison
4+
=====================
5+
6+
This example compares different solvers with L2 regularization.
7+
"""
8+
print(__doc__)
9+
10+
import sys
11+
import time
12+
13+
import numpy as np
14+
import matplotlib.pyplot as plt
15+
16+
from sklearn.datasets import make_classification
17+
from sklearn.datasets import fetch_20newsgroups_vectorized
18+
19+
from lightning.classification import SVRGClassifier
20+
from lightning.classification import SDCAClassifier
21+
from lightning.classification import CDClassifier
22+
from lightning.classification import AdaGradClassifier
23+
from lightning.classification import SAGClassifier
24+
25+
from lightning.impl.adagrad_fast import _proj_elastic_all
26+
27+
class Callback(object):
28+
29+
def __init__(self, X, y):
30+
self.X = X
31+
self.y = y
32+
self.obj = []
33+
self.times = []
34+
self.start_time = time.clock()
35+
self.test_time = 0
36+
37+
def __call__(self, clf, t=None):
38+
test_time = time.clock()
39+
40+
if hasattr(clf, "_finalize_coef"):
41+
clf._finalize_coef()
42+
43+
if t is not None:
44+
_proj_elastic_all(clf.eta, t, clf.g_sum_[0], clf.g_norms_[0],
45+
alpha1=0, alpha2=clf.alpha, delta=0,
46+
w=clf.coef_[0])
47+
48+
49+
y_pred = clf.decision_function(self.X).ravel()
50+
loss = (np.maximum(1 - self.y * y_pred, 0) ** 2).mean()
51+
coef = clf.coef_.ravel()
52+
regul = 0.5 * clf.alpha * np.dot(coef, coef)
53+
self.obj.append(loss + regul)
54+
self.test_time += time.clock() - test_time
55+
self.times.append(time.clock() - self.start_time - self.test_time)
56+
57+
try:
58+
dataset = sys.argv[1]
59+
except:
60+
dataset = "synthetic"
61+
62+
if dataset == "news20":
63+
bunch = fetch_20newsgroups_vectorized(subset="all")
64+
X = bunch.data
65+
y = bunch.target
66+
y[y >= 1] = 1
67+
alpha = 1e-4
68+
eta_svrg = 1e-1
69+
eta_sag = 1
70+
eta_adagrad = 1
71+
xlim = (0, 4)
72+
ylim = (0.04, 0.1)
73+
74+
else:
75+
X, y = make_classification(n_samples=10000,
76+
n_features=100,
77+
n_classes=2,
78+
random_state=0)
79+
alpha = 1e-2
80+
eta_svrg = 1e-3
81+
eta_sag = 1e-3
82+
eta_adagrad = 1e-2
83+
xlim = None
84+
ylim = (0.5, 0.6)
85+
86+
y = y * 2 - 1
87+
88+
89+
clf1 = SVRGClassifier(loss="squared_hinge", alpha=alpha, eta=eta_svrg,
90+
n_inner=1.0, max_iter=50, random_state=0)
91+
clf2 = SDCAClassifier(loss="squared_hinge", alpha=alpha,
92+
max_iter=50, n_calls=X.shape[0]/2, random_state=0)
93+
clf3 = CDClassifier(loss="squared_hinge", alpha=alpha, C=1.0/X.shape[0],
94+
max_iter=50, n_calls=X.shape[1]/3, random_state=0)
95+
clf4 = AdaGradClassifier(loss="squared_hinge", alpha=alpha, eta=eta_adagrad,
96+
n_iter=50, n_calls=X.shape[0]/2, random_state=0)
97+
clf5 = SAGClassifier(loss="squared_hinge", alpha=alpha, eta=eta_sag,
98+
max_iter=50, random_state=0)
99+
100+
plt.figure()
101+
102+
for clf, name in ((clf1, "SVRG"),
103+
(clf2, "SDCA"),
104+
(clf3, "PCD"),
105+
(clf4, "AdaGrad"),
106+
(clf5, "SAG")):
107+
print(name)
108+
cb = Callback(X, y)
109+
clf.callback = cb
110+
111+
if name == "PCD" and hasattr(X, "tocsc"):
112+
clf.fit(X.tocsc(), y)
113+
else:
114+
clf.fit(X, y)
115+
116+
plt.plot(cb.times, cb.obj, label=name)
117+
118+
plt.xlim(xlim)
119+
plt.ylim(ylim)
120+
plt.xlabel("CPU time")
121+
plt.ylabel("Objective value")
122+
plt.legend()
123+
124+
plt.show()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
==================
3+
Robust regression
4+
==================
5+
6+
"""
7+
print(__doc__)
8+
9+
import numpy as np
10+
import pylab as pl
11+
12+
from sklearn.datasets import make_regression
13+
from sklearn.utils import check_random_state
14+
from sklearn.linear_model import Ridge
15+
16+
from lightning.regression import LinearSVR
17+
18+
# Generate regression data.
19+
X_train, y_train = make_regression(n_samples=15, n_features=1,
20+
n_informative=1, random_state=0)
21+
22+
# Add noise.
23+
rs = check_random_state(0)
24+
y_train += rs.normal(np.std(y_train), size=X_train.shape[0])
25+
# Add an outlier.
26+
y_train[5] *= 5
27+
28+
X_test = np.linspace(-5, 5, 100).reshape(-1, 1)
29+
30+
pl.figure()
31+
pl.scatter(X_train.ravel(), y_train)
32+
33+
reg = Ridge(alpha=1e-1)
34+
reg.fit(X_train, y_train)
35+
pl.plot(X_test.ravel(), reg.predict(X_test), label="Ridge")
36+
37+
# LinearSVR is equivalent to absolute-loss regression (robust regression)
38+
# when epsilon=0.
39+
reg = LinearSVR(C=10, epsilon=0, fit_intercept=True, random_state=0)
40+
reg.fit(X_train, y_train)
41+
pl.plot(X_test.ravel(), reg.predict(X_test), label="Robust")
42+
43+
pl.legend(loc="upper left")
44+
45+
pl.show()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
==========================
3+
SGD: Convex Loss Functions
4+
==========================
5+
6+
"""
7+
print(__doc__)
8+
9+
import numpy as np
10+
import pylab as pl
11+
from lightning.impl.sgd import Hinge
12+
from lightning.impl.sgd import SquaredHinge
13+
from lightning.impl.sgd import Log
14+
from lightning.impl.sgd import SquaredLoss
15+
16+
###############################################################################
17+
# Define loss funcitons
18+
xmin, xmax = -3, 3
19+
hinge = Hinge(1)
20+
squared_hinge = SquaredHinge()
21+
log = Log()
22+
squared_loss = SquaredLoss()
23+
24+
###############################################################################
25+
# Plot loss funcitons
26+
xx = np.linspace(xmin, xmax, 100)
27+
pl.plot([xmin, 0, 0, xmax], [1, 1, 0, 0], 'k-',
28+
label="Zero-one loss")
29+
pl.plot(xx, [hinge.loss(x, 1) for x in xx], 'g-',
30+
label="Hinge loss")
31+
pl.plot(xx, [squared_hinge.loss(x, 1) for x in xx], 'b--',
32+
label="Squared hinge loss", zorder=3)
33+
pl.plot(xx, [log.loss(x, 1) for x in xx], 'r-',
34+
label="Log loss")
35+
pl.plot(xx, [2.0*squared_loss.loss(x, 1) for x in xx], 'c-',
36+
label="Squared loss")
37+
pl.ylim((0, 5))
38+
pl.legend(loc="upper right")
39+
pl.xlabel(r"$y \cdot f(x)$")
40+
pl.ylabel("$L(y, f(x))$")
41+
pl.show()
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)