C3_W2_RecSysNN_Assignment_UNQ_C2

Hello All,
I am working on creating a solution for the squared distance between two vectors (a,b). I have coded the structure of what I believe will solve this matter using a single for loop within a range of (a.shape): and have initialized my return variable d=0. Additionally, I have used the += operator to update the values between my two vectors (a,b) within a single line of code. However, when I test this code within the given unit test, I get the last of the three expected outputs correct. Any clues or suggestion are appreciated!

1 Like

That’s likely an area of concern.

What do you get if you print the a.shape values? It might be instructive.

1 Like

Hello TMosh,
Thanks for getting back to me…I was working with Raymond Kwok on this issue, but I think he and I are on different time zones. I am in the U.S., and I believe that he is in Hong Kong 12hrs apart from me.

Here’s my code and the expected output.

{mentor edit: code removed}

1 Like

This is not a good method.

Your function doesn’t define values for a1, a2, a3, b1, b2, or b3.
Which means you’re using those as global variables.

No wonder the function doesn’t work for any other data.

1 Like

Inside the function, your code should only use the variables a and b.

You don’t need to use a for-loop. Numpy functions will automatically apply an operation to every element of a list.

For example:
image

Note: This is only an example of how to compute the sum of the squares of a vector. It is not a complete solution for this function. It is only an example of using np.sum() and squaring the elements of a vector.

1 Like

Ok, I had it like that before and I was getting the correct value for item 3 of the expected output but not for outputs 1 & 2 .

1 Like

Hey TMosh,

I retested your solution, but it does not return an output.

{mentor edit: code removed}

1 Like

Your print() and return statements have incorrect indentation.

1 Like

Hello TMosh,
I revamped my code within test scenarios as follows but the output results are different.
First Pass
{mentor edit: code removed}

Second Pass
{mentor edit: code removed}

1 Like

Several mistakes there:

  • Don’t define the ‘a’ variable inside the function. It’s passed to the function as a parameter.

  • Your code is only using the ‘a’ variable. What about ‘b’?

  • Your code is supposed to compute the sum of the squares of the difference between a and b.

1 Like

Hello TMosh,
I revisited the code and your recommendations. However, the I was not getting an output. Additionally, I was also working with Raymond as he advised me that a for loop was recommended, although you advised me that a for loop was not needed. Moreover, I found myself waiting on follow-up messaging from Raymond, but he elected to close our chat because he thought that I refused to use his guided template which I include here but it does not work. What does work is the one line of code that I created which gives me the first and last of the expected outputs. Let me know if you can assist or if you are not available.

{mentor edit: code removed}


Here is the equation you’re supposed to implement:
image

Have you done that?

image

Also, please don’t post your code on the forum. That’s not allowed by the Code of Conduct.

Hey TMosh,

Thanks for getting back to me that worked and now I am getting the expected output.

Thanks Again!

1 Like

image

I’m confused; the hint suggests using np.sum and np.square. Thus, according to that equation, the answer is either np.square(np.sum(arr1 - arr2)) or np.sum(arr1-arr2)**2. I don’t know why, but the expected output and my values are wrong.

What am I missing?

The python builtin **2 operator is not exactly the same as using the numpy np.square() function.

I recommend you try both, compare the results, both numerically and the data types.