ValueError in computing precision using scikit-learn
<pre><code>from sklearn.metrics import precision_score

precision_score(expected, predicted)
</code></pre>

<p>where expected is <code>array([ 4.,  3.])</code></p>

<p>and predicted is <code>array([ 2.,  4.])</code></p>

<p>I get the foll. error: <code>*** ValueError: pos_label=1 is not a valid label: array([ 2.,  3.,  4.])</code></p>

<p>How can this be fixed?</p>

<p>You need the <code>average</code> parameter for <em>multiclass</em> labels.</p>

<p>Else you would need to set <code>pos_label</code> as one of the class labels in both arrays i.e. 2, 3 or 4:</p>

<pre><code>&gt;&gt;&gt; # score for all classes
&gt;&gt;&gt; precision_score(expected, predicted, average=None)
array([ 0.,  0.,  0.])

&gt;&gt;&gt; # score for each class
&gt;&gt;&gt; precision_score(expected, predicted, pos_label=2)
0.0
&gt;&gt;&gt; precision_score(expected, predicted, pos_label=3)
0.0
&gt;&gt;&gt; precision_score(expected, predicted, pos_label=4)
0.0
</code></pre>

<p>Reference:
<a href="http://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_score.html" rel="nofollow"><code>sklearn.metrics.precision_score</code></a></p>

python scikit-learn precision