Skip to content

Commit

Permalink
[MRG] DOC examples with correct notebook style (scikit-learn#9061)
Browse files Browse the repository at this point in the history
* DOC examples with correct notebook style

* Modifications in examples/ to avoid unwanted notebook style

* Remove last notebook style example

* Space formatting to avoid notebook style
  • Loading branch information
plagree authored and raghavrv committed Jun 20, 2017
1 parent 0d5d842 commit 1f6ac72
Show file tree
Hide file tree
Showing 56 changed files with 169 additions and 171 deletions.
12 changes: 6 additions & 6 deletions examples/applications/plot_face_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')


###############################################################################
# #############################################################################
# Download the data, if not already on disk and load it as numpy arrays

lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
Expand All @@ -72,15 +72,15 @@
print("n_classes: %d" % n_classes)


###############################################################################
# #############################################################################
# Split into a training set and a test set using a stratified k fold

# split into a training and testing set
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42)


###############################################################################
# #############################################################################
# Compute a PCA (eigenfaces) on the face dataset (treated as unlabeled
# dataset): unsupervised feature extraction / dimensionality reduction
n_components = 150
Expand All @@ -101,7 +101,7 @@
print("done in %0.3fs" % (time() - t0))


###############################################################################
# #############################################################################
# Train a SVM classification model

print("Fitting the classifier to the training set")
Expand All @@ -115,7 +115,7 @@
print(clf.best_estimator_)


###############################################################################
# #############################################################################
# Quantitative evaluation of the model quality on the test set

print("Predicting people's names on the test set")
Expand All @@ -127,7 +127,7 @@
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))


###############################################################################
# #############################################################################
# Qualitative evaluation of the predictions using matplotlib

def plot_gallery(images, titles, h, w, n_row=3, n_col=4):
Expand Down
8 changes: 4 additions & 4 deletions examples/applications/plot_model_complexity_influence.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
from sklearn.linear_model.stochastic_gradient import SGDClassifier
from sklearn.metrics import hamming_loss

###############################################################################
# #############################################################################
# Routines


# initialize random generator
# Initialize random generator
np.random.seed(0)


Expand Down Expand Up @@ -122,8 +122,8 @@ def _count_nonzero_coefficients(estimator):
a = estimator.coef_.toarray()
return np.count_nonzero(a)

###############################################################################
# main code
# #############################################################################
# Main code
regression_data = generate_data('regression')
classification_data = generate_data('classification', sparse=True)
configurations = [
Expand Down
7 changes: 4 additions & 3 deletions examples/applications/plot_prediction_latency.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,13 @@ def plot_benchmark_throughput(throughputs, configuration):
plt.show()


###############################################################################
# main code
# #############################################################################
# Main code

start_time = time.time()

# benchmark bulk/atomic prediction speed for various regressors
# #############################################################################
# Benchmark bulk/atomic prediction speed for various regressors
configuration = {
'n_train': int(1e3),
'n_test': int(1e2),
Expand Down
10 changes: 5 additions & 5 deletions examples/applications/plot_stock_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
from sklearn import cluster, covariance, manifold


###############################################################################
# #############################################################################
# Retrieve the data from Internet

def quotes_historical_google(symbol, date1, date2):
Expand Down Expand Up @@ -189,7 +189,7 @@ def quotes_historical_google(symbol, date1, date2):
variation = close_prices - open_prices


###############################################################################
# #############################################################################
# Learn a graphical structure from the correlations
edge_model = covariance.GraphLassoCV()

Expand All @@ -199,7 +199,7 @@ def quotes_historical_google(symbol, date1, date2):
X /= X.std(axis=0)
edge_model.fit(X)

###############################################################################
# #############################################################################
# Cluster using affinity propagation

_, labels = cluster.affinity_propagation(edge_model.covariance_)
Expand All @@ -208,7 +208,7 @@ def quotes_historical_google(symbol, date1, date2):
for i in range(n_labels + 1):
print('Cluster %i: %s' % ((i + 1), ', '.join(names[labels == i])))

###############################################################################
# #############################################################################
# Find a low-dimension embedding for visualization: find the best position of
# the nodes (the stocks) on a 2D plane

Expand All @@ -220,7 +220,7 @@ def quotes_historical_google(symbol, date1, date2):

embedding = node_position_model.fit_transform(X.T).T

###############################################################################
# #############################################################################
# Visualization
plt.figure(1, facecolor='w', figsize=(10, 8))
plt.clf()
Expand Down
4 changes: 2 additions & 2 deletions examples/applications/wikipedia_principal_eigenvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

print(__doc__)

###############################################################################
# #############################################################################
# Where to download the data, if not already on disk
redirects_url = "http:https://downloads.dbpedia.org/3.5.1/en/redirects_en.nt.bz2"
redirects_filename = redirects_url.rsplit("/", 1)[1]
Expand All @@ -73,7 +73,7 @@
print()


###############################################################################
# #############################################################################
# Loading the redirect files

memory = Memory(cachedir=".")
Expand Down
2 changes: 1 addition & 1 deletion examples/calibration/plot_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
clf_sigmoid_score = brier_score_loss(y_test, prob_pos_sigmoid, sw_test)
print("With sigmoid calibration: %1.3f" % clf_sigmoid_score)

###############################################################################
# #############################################################################
# Plot the data and the predicted probabilities
plt.figure()
y_unique = np.unique(y)
Expand Down
2 changes: 1 addition & 1 deletion examples/calibration/plot_compare_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
rfc = RandomForestClassifier(n_estimators=100)


###############################################################################
# #############################################################################
# Plot calibration plots

plt.figure(figsize=(10, 10))
Expand Down
13 changes: 6 additions & 7 deletions examples/classification/plot_lda_qda.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class has its own standard deviation with QDA.
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis

###############################################################################
# colormap
# #############################################################################
# Colormap
cmap = colors.LinearSegmentedColormap(
'red_blue_classes',
{'red': [(0, 1, 1), (1, 0.7, 0.7)],
Expand All @@ -30,8 +30,8 @@ class has its own standard deviation with QDA.
plt.cm.register_cmap(cmap=cmap)


###############################################################################
# generate datasets
# #############################################################################
# Generate datasets
def dataset_fixed_cov():
'''Generate 2 Gaussians samples with the same covariance matrix'''
n, dim = 300, 2
Expand All @@ -54,8 +54,8 @@ def dataset_cov():
return X, y


###############################################################################
# plot functions
# #############################################################################
# Plot functions
def plot_data(lda, X, y, y_pred, fig_index):
splot = plt.subplot(2, 2, fig_index)
if fig_index == 1:
Expand Down Expand Up @@ -132,7 +132,6 @@ def plot_qda_cov(qda, splot):
plot_ellipse(splot, qda.means_[0], qda.covariances_[0], 'red')
plot_ellipse(splot, qda.means_[1], qda.covariances_[1], 'blue')

###############################################################################
for i, (X, y) in enumerate([dataset_fixed_cov(), dataset_cov()]):
# Linear Discriminant Analysis
lda = LinearDiscriminantAnalysis(solver="svd", store_covariance=True)
Expand Down
6 changes: 3 additions & 3 deletions examples/cluster/plot_affinity_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs

##############################################################################
# #############################################################################
# Generate sample data
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=300, centers=centers, cluster_std=0.5,
random_state=0)

##############################################################################
# #############################################################################
# Compute Affinity Propagation
af = AffinityPropagation(preference=-50).fit(X)
cluster_centers_indices = af.cluster_centers_indices_
Expand All @@ -39,7 +39,7 @@
print("Silhouette Coefficient: %0.3f"
% metrics.silhouette_score(X, labels, metric='sqeuclidean'))

##############################################################################
# #############################################################################
# Plot result
import matplotlib.pyplot as plt
from itertools import cycle
Expand Down
6 changes: 3 additions & 3 deletions examples/cluster/plot_dbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
from sklearn.preprocessing import StandardScaler


##############################################################################
# #############################################################################
# Generate sample data
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=750, centers=centers, cluster_std=0.4,
random_state=0)

X = StandardScaler().fit_transform(X)

##############################################################################
# #############################################################################
# Compute DBSCAN
db = DBSCAN(eps=0.3, min_samples=10).fit(X)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
Expand All @@ -46,7 +46,7 @@
print("Silhouette Coefficient: %0.3f"
% metrics.silhouette_score(X, labels))

##############################################################################
# #############################################################################
# Plot result
import matplotlib.pyplot as plt

Expand Down
4 changes: 2 additions & 2 deletions examples/cluster/plot_dict_face_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

faces = datasets.fetch_olivetti_faces()

###############################################################################
# #############################################################################
# Learn the dictionary of images

print('Learning the dictionary... ')
Expand Down Expand Up @@ -66,7 +66,7 @@
dt = time.time() - t0
print('done in %.2fs.' % dt)

###############################################################################
# #############################################################################
# Plot the results
plt.figure(figsize=(4.2, 4))
for i, patch in enumerate(kmeans.cluster_centers_):
Expand Down
8 changes: 4 additions & 4 deletions examples/cluster/plot_face_ward_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from sklearn.cluster import AgglomerativeClustering


###############################################################################
# #############################################################################
# Generate data
try: # SciPy >= 0.16 have face in misc
from scipy.misc import face
Expand All @@ -38,11 +38,11 @@

X = np.reshape(face, (-1, 1))

###############################################################################
# #############################################################################
# Define the structure A of the data. Pixels connected to their neighbors.
connectivity = grid_to_graph(*face.shape)

###############################################################################
# #############################################################################
# Compute clustering
print("Compute structured hierarchical clustering...")
st = time.time()
Expand All @@ -55,7 +55,7 @@
print("Number of pixels: ", label.size)
print("Number of clusters: ", np.unique(label).size)

###############################################################################
# #############################################################################
# Plot the results on an image
plt.figure(figsize=(5, 5))
plt.imshow(face, cmap=plt.cm.gray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold

###############################################################################
# #############################################################################
# Generate data
n_samples = 200
size = 40 # image size
Expand All @@ -58,7 +58,7 @@
noise_coef = (linalg.norm(y, 2) / np.exp(snr / 20.)) / linalg.norm(noise, 2)
y += noise_coef * noise # add noise

###############################################################################
# #############################################################################
# Compute the coefs of a Bayesian Ridge with GridSearch
cv = KFold(2) # cross-validation generator for model selection
ridge = BayesianRidge()
Expand Down Expand Up @@ -88,7 +88,7 @@
coef_ = clf.best_estimator_.steps[0][1].inverse_transform(coef_.reshape(1, -1))
coef_selection_ = coef_.reshape(size, size)

###############################################################################
# #############################################################################
# Inverse the transformation to plot the results on an image
plt.close('all')
plt.figure(figsize=(7.3, 2.7))
Expand Down
2 changes: 1 addition & 1 deletion examples/cluster/plot_kmeans_digits.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def bench_k_means(estimator, name, data):
data=data)
print(82 * '_')

###############################################################################
# #############################################################################
# Visualize the results on PCA-reduced data

reduced_data = PCA(n_components=2).fit_transform(data)
Expand Down
6 changes: 3 additions & 3 deletions examples/cluster/plot_mean_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
from sklearn.cluster import MeanShift, estimate_bandwidth
from sklearn.datasets.samples_generator import make_blobs

###############################################################################
# #############################################################################
# Generate sample data
centers = [[1, 1], [-1, -1], [1, -1]]
X, _ = make_blobs(n_samples=10000, centers=centers, cluster_std=0.6)

###############################################################################
# #############################################################################
# Compute clustering with MeanShift

# The following bandwidth can be automatically detected using
Expand All @@ -37,7 +37,7 @@

print("number of estimated clusters : %d" % n_clusters_)

###############################################################################
# #############################################################################
# Plot result
import matplotlib.pyplot as plt
from itertools import cycle
Expand Down
Loading

0 comments on commit 1f6ac72

Please sign in to comment.