5

I am trying to find out operation applied on list. I have list/array name predictions and and executing following set of instruction.

predictions[predictions < 1e-10] = 1e-10

This code snippet is from a Udacity Machine Learning assignment that uses Numpy.

It was used in the following manner:

def logprob(predictions, labels):
    """Log-probability of the true labels in a predicted batch."""
    predictions[predictions < 1e-10] = 1e-10
    return np.sum(np.multiply(labels, -np.log(predictions))) / labels.shape[0]

As pointed out by @MosesKoledoye and various others, it is actually a Numpy array. (Numpy is a Python library)

What does this line do?

ayhan
  • 70,170
  • 20
  • 182
  • 203
Taivanbat Badamdorj
  • 867
  • 1
  • 10
  • 24
  • 7
    Most probably not a list but a numpy array – Moses Koledoye Jul 25 '16 at 08:54
  • 2
    Just to make clear what `predictions` really is: Please provide a snippet showing how `predictions` is defined and provide the output of `type(predictions)`. – albert Jul 25 '16 at 08:56
  • 1
    Please clarify (in the body of your question, not in a comment) whether `predictions` is a standard Python list, or a Numpy array, or some other kind of data structure. – PM 2Ring Jul 25 '16 at 09:15
  • @MosesKoledoye is right. Thank you for the answer – Taivanbat Badamdorj Jul 25 '16 at 12:10
  • The proposed duplicate, http://stackoverflow.com/questions/36603042/what-does-xx-2-0-mean-in-python, focuses on possible meanings with Python lists. But it is clear from context and the accepted answer that this is a `numpy` array. That other question is useful cross reference, but not a duplicate. – hpaulj Jul 25 '16 at 16:24

2 Answers2

4

As pointed out by @MosesKoledoye, predictions is most likely a numpy array.

A boolean array would then be generated using predictions < 1e-10. At all indices where the boolean array set by the condition is True, the value will be changed to 1e-10, ie. 10-10.

Example:

  >>> a = np.array([1,2,3,4,5]) #define array
  >>> a < 3 #define boolean array through condition
  array([ True,  True, False, False, False], dtype=bool)

  >>> a[a<3]  #select elements using boolean array
  array([1, 2])

  >>> a[a<3] = -1  #change value of elements which fit condition
  >>> a 
  array([-1, -1,  3,  4,  5])

The reason this might be done in the code could be to prevent division by zero or to prevent negative numbers messing up things by instead inserting a very small number.

M.T
  • 4,917
  • 4
  • 33
  • 52
1

All elements of the array, for which the condition (element < 1e-10) is true, are set to 1e-10. Practically you are setting a minimum value.

Nikolas Rieble
  • 2,416
  • 20
  • 43
  • This is not what will happen. You compare a list to a float, in the bracket, which creates a type error =/ – HolyDanna Jul 25 '16 at 09:02
  • 1
    @HolyDanna this will work with `numpy` arrays, not vanilla lists – jonrsharpe Jul 25 '16 at 09:04
  • @jonrsharpe The questions says that `predictions` is a list, not an array. – HolyDanna Jul 25 '16 at 09:07
  • @HolyDanna it does say that, but is unlikely to be correct. If it is a list, the answer is indeed that it will cause a TypeError, but in that case I doubt the OP would be asking the question. – jonrsharpe Jul 25 '16 at 09:09