Mike Williams
Mike Williams

Reputation: 11

Python language/syntax usage

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

Answers (2)

danyamachine
danyamachine

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

Jules Gagnon-Marchand
Jules Gagnon-Marchand

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

Related Questions