2

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

1
  • If this question has been answered to your satisfaction, please accept one of the answers by clicking the green checkmark next to the best answer. Commented Apr 1, 2013 at 14:53

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@user2231929 To see what's happening inside: In [5]: a == b; Out[5]: array([ True, False, False, True, True, False, False], dtype=bool)
2

If both predicted and docs_test.target are numpy arrays, then == will return a new array with 1 in place of matching elements and 0 where the elements differ. mean of that array will give you a measure of similarity, basically numberofmatchingelements / lengthofpredictedarray.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.