Calling a lambda with a numpy array
Its all about Overriding operators in numpy
You can learn numpy.arry here
Let us focus on your lamda function for each;
1. numpy array :
arr = numpy.array([1, 2, 3])
type(arr)
scale = lambda x: x * 3
scale(arr)
this takes each element from array
2. normal list:
a =[1,2,3]
type(a)
scale = lambda x: x * 3
scale(a)
this takes full list as x and multiplies the list here itself
numpy.ndarray
overloads the *
operator by defining its own __mul__
method. Likewise for +
, -
, etc. This allows for vector arithmetic.
These are two different objects which behaves differently when you use * operator on them.
In the first case you generate a numpy array. In this case, * operator was overloaded for performing multiplication. i.e. every element will be multiplied by 3.
In the second case you generate a list. In this case the * operator is treated as a repetition operator, and the entire list is repeated 3 times.
code example:
type(np.array([1,2,3]))
type([1, 2, 3])
result:
numpy.ndarray
list