C3_W2_Collaborative_RecSys_Assignment Square root

please help me to complete this code


As it said in the sq_dist function documentation all you need is to calculate the squared distance between two vectors.

For example if you have a vector a as [a_1, a_2, a_3, ..., a_n] and vector b as [b_1, b_2, b_3, ..., b_n] with same dimension os vector a it’s easy to compute the distance between them as follows

\lVert b - a \rVert = \sqrt{(b_1 - a_1)^2 + (b_2 - a_2)^2 + (b_3 - a_3)^2 + ... + (b_n - a_n)^2 + }

which is very simple to do using NumPy all you need is to get the difference between them, square the result, sum it, and get the square root of the result.

d= np.sqrt(a-b)
doesn’t seem to work for me, anybody else have this same issue?
The hints are also different that what’s listed:
While a summation is often an indication a for loop should be used, here the subtraction can be element-wise in one statement. Further, you can utilized np.square to square, element-wise, the result of the subtraction. np.sum can be used to sum the squared elements.


The Lab is asking for the Squared Distance.
Please see the following section from the lab notebook.


Remember that you want to compute the the square root of the difference (squared) not the the square root of the difference alone. they are not the same in this case.

@Moaz_Elesawey thanks.
@SamReiswig I understand the equation, but how do you write that function in code form?

You might want to check some of the numpy documentation.

It’s wonderful that you now understand the concept and for the coding part np.sum() and np.square() functions help you. All you need to do is first apply the square on the difference and the sum no need to square root here. To be more precise, the inner bracket is a square function on difference and the outer bracket is a sum function.


@SamReiswig @VIVEK_PATEL1
I understand now, thanks!