Skip to content

Commit

Permalink
[MRG + 1] 18 more examples with matplotlib 2.0 updates (scikit-learn#…
Browse files Browse the repository at this point in the history
…8983)

* updated plot_label_propagation_versus_svm_iris.py plot

* updated svm/plot_weighted_samples.py plot

* made semi_supervised/plot_label_propagation_versus_svm_iris.py pep8 compliant

* modified tree/plot_tree_regression.py [size and edgecolor]

* updated tree/plot_tree_regression_multioutput.py [size+color]

* fixed examples/semi_supervised/plot_label_propagation_versus_svm_iris.py for backward compatibility

* neural_networks/plot_mlp_alpha.py - matplotlib2 update

* examples/neural_networks/plot_mlp_alpha.py - pep8 fix

* examples/neighbors/plot_nearest_centroid.py - matplotlib2.0 + pep8 fix

* neighbors/plot_classification.py - matplotlib2.0 + pep8 fix

* examples/neighbors/plot_lof.py - matplotlib2.0 update

* examples/model_selection/plot_underfitting_overfitting.py - matplotlib2.0 + pep8

* examples/mixture/plot_concentration_prior.py - matplotlib2.0 + pep8

* examples/linear_model/plot_logistic_multinomial.py - matplotlib2.0 update

* linear_model/plot_sgd_iris.py - matplotlib2.0 + pep8 fix

* examples/linear_model/plot_sgd_weighted_samples.py - matplotlib2.0 + pep8

* examples/linear_model/plot_sgd_separating_hyperplane.py - matplotlib2.0 update

* examples/feature_selection/plot_permutation_test_for_classification.py - matplotlib + pe8

* examples/linear_model/plot_bayesian_ridge.py - matplotlib2.0 update

* examples/feature_selection/plot_feature_selection.py - matplotlib2.0 update

* examples/feature_selection/plot_f_test_vs_mi.py - matplotlib2.0 + pep8

* examples/feature_selection/plot_f_test_vs_mi.py - matplotlib2.0+ pep8 fix

* examples/model_selection/plot_underfitting_overfitting.py - error fixed

* blue -> black edgecolor fix for 2 examples
  • Loading branch information
Aarshay Jain authored and amueller committed Jun 7, 2017
1 parent 425fc14 commit 6986e9b
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 43 deletions.
6 changes: 3 additions & 3 deletions examples/feature_selection/plot_f_test_vs_mi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
We consider 3 features x_1, x_2, x_3 distributed uniformly over [0, 1], the
target depends on them as follows:
y = x_1 + sin(6 * pi * x_2) + 0.1 * N(0, 1), that is the third features is completely irrelevant.
y = x_1 + sin(6 * pi * x_2) + 0.1 * N(0, 1), that is the third features is
completely irrelevant.
The code below plots the dependency of y against individual x_i and normalized
values of univariate F-tests statistics and mutual information.
Expand Down Expand Up @@ -39,11 +40,10 @@
plt.figure(figsize=(15, 5))
for i in range(3):
plt.subplot(1, 3, i + 1)
plt.scatter(X[:, i], y)
plt.scatter(X[:, i], y, edgecolor='black', s=20)
plt.xlabel("$x_{}$".format(i + 1), fontsize=14)
if i == 0:
plt.ylabel("$y$", fontsize=14)
plt.title("F-test={:.2f}, MI={:.2f}".format(f_test[i], mi[i]),
fontsize=16)
plt.show()

8 changes: 5 additions & 3 deletions examples/feature_selection/plot_feature_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
scores = -np.log10(selector.pvalues_)
scores /= scores.max()
plt.bar(X_indices - .45, scores, width=.2,
label=r'Univariate score ($-Log(p_{value})$)', color='darkorange')
label=r'Univariate score ($-Log(p_{value})$)', color='darkorange',
edgecolor='black')

###############################################################################
# Compare to the weights of an SVM
Expand All @@ -65,7 +66,7 @@
svm_weights /= svm_weights.max()

plt.bar(X_indices - .25, svm_weights, width=.2, label='SVM weight',
color='navy')
color='navy', edgecolor='black')

clf_selected = svm.SVC(kernel='linear')
clf_selected.fit(selector.transform(X), y)
Expand All @@ -74,7 +75,8 @@
svm_weights_selected /= svm_weights_selected.max()

plt.bar(X_indices[selector.get_support()] - .05, svm_weights_selected,
width=.2, label='SVM weights after selection', color='c')
width=.2, label='SVM weights after selection', color='c',
edgecolor='black')


plt.title("Comparing feature selection")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@

###############################################################################
# View histogram of permutation scores
plt.hist(permutation_scores, 20, label='Permutation scores')
plt.hist(permutation_scores, 20, label='Permutation scores',
edgecolor='black')
ylim = plt.ylim()
# BUG: vlines(..., linestyle='--') fails on older versions of matplotlib
#plt.vlines(score, ylim[0], ylim[1], linestyle='--',
# plt.vlines(score, ylim[0], ylim[1], linestyle='--',
# color='g', linewidth=3, label='Classification Score'
# ' (pvalue %s)' % pvalue)
#plt.vlines(1.0 / n_classes, ylim[0], ylim[1], linestyle='--',
# plt.vlines(1.0 / n_classes, ylim[0], ylim[1], linestyle='--',
# color='k', linewidth=3, label='Luck')
plt.plot(2 * [score], ylim, '--g', linewidth=3,
label='Classification Score'
Expand Down
3 changes: 2 additions & 1 deletion examples/linear_model/plot_bayesian_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@

plt.figure(figsize=(6, 5))
plt.title("Histogram of the weights")
plt.hist(clf.coef_, bins=n_features, color='gold', log=True)
plt.hist(clf.coef_, bins=n_features, color='gold', log=True,
edgecolor='black')
plt.scatter(clf.coef_[relevant_features], 5 * np.ones(len(relevant_features)),
color='navy', label="Relevant features")
plt.ylabel("Features")
Expand Down
3 changes: 2 additions & 1 deletion examples/linear_model/plot_logistic_multinomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
colors = "bry"
for i, color in zip(clf.classes_, colors):
idx = np.where(y == i)
plt.scatter(X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired)
plt.scatter(X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired,
edgecolor='black', s=20)

# Plot the three one-against-all classifiers
xmin, xmax = plt.xlim()
Expand Down
8 changes: 5 additions & 3 deletions examples/linear_model/plot_sgd_iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features. We could
# avoid this ugly slicing by using a two-dim dataset

# we only take the first two features. We could
# avoid this ugly slicing by using a two-dim dataset
X = iris.data[:, :2]
y = iris.target
colors = "bry"

Expand Down Expand Up @@ -56,7 +58,7 @@
for i, color in zip(clf.classes_, colors):
idx = np.where(y == i)
plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i],
cmap=plt.cm.Paired)
cmap=plt.cm.Paired, edgecolor='black', s=20)
plt.title("Decision surface of multi-class SGD")
plt.axis('tight')

Expand Down
3 changes: 2 additions & 1 deletion examples/linear_model/plot_sgd_separating_hyperplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
linestyles = ['dashed', 'solid', 'dashed']
colors = 'k'
plt.contour(X1, X2, Z, levels, colors=colors, linestyles=linestyles)
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired,
edgecolor='black', s=20)

plt.axis('tight')
plt.show()
6 changes: 3 additions & 3 deletions examples/linear_model/plot_sgd_weighted_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
xx, yy = np.meshgrid(np.linspace(-4, 5, 500), np.linspace(-4, 5, 500))
plt.figure()
plt.scatter(X[:, 0], X[:, 1], c=y, s=sample_weight, alpha=0.9,
cmap=plt.cm.bone)
cmap=plt.cm.bone, edgecolor='black')

## fit the unweighted model
# fit the unweighted model
clf = linear_model.SGDClassifier(alpha=0.01, n_iter=100)
clf.fit(X, y)
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
no_weights = plt.contour(xx, yy, Z, levels=[0], linestyles=['solid'])

## fit the weighted model
# fit the weighted model
clf = linear_model.SGDClassifier(alpha=0.01, n_iter=100)
clf.fit(X, y, sample_weight=sample_weight)
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Expand Down
4 changes: 2 additions & 2 deletions examples/mixture/plot_concentration_prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def plot_ellipses(ax, weights, means, covars):
# eigenvector normalization
eig_vals = 2 * np.sqrt(2) * np.sqrt(eig_vals)
ell = mpl.patches.Ellipse(means[n], eig_vals[0], eig_vals[1],
180 + angle)
180 + angle, edgecolor='black')
ell.set_clip_box(ax.bbox)
ell.set_alpha(weights[n])
ell.set_facecolor('#56B4E9')
Expand All @@ -71,7 +71,7 @@ def plot_results(ax1, ax2, estimator, X, y, title, plot_title=False):
ax2.yaxis.grid(True, alpha=0.7)
for k, w in enumerate(estimator.weights_):
ax2.bar(k, w, width=0.9, color='#56B4E9', zorder=3,
align='center')
align='center', edgecolor='black')
ax2.text(k, w + 0.007, "%.1f%%" % (w * 100.),
horizontalalignment='center')
ax2.set_xlim(-.6, 2 * n_components - .4)
Expand Down
7 changes: 5 additions & 2 deletions examples/model_selection/plot_underfitting_overfitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score


def true_fun(X):
return np.cos(1.5 * np.pi * X)

np.random.seed(0)

n_samples = 30
degrees = [1, 4, 15]

true_fun = lambda X: np.cos(1.5 * np.pi * X)
X = np.sort(np.random.rand(n_samples))
y = true_fun(X) + np.random.randn(n_samples) * 0.1

Expand All @@ -57,7 +60,7 @@
X_test = np.linspace(0, 1, 100)
plt.plot(X_test, pipeline.predict(X_test[:, np.newaxis]), label="Model")
plt.plot(X_test, true_fun(X_test), label="True function")
plt.scatter(X, y, label="Samples")
plt.scatter(X, y, edgecolor='b', s=20, label="Samples")
plt.xlabel("x")
plt.ylabel("y")
plt.xlim((0, 1))
Expand Down
9 changes: 6 additions & 3 deletions examples/neighbors/plot_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features. We could
# avoid this ugly slicing by using a two-dim dataset

# we only take the first two features. We could avoid this ugly
# slicing by using a two-dim dataset
X = iris.data[:, :2]
y = iris.target

h = .02 # step size in the mesh
Expand Down Expand Up @@ -46,7 +48,8 @@
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
edgecolor='k', s=20)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("3-Class classification (k = %i, weights = '%s')"
Expand Down
8 changes: 5 additions & 3 deletions examples/neighbors/plot_lof.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
In practice, such informations are generally not available, and taking
n_neighbors=20 appears to work well in general.
"""
print(__doc__)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import LocalOutlierFactor
print(__doc__)

np.random.seed(42)

Expand All @@ -44,8 +44,10 @@
plt.title("Local Outlier Factor (LOF)")
plt.contourf(xx, yy, Z, cmap=plt.cm.Blues_r)

a = plt.scatter(X[:200, 0], X[:200, 1], c='white')
b = plt.scatter(X[200:, 0], X[200:, 1], c='red')
a = plt.scatter(X[:200, 0], X[:200, 1], c='white',
edgecolor='k', s=20)
b = plt.scatter(X[200:, 0], X[200:, 1], c='red',
edgecolor='k', s=20)
plt.axis('tight')
plt.xlim((-5, 5))
plt.ylim((-5, 5))
Expand Down
8 changes: 5 additions & 3 deletions examples/neighbors/plot_nearest_centroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features. We could
# avoid this ugly slicing by using a two-dim dataset
# we only take the first two features. We could avoid this ugly
# slicing by using a two-dim dataset
X = iris.data[:, :2]
y = iris.target

h = .02 # step size in the mesh
Expand Down Expand Up @@ -48,7 +49,8 @@
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
edgecolor='b', s=20)
plt.title("3-Class classification (shrink_threshold=%r)"
% shrinkage)
plt.axis('tight')
Expand Down
5 changes: 3 additions & 2 deletions examples/neural_networks/plot_mlp_alpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@
ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)

# Plot also the training points
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright,
edgecolors='black', s=25)
# and testing points
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright,
alpha=0.6)
alpha=0.6, edgecolors='black', s=25)

ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@

# Plot also the training points
colors = [color_map[y] for y in y_train]
plt.scatter(X[:, 0], X[:, 1], c=colors, cmap=plt.cm.Paired)
plt.scatter(X[:, 0], X[:, 1], c=colors, edgecolors='black')

plt.title(titles[i])

plt.text(.90, 0, "Unlabeled points are colored white")
plt.suptitle("Unlabeled points are colored white", y=0.1)
plt.show()
2 changes: 1 addition & 1 deletion examples/svm/plot_weighted_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def plot_decision_function(classifier, sample_weight, axis, title):
# plot the line, the points, and the nearest vectors to the plane
axis.contourf(xx, yy, Z, alpha=0.75, cmap=plt.cm.bone)
axis.scatter(X[:, 0], X[:, 1], c=y, s=100 * sample_weight, alpha=0.9,
cmap=plt.cm.bone)
cmap=plt.cm.bone, edgecolors='black')

axis.axis('off')
axis.set_title(title)
Expand Down
6 changes: 4 additions & 2 deletions examples/tree/plot_tree_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@

# Plot the results
plt.figure()
plt.scatter(X, y, c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue", label="max_depth=2", linewidth=2)
plt.scatter(X, y, s=20, edgecolor="black",
c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue",
label="max_depth=2", linewidth=2)
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
plt.xlabel("data")
plt.ylabel("target")
Expand Down
15 changes: 10 additions & 5 deletions examples/tree/plot_tree_regression_multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@
# Plot the results
plt.figure()
s = 50
plt.scatter(y[:, 0], y[:, 1], c="navy", s=s, label="data")
plt.scatter(y_1[:, 0], y_1[:, 1], c="cornflowerblue", s=s, label="max_depth=2")
plt.scatter(y_2[:, 0], y_2[:, 1], c="c", s=s, label="max_depth=5")
plt.scatter(y_3[:, 0], y_3[:, 1], c="orange", s=s, label="max_depth=8")
s = 25
plt.scatter(y[:, 0], y[:, 1], c="navy", s=s,
edgecolor="black", label="data")
plt.scatter(y_1[:, 0], y_1[:, 1], c="cornflowerblue", s=s,
edgecolor="black", label="max_depth=2")
plt.scatter(y_2[:, 0], y_2[:, 1], c="red", s=s,
edgecolor="black", label="max_depth=5")
plt.scatter(y_3[:, 0], y_3[:, 1], c="orange", s=s,
edgecolor="black", label="max_depth=8")
plt.xlim([-6, 6])
plt.ylim([-6, 6])
plt.xlabel("target 1")
plt.ylabel("target 2")
plt.title("Multi-output Decision Tree Regression")
plt.legend()
plt.legend(loc="best")
plt.show()

0 comments on commit 6986e9b

Please sign in to comment.