2011-08-05 22:36:51 +08:00
|
|
|
"""
|
2011-08-24 00:03:01 +08:00
|
|
|
==============================================
|
|
|
|
|
Label Propagation learning a complex structure
|
|
|
|
|
==============================================
|
2011-08-05 22:36:51 +08:00
|
|
|
|
|
|
|
|
Example of LabelPropagation learning a complex internal structure
|
2011-08-10 07:02:28 +08:00
|
|
|
to demonstrate "manifold learning". The outer circle should be
|
2011-08-08 11:58:34 +08:00
|
|
|
labeled "red" and the inner circle "blue". Because both label groups
|
2011-08-10 07:02:28 +08:00
|
|
|
lie inside their own distinct shape, we can see that the labels
|
2011-08-08 11:58:34 +08:00
|
|
|
propagate correctly around the circle.
|
2011-08-05 22:36:51 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-08-05 22:36:51 +08:00
|
|
|
|
2012-02-02 17:55:32 +08:00
|
|
|
# Authors: Clay Woolam <clay@woolam.org>
|
2012-03-04 07:33:24 +08:00
|
|
|
# Andreas Mueller <amueller@ais.uni-bonn.de>
|
2012-02-02 17:55:32 +08:00
|
|
|
# Licence: BSD
|
|
|
|
|
|
2011-08-05 22:36:51 +08:00
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pl
|
2012-01-23 13:13:56 +08:00
|
|
|
from sklearn.semi_supervised import label_propagation
|
2012-03-04 07:33:24 +08:00
|
|
|
from sklearn.datasets import make_circles
|
2011-08-05 22:36:51 +08:00
|
|
|
|
2011-08-08 11:58:34 +08:00
|
|
|
# generate ring with inner box
|
2012-03-04 21:38:14 +08:00
|
|
|
n_samples = 200
|
2012-04-28 18:04:36 +08:00
|
|
|
X, y = make_circles(n_samples=n_samples, shuffle=False)
|
2012-01-22 06:40:34 +08:00
|
|
|
outer, inner = 0, 1
|
2012-03-04 21:38:14 +08:00
|
|
|
labels = -np.ones(n_samples)
|
|
|
|
|
labels[0] = outer
|
|
|
|
|
labels[-1] = inner
|
2011-08-08 11:58:34 +08:00
|
|
|
|
2011-09-15 10:06:35 +08:00
|
|
|
###############################################################################
|
|
|
|
|
# Learn with LabelSpreading
|
2012-01-22 06:40:34 +08:00
|
|
|
label_spread = label_propagation.LabelSpreading(kernel='knn', alpha=1.0)
|
2012-03-04 07:33:24 +08:00
|
|
|
label_spread.fit(X, labels)
|
2011-08-08 11:58:34 +08:00
|
|
|
|
2011-09-15 10:06:35 +08:00
|
|
|
###############################################################################
|
|
|
|
|
# Plot output labels
|
2011-08-12 15:40:29 +08:00
|
|
|
output_labels = label_spread.transduction_
|
2012-02-02 16:50:07 +08:00
|
|
|
pl.figure(figsize=(8.5, 4))
|
|
|
|
|
pl.subplot(1, 2, 1)
|
2012-04-28 18:04:36 +08:00
|
|
|
plot_outer_labeled, = pl.plot(X[labels == outer, 0],
|
|
|
|
|
X[labels == outer, 1], 'rs')
|
2012-03-04 07:33:24 +08:00
|
|
|
plot_unlabeled, = pl.plot(X[labels == -1, 0], X[labels == -1, 1], 'g.')
|
2012-04-28 18:04:36 +08:00
|
|
|
plot_inner_labeled, = pl.plot(X[labels == inner, 0],
|
|
|
|
|
X[labels == inner, 1], 'bs')
|
2011-09-15 10:06:35 +08:00
|
|
|
pl.legend((plot_outer_labeled, plot_inner_labeled, plot_unlabeled),
|
|
|
|
|
('Outer Labeled', 'Inner Labeled', 'Unlabeled'), 'upper left',
|
|
|
|
|
numpoints=1, shadow=False)
|
2011-08-08 11:58:34 +08:00
|
|
|
pl.title("Raw data (2 classes=red and blue)")
|
|
|
|
|
|
2012-02-02 16:50:07 +08:00
|
|
|
pl.subplot(1, 2, 2)
|
2011-08-10 07:02:28 +08:00
|
|
|
output_label_array = np.asarray(output_labels)
|
2012-03-04 07:33:24 +08:00
|
|
|
outer_numbers = np.where(output_label_array == outer)[0]
|
|
|
|
|
inner_numbers = np.where(output_label_array == inner)[0]
|
|
|
|
|
plot_outer, = pl.plot(X[outer_numbers, 0], X[outer_numbers, 1], 'rs')
|
|
|
|
|
plot_inner, = pl.plot(X[inner_numbers, 0], X[inner_numbers, 1], 'bs')
|
2011-09-15 10:06:35 +08:00
|
|
|
pl.legend((plot_outer, plot_inner), ('Outer Learned', 'Inner Learned'),
|
|
|
|
|
'upper left', numpoints=1, shadow=False)
|
2012-01-22 06:40:34 +08:00
|
|
|
pl.title("Labels learned with Label Spreading (KNN)")
|
2011-08-05 22:36:51 +08:00
|
|
|
|
2012-02-02 16:50:07 +08:00
|
|
|
pl.subplots_adjust(left=0.07, bottom=0.07, right=0.93, top=0.92)
|
2011-08-08 11:58:34 +08:00
|
|
|
pl.show()
|