Reputation: 11
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!
Upvotes: 1
Views: 59
Reputation: 1908
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])
Upvotes: 3
Reputation: 3781
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
Upvotes: 2