While using python / numpy I came across the following syntax:
np.mean(predicted == docs_test.target)
where the arguments are of type numpy.ndarray
What is the meaning of == here?
Thanks, Jaideep
While using python / numpy I came across the following syntax:
np.mean(predicted == docs_test.target)
where the arguments are of type numpy.ndarray
What is the meaning of == here?
Thanks, Jaideep
Assuming predicted and docs_test.target are two arrays of the same size, this computes the fraction of elements where the two arrays are in exact agreement.
For example,
In [1]: import numpy as np
In [2]: a = np.array([1, 2, 3, 4, 5, 6, 7])
In [3]: b = np.array([1, 3, 7, 4, 5, 0, -10])
In [4]: np.mean(a == b)
Out[4]: 0.42857142857142855
This is telling us that in ~43% of cases (3 out of 7), a and b are in agreement.
In [5]: a == b; Out[5]: array([ True, False, False, True, True, False, False], dtype=bool)From the docs:
Each of ... the comparisons (
==,<,>,<=,>=,!=) is equivalent to the corresponding universal function
In this case the corresponding universal function is numpy.equal:
numpy.equal(x1, x2[, out])Return (
x1 == x2) element-wise.