8000 DOC Update plot_label_propagation_structure.py to notebook style (#22… · scikit-learn/scikit-learn@e7c7104 · GitHub
[go: up one dir, main page]

Skip to content

Commit e7c7104

Browse files
zempleniglemaitrejeremiedbb
authored
DOC Update plot_label_propagation_structure.py to notebook style (#22726)
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
1 parent 926df1b commit e7c7104

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

examples/semi_supervised/plot_label_propagation_structure.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,27 @@
1515
# Andreas Mueller <amueller@ais.uni-bonn.de>
1616
# License: BSD
1717

18+
# %%
19+
# We generate a dataset with two concentric circles. In addition, a label
20+
# is associated with each sample of the dataset that is: 0 (belonging to
21+
# the outer circle), 1 (belonging to the inner circle), and -1 (unknown).
22+
# Here, all labels but two are tagged as unknown.
23+
1824
import numpy as np
19-
import matplotlib.pyplot as plt
20-
from sklearn.semi_supervised import LabelSpreading
2125
from sklearn.datasets import make_circles
2226

23-
# generate ring with inner box
2427
n_samples = 200
2528
X, y = make_circles(n_samples=n_samples, shuffle=False)
2629
outer, inner = 0, 1
2730
labels = np.full(n_samples, -1.0)
2831
labels[0] = outer
2932
labels[-1] = inner
3033

31-
# #############################################################################
32-
# Learn with LabelSpreading
33-
label_spread = LabelSpreading(kernel="knn", alpha=0.8)
34-
label_spread.fit(X, labels)
34+
# %%
35+
# Plot raw data
36+
import matplotlib.pyplot as plt
3537

36-
# #############################################################################
37-
# Plot output labels
38-
output_labels = label_spread.transduction_
39-
plt.figure(figsize=(8.5, 4))
40-
plt.subplot(1, 2, 1)
38+
plt.figure(figsize=(4, 4))
4139
plt.scatter(
4240
X[labels == outer, 0],
4341
X[labels == outer, 1],
@@ -66,10 +64,24 @@
6664
plt.legend(scatterpoints=1, shadow=False, loc="upper right")
6765
plt.title("Raw data (2 classes=outer and inner)")
6866

69-
plt.subplot(1, 2, 2)
67+
# %%
68+
#
69+
# The aim of :class:`~sklearn.semi_supervised.LabelSpreading` is to associate
70+
# a label to sample where the label is initially unknown.
71+
from sklearn.semi_supervised import LabelSpreading
72+
73+
label_spread = LabelSpreading(kernel="knn", alpha=0.8)
74+
label_spread.fit(X, labels)
75+
76+
# %%
77+
# Now, we can check which labels have been associated with each sample
78+
# when the label was unknown.
79+
output_labels = label_spread.transduction_
7080
output_label_array = np.asarray(output_labels)
7181
outer_numbers = np.where(output_label_array == outer)[0]
7282
inner_numbers = np.where(output_label_array == inner)[0]
83+
84+
plt.figure(figsize=(4, 4))
7385
plt.scatter(
7486
X[outer_numbers, 0],
7587
X[outer_numbers, 1],
@@ -90,6 +102,4 @@
90102
)
91103
plt.legend(scatterpoints=1, shadow=False, loc="upper right")
92104
plt.title("Labels learned with Label Spreading (KNN)")
93-
94-
plt.subplots_adjust(left=0.07, bottom=0.07, right=0.93, top=0.92)
95105
plt.show()

0 commit comments

Comments
 (0)
0