Skip to content

Commit cc2430b

Browse files
committed
Rebuild dev docs at master=0ba5c24
1 parent 7e810c4 commit cc2430b

File tree

300 files changed

+6494
-1505
lines changed

Some content is hidden

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

300 files changed

+6494
-1505
lines changed

dev/_downloads/plot_classifier_comparison.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from sklearn.cross_validation import train_test_split
3535
from sklearn.preprocessing import StandardScaler
3636
from sklearn.datasets import make_moons, make_circles, make_classification
37+
from sklearn.neural_network import MLPClassifier
3738
from sklearn.neighbors import KNeighborsClassifier
3839
from sklearn.svm import SVC
3940
from sklearn.gaussian_process import GaussianProcessClassifier
@@ -46,8 +47,8 @@
4647
h = .02 # step size in the mesh
4748

4849
names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Gaussian Process",
49-
"Decision Tree", "Random Forest", "AdaBoost", "Naive Bayes",
50-
"QDA"]
50+
"Decision Tree", "Random Forest", "Neural Net", "AdaBoost",
51+
"Naive Bayes", "QDA"]
5152

5253
classifiers = [
5354
KNeighborsClassifier(3),
@@ -56,6 +57,7 @@
5657
GaussianProcessClassifier(1.0 * RBF(1.0), warm_start=True),
5758
DecisionTreeClassifier(max_depth=5),
5859
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
60+
MLPClassifier(alpha=1),
5961
AdaBoostClassifier(),
6062
GaussianNB(),
6163
QuadraticDiscriminantAnalysis()]

dev/_downloads/plot_mlp_alpha.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""
2+
================================================
3+
Varying regularization in Multi-layer Perceptron
4+
================================================
5+
6+
A comparison of different values for regularization parameter 'alpha' on
7+
synthetic datasets. The plot shows that different alphas yield different
8+
decision functions.
9+
10+
Alpha is a parameter for regularization term, aka penalty term, that combats
11+
overfitting by constraining the size of the weights. Increasing alpha may fix
12+
high variance (a sign of overfitting) by encouraging smaller weights, resulting
13+
in a decision boundary plot that appears with lesser curvatures.
14+
Similarly, decreasing alpha may fix high bias (a sign of underfitting) by
15+
encouraging larger weights, potentially resulting in a more complicated
16+
decision boundery.
17+
"""
18+
print(__doc__)
19+
20+
21+
# Author: Issam H. Laradji
22+
# License: BSD 3 clause
23+
24+
import numpy as np
25+
from matplotlib import pyplot as plt
26+
from matplotlib.colors import ListedColormap
27+
from sklearn.cross_validation import train_test_split
28+
from sklearn.preprocessing import StandardScaler
29+
from sklearn.datasets import make_moons, make_circles, make_classification
30+
from sklearn.neural_network import MLPClassifier
31+
32+
h = .02 # step size in the mesh
33+
34+
alphas = np.logspace(-5, 3, 5)
35+
names = []
36+
for i in alphas:
37+
names.append('alpha ' + str(i))
38+
39+
classifiers = []
40+
for i in alphas:
41+
classifiers.append(MLPClassifier(alpha=i, random_state=1))
42+
43+
X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,
44+
random_state=0, n_clusters_per_class=1)
45+
rng = np.random.RandomState(2)
46+
X += 2 * rng.uniform(size=X.shape)
47+
linearly_separable = (X, y)
48+
49+
datasets = [make_moons(noise=0.3, random_state=0),
50+
make_circles(noise=0.2, factor=0.5, random_state=1),
51+
linearly_separable]
52+
53+
figure = plt.figure(figsize=(17, 9))
54+
i = 1
55+
# iterate over datasets
56+
for X, y in datasets:
57+
# preprocess dataset, split into training and test part
58+
X = StandardScaler().fit_transform(X)
59+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4)
60+
61+
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
62+
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
63+
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
64+
np.arange(y_min, y_max, h))
65+
66+
# just plot the dataset first
67+
cm = plt.cm.RdBu
68+
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
69+
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
70+
# Plot the training points
71+
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
72+
# and testing points
73+
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6)
74+
ax.set_xlim(xx.min(), xx.max())
75+
ax.set_ylim(yy.min(), yy.max())
76+
ax.set_xticks(())
77+
ax.set_yticks(())
78+
i += 1
79+
80+
# iterate over classifiers
81+
for name, clf in zip(names, classifiers):
82+
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
83+
clf.fit(X_train, y_train)
84+
score = clf.score(X_test, y_test)
85+
86+
# Plot the decision boundary. For that, we will assign a color to each
87+
# point in the mesh [x_min, m_max]x[y_min, y_max].
88+
if hasattr(clf, "decision_function"):
89+
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
90+
else:
91+
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
92+
93+
# Put the result into a color plot
94+
Z = Z.reshape(xx.shape)
95+
ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
96+
97+
# Plot also the training points
98+
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
99+
# and testing points
100+
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright,
101+
alpha=0.6)
102+
103+
ax.set_xlim(xx.min(), xx.max())
104+
ax.set_ylim(yy.min(), yy.max())
105+
ax.set_xticks(())
106+
ax.set_yticks(())
107+
ax.set_title(name)
108+
ax.text(xx.max() - .3, yy.min() + .3, ('%.2f' % score).lstrip('0'),
109+
size=15, horizontalalignment='right')
110+
i += 1
111+
112+
figure.subplots_adjust(left=.02, right=.98)
113+
plt.show()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
========================================================
3+
Compare Stochastic learning strategies for MLPClassifier
4+
========================================================
5+
6+
This example visualizes some training loss curves for different stochastic
7+
learning strategies, including SGD and Adam. Because of time-constraints, we
8+
use several small datasets, for which L-BFGS might be more suitable. The
9+
general trend shown in these examples seems to carry over to larger datasets,
10+
however.
11+
"""
12+
13+
print(__doc__)
14+
import matplotlib.pyplot as plt
15+
from sklearn.neural_network import MLPClassifier
16+
from sklearn.preprocessing import MinMaxScaler
17+
from sklearn import datasets
18+
19+
# different learning rate schedules and momentum parameters
20+
params = [{'algorithm': 'sgd', 'learning_rate': 'constant', 'momentum': 0,
21+
'learning_rate_init': 0.2},
22+
{'algorithm': 'sgd', 'learning_rate': 'constant', 'momentum': .9,
23+
'nesterovs_momentum': False, 'learning_rate_init': 0.2},
24+
{'algorithm': 'sgd', 'learning_rate': 'constant', 'momentum': .9,
25+
'nesterovs_momentum': True, 'learning_rate_init': 0.2},
26+
{'algorithm': 'sgd', 'learning_rate': 'invscaling', 'momentum': 0,
27+
'learning_rate_init': 0.2},
28+
{'algorithm': 'sgd', 'learning_rate': 'invscaling', 'momentum': .9,
29+
'nesterovs_momentum': True, 'learning_rate_init': 0.2},
30+
{'algorithm': 'sgd', 'learning_rate': 'invscaling', 'momentum': .9,
31+
'nesterovs_momentum': False, 'learning_rate_init': 0.2},
32+
{'algorithm': 'adam'}]
33+
34+
labels = ["constant learning-rate", "constant with momentum",
35+
"constant with Nesterov's momentum",
36+
"inv-scaling learning-rate", "inv-scaling with momentum",
37+
"inv-scaling with Nesterov's momentum", "adam"]
38+
39+
plot_args = [{'c': 'red', 'linestyle': '-'},
40+
{'c': 'green', 'linestyle': '-'},
41+
{'c': 'blue', 'linestyle': '-'},
42+
{'c': 'red', 'linestyle': '--'},
43+
{'c': 'green', 'linestyle': '--'},
44+
{'c': 'blue', 'linestyle': '--'},
45+
{'c': 'black', 'linestyle': '-'}]
46+
47+
48+
def plot_on_dataset(X, y, ax, name):
49+
# for each dataset, plot learning for each learning strategy
50+
print("\nlearning on dataset %s" % name)
51+
ax.set_title(name)
52+
X = MinMaxScaler().fit_transform(X)
53+
mlps = []
54+
if name == "digits":
55+
# digits is larger but converges fairly quickly
56+
max_iter = 15
57+
else:
58+
max_iter = 400
59+
60+
for label, param in zip(labels, params):
61+
print("training: %s" % label)
62+
mlp = MLPClassifier(verbose=0, random_state=0,
63+
max_iter=max_iter, **param)
64+
mlp.fit(X, y)
65+
mlps.append(mlp)
66+
print("Training set score: %f" % mlp.score(X, y))
67+
print("Training set loss: %f" % mlp.loss_)
68+
for mlp, label, args in zip(mlps, labels, plot_args):
69+
ax.plot(mlp.loss_curve_, label=label, **args)
70+
71+
72+
fig, axes = plt.subplots(2, 2, figsize=(15, 10))
73+
# load / generate some toy datasets
74+
iris = datasets.load_iris()
75+
digits = datasets.load_digits()
76+
data_sets = [(iris.data, iris.target),
77+
(digits.data, digits.target),
78+
datasets.make_circles(noise=0.2, factor=0.5, random_state=1),
79+
datasets.make_moons(noise=0.3, random_state=0)]
80+
81+
for ax, data, name in zip(axes.ravel(), data_sets, ['iris', 'digits',
82+
'circles', 'moons']):
83+
plot_on_dataset(*data, ax=ax, name=name)
84+
85+
fig.legend(ax.get_lines(), labels=labels, ncol=3, loc="upper center")
86+
plt.show()

dev/_downloads/plot_mnist_filters.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
=====================================
3+
Visualization of MLP weights on MNIST
4+
=====================================
5+
6+
Sometimes looking at the learned coefficients of a neural network can provide
7+
insight into the learning behavior. For example if weights look unstructured,
8+
maybe some were not used at all, or if very large coefficients exist, maybe
9+
regularization was too low or the learning rate too high.
10+
11+
This example shows how to plot some of the first layer weights in a
12+
MLPClassifier trained on the MNIST dataset.
13+
14+
The input data consists of 28x28 pixel handwritten digits, leading to 784
15+
features in the dataset. Therefore the first layer weight matrix have the shape
16+
(784, hidden_layer_sizes[0]). We can therefore visualize a single column of
17+
the weight matrix as a 28x28 pixel image.
18+
19+
To make the example run faster, we use very few hidden units, and train only
20+
for a very short time. Training longer would result in weights with a much
21+
smoother spatial appearance.
22+
"""
23+
print(__doc__)
24+
25+
import matplotlib.pyplot as plt
26+
from sklearn.datasets import fetch_mldata
27+
from sklearn.neural_network import MLPClassifier
28+
29+
mnist = fetch_mldata("MNIST original")
30+
# rescale the data, use the traditional train/test split
31+
X, y = mnist.data / 255., mnist.target
32+
X_train, X_test = X[:60000], X[60000:]
33+
y_train, y_test = y[:60000], y[60000:]
34+
35+
# mlp = MLPClassifier(hidden_layer_sizes=(100, 100), max_iter=400, alpha=1e-4,
36+
# algorithm='sgd', verbose=10, tol=1e-4, random_state=1)
37+
mlp = MLPClassifier(hidden_layer_sizes=(50,), max_iter=10, alpha=1e-4,
38+
algorithm='sgd', verbose=10, tol=1e-4, random_state=1,
39+
learning_rate_init=.1)
40+
41+
mlp.fit(X_train, y_train)
42+
print("Training set score: %f" % mlp.score(X_train, y_train))
43+
print("Test set score: %f" % mlp.score(X_test, y_test))
44+
45+
fig, axes = plt.subplots(4, 4)
46+
# use global min / max to ensure all weights are shown on the same scale
47+
vmin, vmax = mlp.coefs_[0].min(), mlp.coefs_[0].max()
48+
for coef, ax in zip(mlp.coefs_[0].T, axes.ravel()):
49+
ax.matshow(coef.reshape(28, 28), cmap=plt.cm.gray, vmin=.5 * vmin,
50+
vmax=.5 * vmax)
51+
ax.set_xticks(())
52+
ax.set_yticks(())
53+
54+
plt.show()
197 Bytes
456 Bytes
450 Bytes
1.11 KB
1.11 KB
338 Bytes

0 commit comments

Comments
 (0)