|
15 | 15 | # Andreas Mueller <amueller@ais.uni-bonn.de>
|
16 | 16 | # License: BSD
|
17 | 17 |
|
| 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 | + |
18 | 24 | import numpy as np
|
19 |
| -import matplotlib.pyplot as plt |
20 |
| -from sklearn.semi_supervised import LabelSpreading |
21 | 25 | from sklearn.datasets import make_circles
|
22 | 26 |
|
23 |
| -# generate ring with inner box |
24 | 27 | n_samples = 200
|
25 | 28 | X, y = make_circles(n_samples=n_samples, shuffle=False)
|
26 | 29 | outer, inner = 0, 1
|
27 | 30 | labels = np.full(n_samples, -1.0)
|
28 | 31 | labels[0] = outer
|
29 | 32 | labels[-1] = inner
|
30 | 33 |
|
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 |
35 | 37 |
|
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)) |
41 | 39 | plt.scatter(
|
42 | 40 | X[labels == outer, 0],
|
43 | 41 | X[labels == outer, 1],
|
|
66 | 64 | plt.legend(scatterpoints=1, shadow=False, loc="upper right")
|
67 | 65 | plt.title("Raw data (2 classes=outer and inner)")
|
68 | 66 |
|
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_ |
70 | 80 | output_label_array = np.asarray(output_labels)
|
71 | 81 | outer_numbers = np.where(output_label_array == outer)[0]
|
72 | 82 | inner_numbers = np.where(output_label_array == inner)[0]
|
| 83 | + |
| 84 | +plt.figure(figsize=(4, 4)) |
73 | 85 | plt.scatter(
|
74 | 86 | X[outer_numbers, 0],
|
75 | 87 | X[outer_numbers, 1],
|
|
90 | 102 | )
|
91 | 103 | plt.legend(scatterpoints=1, shadow=False, loc="upper right")
|
92 | 104 | 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) |
95 | 105 | plt.show()
|
0 commit comments