<h4 dir="auto">Description</h4>
<p dir="auto">According to documentation in <a href="http://scikit-learn.org/stable/modules/label_propagation.html" rel="nofollow">http://scikit-learn.org/stable/modules/label_propagation.html</a>, when the alpha parameter,  clamping factor, is 1, the algorithm performs hard clamping, which does not allow the labels to change cluster. However this is not the case in the following case. I copied and slightly changed the example found in <a href="http://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation" rel="nofollow">http://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation</a></p>
<h4 dir="auto">Steps/Code to Reproduce</h4>
<p dir="auto">from sklearn import datasets<br>
from sklearn.semi_supervised import LabelPropagation<br>
import numpy as np</p>
<p dir="auto">np.random.seed(1) # fix random seed</p>
<p dir="auto">label_prop_model = LabelPropagation()<br>
iris = datasets.load_iris()</p>
<p dir="auto">inds = np.random.randint(0, 2,size=len(iris.target))</p>
<p dir="auto">random_unlabeled_points = np.where(inds)<br>
random_labeleled_points = np.where(inds == 0)<br>
labels = np.copy(iris.target)<br>
labels[random_unlabeled_points] = -1<br>
label_prop_model.fit(iris.data, labels)<br>
ypred = label_prop_model.transduction_</p>
<p dir="auto">fracunchanged = sum(iris.target[random_labeleled_points] == ypred[random_labeleled_points])/len(iris.target[random_labeleled_points])<br>
print(fracunchanged)</p>
<h4 dir="auto">Expected Results</h4>
<p dir="auto">If for alpha = 1, the algorithm performs hard clamping, I was expecting the transduction labels of the labelled data to be unchanged and equal to the initial labels, therefore the printed fraction should be 1. Any deviation from 1 indicates something is wrong.</p>
<h4 dir="auto">Actual Results</h4>
<p dir="auto">Actually, I am getting 0.985294117647, indicating some initial labels changed during transduction.</p>
<h4 dir="auto">Versions</h4>
<p dir="auto">sklearn version: '0.18.1'<br>
numpy version: '1.11.2'</p>
<p dir="auto">In some cases the labels don't stay clamped in <code class="notranslate">LabelPropagation</code>.  For example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from sklearn.semi_supervised import LabelPropagation
import numpy as np
lp = LabelPropagation(kernel = 'knn', n_neighbors = 2)
X = np.array([[1.,1.],[1.,0.],[0.,1.]])
y = np.array([1.,0.,-1.])
lp.fit(X,y)
print lp
print y
print lp.transduction_

# Produces:

LabelPropagation(alpha=1, gamma=20, kernel='knn', max_iter=30, n_neighbors=2,
         tol=0.001)
[ 1.  0. -1.]
[ 0.  0.  1.]"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">semi_supervised</span> <span class="pl-k">import</span> <span class="pl-v">LabelPropagation</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">lp</span> <span class="pl-c1">=</span> <span class="pl-v">LabelPropagation</span>(<span class="pl-s1">kernel</span> <span class="pl-c1">=</span> <span class="pl-s">'knn'</span>, <span class="pl-s1">n_neighbors</span> <span class="pl-c1">=</span> <span class="pl-c1">2</span>)
<span class="pl-v">X</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([[<span class="pl-c1">1.</span>,<span class="pl-c1">1.</span>],[<span class="pl-c1">1.</span>,<span class="pl-c1">0.</span>],[<span class="pl-c1">0.</span>,<span class="pl-c1">1.</span>]])
<span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">1.</span>,<span class="pl-c1">0.</span>,<span class="pl-c1">-</span><span class="pl-c1">1.</span>])
<span class="pl-s1">lp</span>.<span class="pl-en">fit</span>(<span class="pl-v">X</span>,<span class="pl-s1">y</span>)
<span class="pl-k">print</span> <span class="pl-s1">lp</span>
<span class="pl-k">print</span> <span class="pl-s1">y</span>
<span class="pl-k">print</span> <span class="pl-s1">lp</span>.<span class="pl-s1">transduction_</span>

<span class="pl-c"># Produces:</span>

<span class="pl-v">LabelPropagation</span>(<span class="pl-s1">alpha</span><span class="pl-c1">=</span><span class="pl-c1">1</span>, <span class="pl-s1">gamma</span><span class="pl-c1">=</span><span class="pl-c1">20</span>, <span class="pl-s1">kernel</span><span class="pl-c1">=</span><span class="pl-s">'knn'</span>, <span class="pl-s1">max_iter</span><span class="pl-c1">=</span><span class="pl-c1">30</span>, <span class="pl-s1">n_neighbors</span><span class="pl-c1">=</span><span class="pl-c1">2</span>,
         <span class="pl-s1">tol</span><span class="pl-c1">=</span><span class="pl-c1">0.001</span>)
[ <span class="pl-c1">1.</span>  <span class="pl-c1">0.</span> <span class="pl-c1">-</span><span class="pl-c1">1.</span>]
[ <span class="pl-c1">0.</span>  <span class="pl-c1">0.</span>  <span class="pl-c1">1.</span>]</pre></div>
<p dir="auto">I think the problem occurs in <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/semi_supervised/label_propagation.py">label_propagation.py</a> at <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/semi_supervised/label_propagation.py#L235">line 235</a> and <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/semi_supervised/label_propagation.py#L249">line 249</a>.  When <code class="notranslate">alpha</code> is equal to 1, <code class="notranslate">y_static</code> is set to 0 in <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/semi_supervised/label_propagation.py#L235">line 235</a>, and then <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/semi_supervised/label_propagation.py#L249">line 249</a> doesn't change <code class="notranslate">self.label_distributions_</code>, whereas it should clamp the values of the labelled data points.</p>
<p dir="auto">My understanding from the <a href="http://scikit-learn.org/stable/modules/label_propagation.html" rel="nofollow">documentation</a> is that for <code class="notranslate">LabelPropagation</code> it is always supposed to do hard clamping, and thus it should be completely independent of <code class="notranslate">alpha</code>.  I'm not actually sure what the intention was here since the same <code class="notranslate">fit</code> method is used for <code class="notranslate">LabelSpreading</code>.</p>