Skip to content

Commit bf6a187

Browse files
rthNicolasHug
authored andcommitted
MNT Format code with black (#15)
1 parent fba8ffd commit bf6a187

File tree

12 files changed

+262
-174
lines changed

12 files changed

+262
-174
lines changed

.circleci/config.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,23 @@ jobs:
3434
branches:
3535
ignore: gh-pages
3636

37+
lint:
38+
docker:
39+
- image: circleci/python:3.6.1
40+
steps:
41+
- checkout
42+
- run:
43+
command: |
44+
sudo python3 -m pip install black flake8
45+
- run:
46+
command: |
47+
black --check examples sklearn_extra *py
48+
# ensure there is no unused imports with flake8
49+
flake8 --select=F401
50+
3751
workflows:
3852
version: 2
3953
build-doc-and-deploy:
4054
jobs:
4155
- python3
56+
- lint

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import sys
1616
import os
1717

18-
import sphinx_gallery
18+
import sphinx_gallery # noqa
1919
import sphinx_rtd_theme
2020

2121
# If extensions (or modules to document with autodoc) are in another directory,

examples/plot_digits_classification_fastfood.py

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,26 @@
3737
# digit they represent: it is given in the 'target' of the dataset.
3838
for index, (image, label) in enumerate(zip(digits.images, digits.target)):
3939
pl.subplot(2, 4, index + 1)
40-
pl.axis('off')
41-
pl.imshow(image, cmap=pl.cm.gray_r, interpolation='nearest')
42-
pl.title('Training: %i' % label)
40+
pl.axis("off")
41+
pl.imshow(image, cmap=pl.cm.gray_r, interpolation="nearest")
42+
pl.title("Training: %i" % label)
4343
if index > 3:
4444
break
4545

4646
# To apply an classifier on this data, we need to flatten the image, to
4747
# turn the data in a (samples, feature) matrix:
4848
n_samples = len(digits.images)
4949
data = digits.images.reshape((n_samples, -1))
50-
gamma = .001
50+
gamma = 0.001
5151
sigma = np.sqrt(1 / (2 * gamma))
5252
number_of_features_to_generate = 1000
5353
train__idx = range(n_samples // 2)
5454
test__idx = range(n_samples // 2, n_samples)
5555

5656
# map data into featurespace
5757
rbf_transform = Fastfood(
58-
sigma=sigma, n_components=number_of_features_to_generate)
58+
sigma=sigma, n_components=number_of_features_to_generate
59+
)
5960
data_transformed_train = rbf_transform.fit_transform(data[train__idx])
6061
data_transformed_test = rbf_transform.transform(data[test__idx])
6162

@@ -70,38 +71,56 @@
7071

7172
# Run the linear classifier on the mapped data.
7273
linear_classifier_transformation.fit(
73-
data_transformed_train, digits.target[train__idx])
74+
data_transformed_train, digits.target[train__idx]
75+
)
7476

7577
# Now predict the value of the digit on the second half:
7678
expected = digits.target[test__idx]
7779
predicted = classifier.predict(data[test__idx])
7880
predicted_linear = linear_classifier.predict(data[test__idx])
7981
predicted_linear_transformed = linear_classifier_transformation.predict(
80-
data_transformed_test)
82+
data_transformed_test
83+
)
8184

82-
print("Classification report for dual classifier %s:\n%s\n"
83-
% (classifier, metrics.classification_report(expected, predicted)))
84-
print("Classification report for primal linear classifier %s:\n%s\n"
85-
% (linear_classifier,
86-
metrics.classification_report(expected, predicted_linear)))
85+
print(
86+
"Classification report for dual classifier %s:\n%s\n"
87+
% (classifier, metrics.classification_report(expected, predicted))
88+
)
89+
print(
90+
"Classification report for primal linear classifier %s:\n%s\n"
91+
% (
92+
linear_classifier,
93+
metrics.classification_report(expected, predicted_linear),
94+
)
95+
)
8796
print(
8897
"Classification report for primal transformation classifier %s:\n%s\n"
89-
% (linear_classifier_transformation,
90-
metrics.classification_report(expected, predicted_linear_transformed)))
98+
% (
99+
linear_classifier_transformation,
100+
metrics.classification_report(expected, predicted_linear_transformed),
101+
)
102+
)
91103

92-
print("Confusion matrix for dual classifier:\n%s"
93-
% metrics.confusion_matrix(expected, predicted))
94-
print("Confusion matrix for primal linear classifier:\n%s"
95-
% metrics.confusion_matrix(expected, predicted_linear))
96-
print("Confusion matrix for for primal transformation classifier:\n%s"
97-
% metrics.confusion_matrix(expected, predicted_linear_transformed))
104+
print(
105+
"Confusion matrix for dual classifier:\n%s"
106+
% metrics.confusion_matrix(expected, predicted)
107+
)
108+
print(
109+
"Confusion matrix for primal linear classifier:\n%s"
110+
% metrics.confusion_matrix(expected, predicted_linear)
111+
)
112+
print(
113+
"Confusion matrix for for primal transformation classifier:\n%s"
114+
% metrics.confusion_matrix(expected, predicted_linear_transformed)
115+
)
98116

99117
for index, (image, prediction) in enumerate(
100-
zip(digits.images[test__idx], predicted)):
118+
zip(digits.images[test__idx], predicted)
119+
):
101120
pl.subplot(2, 4, index + 4)
102-
pl.axis('off')
103-
pl.imshow(image, cmap=pl.cm.gray_r, interpolation='nearest')
104-
pl.title('Prediction: %i' % prediction)
121+
pl.axis("off")
122+
pl.imshow(image, cmap=pl.cm.gray_r, interpolation="nearest")
123+
pl.title("Prediction: %i" % prediction)
105124
if index > 3:
106125
break
107126

examples/plot_kernel_approximation.py

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,25 @@
6565
# To apply an classifier on this data, we need to flatten the image, to
6666
# turn the data in a (samples, feature) matrix:
6767
n_samples = len(digits.data)
68-
data = digits.data / 16.
68+
data = digits.data / 16.0
6969
data -= data.mean(axis=0)
7070

7171
# We learn the digits on the first half of the digits
72-
data_train, targets_train = data[:n_samples // 2], digits.target[:n_samples // 2]
72+
data_train, targets_train = (
73+
data[: n_samples // 2],
74+
digits.target[: n_samples // 2],
75+
)
7376

7477

7578
# Now predict the value of the digit on the second half:
76-
data_test, targets_test = data[n_samples // 2:], digits.target[n_samples // 2:]
77-
#data_test = scaler.transform(data_test)
79+
data_test, targets_test = (
80+
data[n_samples // 2 :],
81+
digits.target[n_samples // 2 :],
82+
)
83+
# data_test = scaler.transform(data_test)
7884

7985
# fix model parameters:
80-
GAMMA = .2
86+
GAMMA = 0.2
8187
SIGMA = np.sqrt(1 / (2 * GAMMA))
8288

8389
# Create a classifier: a support vector classifier
@@ -86,17 +92,22 @@
8692

8793
# create pipeline from kernel approximation
8894
# and linear svm
89-
feature_map_fastfood = Fastfood(sigma=SIGMA, tradeoff_mem_accuracy='mem', random_state=1)
95+
feature_map_fastfood = Fastfood(
96+
sigma=SIGMA, tradeoff_mem_accuracy="mem", random_state=1
97+
)
9098
feature_map_fourier = RBFSampler(gamma=GAMMA, random_state=1)
9199
feature_map_nystroem = Nystroem(gamma=GAMMA, random_state=1)
92-
fastfood_approx_svm = pipeline.Pipeline([("feature_map", feature_map_fastfood),
93-
("svm", svm.LinearSVC())])
100+
fastfood_approx_svm = pipeline.Pipeline(
101+
[("feature_map", feature_map_fastfood), ("svm", svm.LinearSVC())]
102+
)
94103

95-
fourier_approx_svm = pipeline.Pipeline([("feature_map", feature_map_fourier),
96-
("svm", svm.LinearSVC())])
104+
fourier_approx_svm = pipeline.Pipeline(
105+
[("feature_map", feature_map_fourier), ("svm", svm.LinearSVC())]
106+
)
97107

98-
nystroem_approx_svm = pipeline.Pipeline([("feature_map", feature_map_nystroem),
99-
("svm", svm.LinearSVC())])
108+
nystroem_approx_svm = pipeline.Pipeline(
109+
[("feature_map", feature_map_nystroem), ("svm", svm.LinearSVC())]
110+
)
100111

101112
# fit and predict using linear and kernel svm:
102113

@@ -148,43 +159,64 @@
148159
timescale = plt.subplot(212)
149160

150161
accuracy.plot(sample_sizes, nystroem_scores, label="Nystroem approx. kernel")
151-
timescale.plot(sample_sizes, nystroem_times, '--',
152-
label='Nystroem approx. kernel')
162+
timescale.plot(
163+
sample_sizes, nystroem_times, "--", label="Nystroem approx. kernel"
164+
)
153165

154166
accuracy.plot(sample_sizes, fourier_scores, label="Fourier approx. kernel")
155-
timescale.plot(sample_sizes, fourier_times, '--',
156-
label='Fourier approx. kernel')
167+
timescale.plot(
168+
sample_sizes, fourier_times, "--", label="Fourier approx. kernel"
169+
)
157170

158171
accuracy.plot(sample_sizes, fastfood_scores, label="Fastfood approx. kernel")
159-
timescale.plot(sample_sizes, fastfood_times, '--',
160-
label='Fastfood approx. kernel')
172+
timescale.plot(
173+
sample_sizes, fastfood_times, "--", label="Fastfood approx. kernel"
174+
)
161175

162176
# horizontal lines for exact rbf and linear kernels:
163-
accuracy.plot([sample_sizes[0], sample_sizes[-1]],
164-
[linear_svm_score, linear_svm_score], label="linear svm")
165-
timescale.plot([sample_sizes[0], sample_sizes[-1]],
166-
[linear_svm_time, linear_svm_time], '--', label='linear svm')
167-
168-
accuracy.plot([sample_sizes[0], sample_sizes[-1]],
169-
[kernel_svm_score, kernel_svm_score], label="rbf svm")
170-
timescale.plot([sample_sizes[0], sample_sizes[-1]],
171-
[kernel_svm_time, kernel_svm_time], '--', label='rbf svm')
177+
accuracy.plot(
178+
[sample_sizes[0], sample_sizes[-1]],
179+
[linear_svm_score, linear_svm_score],
180+
label="linear svm",
181+
)
182+
timescale.plot(
183+
[sample_sizes[0], sample_sizes[-1]],
184+
[linear_svm_time, linear_svm_time],
185+
"--",
186+
label="linear svm",
187+
)
188+
189+
accuracy.plot(
190+
[sample_sizes[0], sample_sizes[-1]],
191+
[kernel_svm_score, kernel_svm_score],
192+
label="rbf svm",
193+
)
194+
timescale.plot(
195+
[sample_sizes[0], sample_sizes[-1]],
196+
[kernel_svm_time, kernel_svm_time],
197+
"--",
198+
label="rbf svm",
199+
)
172200

173201
# vertical line for dataset dimensionality = 64
174202
accuracy.plot([64, 64], [0.7, 1], label="n_features")
175203

176204
# legends and labels
177205
accuracy.set_title("Classification accuracy")
178-
timescale.set_title("Training times for dataset size of " + str(n_samples) + " with dimensionality of "
179-
+ str(np.size(data, 1)))
206+
timescale.set_title(
207+
"Training times for dataset size of "
208+
+ str(n_samples)
209+
+ " with dimensionality of "
210+
+ str(np.size(data, 1))
211+
)
180212
accuracy.set_xlim(sample_sizes[0], sample_sizes[-1])
181213
accuracy.set_xticks(())
182214
accuracy.set_ylim(np.min(fourier_scores), 1)
183215
timescale.set_xlabel("Sampling steps = transformed feature dimension")
184216
accuracy.set_ylabel("Classification accuracy")
185217
timescale.set_ylabel("Training time in seconds")
186-
accuracy.legend(loc='best')
187-
timescale.legend(loc='best')
218+
accuracy.legend(loc="best")
219+
timescale.legend(loc="best")
188220

189221
# visualize the decision surface, projected down to the first
190222
# two principal components of the dataset
@@ -203,20 +235,20 @@
203235
flat_grid = grid.reshape(-1, data.shape[1])
204236

205237
# title for the plots
206-
titles = ['SVC with rbf kernel',
207-
'SVC (linear kernel)\n with Fastfood rbf feature map\n'
208-
'n_components=100',
209-
'SVC (linear kernel)\n with Fourier rbf feature map\n'
210-
'n_components=100',
211-
'SVC (linear kernel)\n with Nystroem rbf feature map\n'
212-
'n_components=100']
238+
titles = [
239+
"SVC with rbf kernel",
240+
"SVC (linear kernel)\n with Fastfood rbf feature map\n" "n_components=100",
241+
"SVC (linear kernel)\n with Fourier rbf feature map\n" "n_components=100",
242+
"SVC (linear kernel)\n with Nystroem rbf feature map\n" "n_components=100",
243+
]
213244

214245
plt.tight_layout()
215246
plt.figure(figsize=(12, 5))
216247

217248
# predict and plot
218-
for i, clf in enumerate((kernel_svm, fastfood_approx_svm, nystroem_approx_svm,
219-
fourier_approx_svm)):
249+
for i, clf in enumerate(
250+
(kernel_svm, fastfood_approx_svm, nystroem_approx_svm, fourier_approx_svm)
251+
):
220252
# Plot the decision boundary. For that, we will assign a color to each
221253
# point in the mesh [x_min, m_max]x[y_min, y_max].
222254
plt.subplot(1, 4, i + 1)
@@ -225,7 +257,7 @@
225257
# Put the result into a color plot
226258
Z = Z.reshape(grid.shape[:-1])
227259
plt.contourf(multiples, multiples, Z, cmap=plt.cm.Paired)
228-
plt.axis('off')
260+
plt.axis("off")
229261

230262
# Plot also the training points
231263
plt.scatter(X[:, 0], X[:, 1], c=targets_train, cmap=plt.cm.Paired)

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[build-system]
2+
# build with the oldest numpy that has pre-build wheels for Py3.7 on PyPi
3+
requires = ["setuptools", "wheel", "cython>=0.28", "numpy==1.14.5"]
4+
5+
[tool.black]
6+
line-length = 79

0 commit comments

Comments
 (0)