1

New to python but ran into something I don't understand. The following line of code:

diff = features[0:] - test[0]    # <- this is what I don't get

is used thusly:

x = diff[i] 

to return the element-wise difference between features[i] and test[0]. Can anyone point to a language reference for this or explain it? I understand how the result can be developed using "def" or "lambda" but I don't understand the construction.

thanks!

2
  • 2
    This looks like it's using NumPy or something similar. Go read the NumPy tutorial, or the documentation for whatever the type of features is. Commented Aug 31, 2016 at 22:24
  • Nump allows you express basic operations element wise. More here. Commented Aug 31, 2016 at 22:28

2 Answers 2

3

the answer depends on what features[0:] and test[0] evaluate to. if test[0] is a number and features[0:] is a numpy array, then you may be using numpy to subtract a number from each element in a list:

>>> import numpy
>>> array = numpy.array([49, 51, 53, 56])
>>> array - 13
array([36, 38, 40, 43])
Sign up to request clarification or add additional context in comments.

Comments

2

feature appears to be a Numpy array. Numpy arrays 'broadcast' scalar operations to the whole array.

import numpy as np
asd = np.full([10,10], 10, np.int64)
asd /= 5
print asd # prints a 10x10 array of 2s

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.