Skip to content

Commit db127bd

Browse files
lesteverth
authored andcommitted
[MRG+1] Support minimal dependencies in examples (scikit-learn#10351)
* Support minimal dependencies in examples * bins="auto" only supported for numpy >= 1.11 * viridis is default cmap in matplotlib 2 * Use signature instead of viridis cmap
1 parent bb4cdde commit db127bd

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

.circleci/config.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ jobs:
88
- MINICONDA_PATH: ~/miniconda
99
- CONDA_ENV_NAME: testenv
1010
- PYTHON_VERSION: 3
11-
- MATPLOTLIB_VERSION: "*"
1211
steps:
1312
- checkout
1413
- run: ./build_tools/circle/checkout_merge_commit.sh
@@ -42,7 +41,7 @@ jobs:
4241
- MINICONDA_PATH: ~/miniconda
4342
- CONDA_ENV_NAME: testenv
4443
- PYTHON_VERSION: 2
45-
- MATPLOTLIB_VERSION: "*"
44+
- MATPLOTLIB_VERSION: "1.3"
4645
steps:
4746
- checkout
4847
- run: ./build_tools/circle/checkout_merge_commit.sh

examples/manifold/plot_t_sne_perplexity.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
X, color = datasets.samples_generator.make_s_curve(n_samples, random_state=0)
7373

7474
ax = subplots[1][0]
75-
ax.scatter(X[:, 0], X[:, 2], c=color, cmap=plt.cm.viridis)
75+
ax.scatter(X[:, 0], X[:, 2], c=color)
7676
ax.xaxis.set_major_formatter(NullFormatter())
7777
ax.yaxis.set_major_formatter(NullFormatter())
7878

@@ -87,7 +87,7 @@
8787
print("S-curve, perplexity=%d in %.2g sec" % (perplexity, t1 - t0))
8888

8989
ax.set_title("Perplexity=%d" % perplexity)
90-
ax.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.viridis)
90+
ax.scatter(Y[:, 0], Y[:, 1], c=color)
9191
ax.xaxis.set_major_formatter(NullFormatter())
9292
ax.yaxis.set_major_formatter(NullFormatter())
9393
ax.axis('tight')
@@ -102,7 +102,7 @@
102102
])
103103
color = xx.ravel()
104104
ax = subplots[2][0]
105-
ax.scatter(X[:, 0], X[:, 1], c=color, cmap=plt.cm.viridis)
105+
ax.scatter(X[:, 0], X[:, 1], c=color)
106106
ax.xaxis.set_major_formatter(NullFormatter())
107107
ax.yaxis.set_major_formatter(NullFormatter())
108108

@@ -117,7 +117,7 @@
117117
print("uniform grid, perplexity=%d in %.2g sec" % (perplexity, t1 - t0))
118118

119119
ax.set_title("Perplexity=%d" % perplexity)
120-
ax.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.viridis)
120+
ax.scatter(Y[:, 0], Y[:, 1], c=color)
121121
ax.xaxis.set_major_formatter(NullFormatter())
122122
ax.yaxis.set_major_formatter(NullFormatter())
123123
ax.axis('tight')

examples/model_selection/plot_precision_recall.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,17 @@
137137
# ................................
138138
from sklearn.metrics import precision_recall_curve
139139
import matplotlib.pyplot as plt
140+
from sklearn.externals.funcsigs import signature
140141

141142
precision, recall, _ = precision_recall_curve(y_test, y_score)
142143

144+
# In matplotlib < 1.5, plt.fill_between does not have a 'step' argument
145+
step_kwargs = ({'step': 'post'}
146+
if 'step' in signature(plt.fill_between).parameters
147+
else {})
143148
plt.step(recall, precision, color='b', alpha=0.2,
144149
where='post')
145-
plt.fill_between(recall, precision, step='post', alpha=0.2,
146-
color='b')
150+
plt.fill_between(recall, precision, alpha=0.2, color='b', **step_kwargs)
147151

148152
plt.xlabel('Recall')
149153
plt.ylabel('Precision')
@@ -212,8 +216,8 @@
212216
plt.figure()
213217
plt.step(recall['micro'], precision['micro'], color='b', alpha=0.2,
214218
where='post')
215-
plt.fill_between(recall["micro"], precision["micro"], step='post', alpha=0.2,
216-
color='b')
219+
plt.fill_between(recall["micro"], precision["micro"], alpha=0.2, color='b',
220+
**step_kwargs)
217221

218222
plt.xlabel('Recall')
219223
plt.ylabel('Precision')

examples/plot_kernel_ridge_regression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
X = 5 * rng.rand(10000, 1)
121121
y = np.sin(X).ravel()
122122
y[::5] += 3 * (0.5 - rng.rand(X.shape[0] // 5))
123-
sizes = np.logspace(1, 4, 7, dtype=np.int)
123+
sizes = np.logspace(1, 4, 7).astype(np.int)
124124
for name, estimator in {"KRR": KernelRidge(kernel='rbf', alpha=0.1,
125125
gamma=10),
126126
"SVR": SVR(kernel='rbf', C=1e1, gamma=10)}.items():

examples/preprocessing/plot_all_scaling.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
# scale the output between 0 and 1 for the colorbar
103103
y = minmax_scale(y_full)
104104

105+
# plasma does not exist in matplotlib < 1.5
106+
cmap = getattr(cm, 'plasma_r', cm.hot_r)
105107

106108
def create_axes(title, figsize=(16, 6)):
107109
fig = plt.figure(figsize=figsize)
@@ -153,7 +155,7 @@ def plot_distribution(axes, X, y, hist_nbins=50, title="",
153155
ax.set_ylabel(x1_label)
154156

155157
# The scatter plot
156-
colors = cm.plasma_r(y)
158+
colors = cmap(y)
157159
ax.scatter(X[:, 0], X[:, 1], alpha=0.5, marker='o', s=5, lw=0, c=colors)
158160

159161
# Removing the top and the right spine for aesthetics
@@ -209,7 +211,7 @@ def make_plot(item_idx):
209211
title="Zoom-in")
210212

211213
norm = mpl.colors.Normalize(y_full.min(), y_full.max())
212-
mpl.colorbar.ColorbarBase(ax_colorbar, cmap=cm.plasma_r,
214+
mpl.colorbar.ColorbarBase(ax_colorbar, cmap=cmap,
213215
norm=norm, orientation='vertical',
214216
label='Color mapping for values of y')
215217

examples/preprocessing/plot_transformed_target.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ def exp_transform(x):
6363

6464
f, (ax0, ax1) = plt.subplots(1, 2)
6565

66-
ax0.hist(y, bins='auto', normed=True)
66+
ax0.hist(y, bins=100, normed=True)
6767
ax0.set_xlim([0, 2000])
6868
ax0.set_ylabel('Probability')
6969
ax0.set_xlabel('Target')
7070
ax0.set_title('Target distribution')
7171

72-
ax1.hist(y_trans, bins='auto', normed=True)
72+
ax1.hist(y_trans, bins=100, normed=True)
7373
ax1.set_ylabel('Probability')
7474
ax1.set_xlabel('Target')
7575
ax1.set_title('Transformed target distribution')
@@ -148,12 +148,12 @@ def exp_transform(x):
148148

149149
f, (ax0, ax1) = plt.subplots(1, 2)
150150

151-
ax0.hist(y, bins='auto', normed=True)
151+
ax0.hist(y, bins=100, normed=True)
152152
ax0.set_ylabel('Probability')
153153
ax0.set_xlabel('Target')
154154
ax0.set_title('Target distribution')
155155

156-
ax1.hist(y_trans, bins='auto', normed=True)
156+
ax1.hist(y_trans, bins=100, normed=True)
157157
ax1.set_ylabel('Probability')
158158
ax1.set_xlabel('Target')
159159
ax1.set_title('Transformed target distribution')

0 commit comments

Comments
 (0)