In weeks 2’s “Optional lab: Python, NumPy and vectorization”, the formula describing the sum of two vectors in paragraph “3.4.4 Vector Vector element-wise operations” may be incorrect as it would result in a scalar rather than another vector.
As the operation says “Element wise”, It won’t result in a scalar value.
Suppose take two vectors a and b
a = np.array([4,5,7])
b = np.array([10,20, 30,])
For addition operation (a+b)
The value 4 in the “a” is being added to 10 in “b”
The value 5 in the “a” is being added to 20 in “b”
The value 7 in the “a” is being added to 30 in “b”
And, the output is also a vector consisting of
[14, 25, 37]
Here is the case Python’s Broadcasting comes into the picture.
Both the arrays are not of the same shape.
scalar a
is being stretched during the arithmetic operation into an array with the same shape as b
.
The above output is the same as the one below
You can learn more about Python’s Broadcasting by referring to the documentation.
https://numpy.org/doc/stable/user/basics.broadcasting.html
If you have any doubt, feel free to ask
Regards,
Ajay
Thanks for taking the time to add your detailed contribution.
I don’t have any problem with the python code, my observation concerns the formula:
which should be something like:
The Operation you are going to perform is the summation operation. Indexing starts from 0 and goes to n-1 in python.
1st iteration over the summation operation: ( a[0]+b[0] )
2nd iteration over the summation operation: ( a[1]+b[1] )
The output is an array consisting of all the individual outputs.
This is happening internally.
a+ b= [a0 + b0, a1 + b1, ....an-1+ bn-1]
Please tell me if it doesn’t make sense.
We will discuss.
Ajay
I’m grateful for your answer however I don’t feel you understand my point because you keep talking about the python code whereas I’m okay with that and I’m mainly highlighting that the formula in 3.4.4 Vector Vector element-wise operations” is in my opinion incorrect because it returns a scalar value.
Hello Cono,
The intention in 3.4.4 is to show that the elements having the same index from a and b get added as follows:
a_0 +b_0, a_1 +b_1, ...,a_{n-1}+b_{n-1}
The summation shown in the notebook is:
a + b =\sum_{i=0}^{n-1} a_i + b_i
= a_0+b_0+a_1+b_1+...+a_{n-1}+b_{n-1} = constant/scalar
which is kind of misleading. So, your point is valid. I will go ahead and report it because it is causing confusion.
When I started to implement the exact formulae, then I got to know that it is returning a scalar value which is strange.
But, the actual idea is that elements are being added based on the index.
Implementation of the actual formula given in the notebook
It is returning a scalar according to the formulae but the main idea behind the element-wise operation is to return a vector.
Regards,
Ajay
I agree with @shanup, this notation is misleading.
The summation implies that all of the ‘i’ pairs are added together. That’s not what an element-wise sum does.