In [2]:
from sklearn.semi_supervised import LabelPropagation
from pprint import pprint
import scipy
import scipy.sparse

import joblib

import sklearn
import sklearn.datasets
import sklearn.cross_validation

import sys
from time import time

import numpy as np
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [52]:
circles = sklearn.datasets.make_circles(shuffle=False)
circles[0].shape


Out[52]:
(100, 2)

In [129]:
x0 = [circles[0][0,0],circles[0][0,1]]
x50 = [circles[0][50,0],circles[0][50,1]]
scatter(circles[0][1:50,0],circles[0][1:50,1], color='red')
scatter(circles[0][51:,0],circles[0][51:,1], color='blue')

scatter(x0[0],x0[1] ,color='orange')
scatter(x50[0],x50[1] ,color='cyan')


Out[129]:
<matplotlib.collections.PathCollection at 0x10c3dec10>

In [94]:
y = -np.ones(100)
y[0] = 0
y[50] = 1
plot(y)


Out[94]:
[<matplotlib.lines.Line2D at 0x10b07d6d0>]

In [131]:
l = np.hstack((np.zeros(50),np.ones(50)))
l.shape
print l[0],l[49],l[50],l[99]
print y[0],y[49],y[50],y[99]
print l.shape


0.0 0.0 1.0 1.0
0.0 -1.0 1.0 -1.0
(100,)

In [166]:
label_prop_model = LabelPropagation(kernel='knn', n_neighbors=1)
ssl = label_prop_model.fit(circles[0],l)
print ssl.predict(x0), ssl.predict(x50)
print ssl.score(circles[0],l)


[ 0.] [ 1.]
1.0

In [164]:
label_prop_model = LabelPropagation(kernel='rbf', gamma=100)
ssl = label_prop_model.fit(circles[0],l)
print ssl.predict(x0), ssl.predict(x50)
print ssl.score(circles[0],l)


[ 0.] [ 1.]
1.0

In [167]:
moons = sklearn.datasets.make_moons(shuffle=False)
moons[0].shape


Out[167]:
(100, 2)

In [168]:
x0 = [moons[0][0,0],moons[0][0,1]]
x50 = [moons[0][50,0],moons[0][50,1]]
scatter(moons[0][1:50,0],moons[0][1:50,1], color='red')
scatter(moons[0][51:,0],moons[0][51:,1], color='blue')

scatter(x0[0],x0[1] ,color='orange')
scatter(x50[0],x50[1] ,color='cyan')


Out[168]:
<matplotlib.collections.PathCollection at 0x10c59f7d0>

In [174]:
label_prop_model = LabelPropagation(kernel='knn', n_neighbors=1)
ssl = label_prop_model.fit(moons[0],l)
print ssl.predict(x0), ssl.predict(x50)
print ssl.score(moons[0],l)


[ 0.] [ 1.]
1.0

In [177]:
label_prop_model = LabelPropagation(kernel='rbf', gamma=100)
ssl = label_prop_model.fit(circles[0],l)
print ssl.predict(x0), ssl.predict(x50)
print ssl.score(circles[0],l)


[ 0.] [ 1.]
1.0

In [ ]: