8.19.1.13. sklearn.metrics.precision_score

sklearn.metrics.precision_score(y_true, y_pred, labels=None, pos_label=1, average='weighted')

Compute the precision

The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative.

The best value is 1 and the worst value is 0.

Parameters:

y_true : array, shape = [n_samples]

Ground truth (correct) target values.

y_pred : array, shape = [n_samples]

Estimated targets as returned by a classifier.

labels : array

Integer array of labels.

pos_label : int

In the binary classification case, give the label of the positive class (default is 1). Everything else but pos_label is considered to belong to the negative class. Set to None in the case of multiclass classification.

average : string, [None, ‘micro’, ‘macro’, ‘weighted’ (default)]

In the multiclass classification case, this determines the type of averaging performed on the data.

None:

Do not perform any averaging, return the scores for each class.

'macro':

Average over classes (does not take imbalance into account).

'micro':

Average over instances (takes imbalance into account). This implies that precision == recall == F1.

'weighted':

Average weighted by support (takes imbalance into account). Can result in F-score that is not between precision and recall.

Returns:

precision : float (if average is not None) or array of float, shape = [n_unique_labels]

Precision of the positive class in binary classification or weighted average of the precision of each class for the multiclass task.

Examples

In the binary case:

>>> from sklearn.metrics import precision_score
>>> y_pred = [0, 1, 0, 0]
>>> y_true = [0, 1, 0, 1]
>>> precision_score(y_true, y_pred)
1.0

In the multiclass case:

>>> from sklearn.metrics import precision_score
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> precision_score(y_true, y_pred, average='macro')  
0.22...
>>> precision_score(y_true, y_pred, average='micro')  
0.33...
>>> precision_score(y_true, y_pred, average='weighted')        
0.22...
>>> precision_score(y_true, y_pred, average=None)  
array([ 0.66...,  0.        ,  0.        ])
Previous
Next