C2_W1_Assigment Exercise 2

Good day, I have been trying to get this exercise done but still getting errors not sure why, I called all my previous variables and defined all the functions but now I got this error when I run the unitest at the end of the Exercise 3 in the assignment

# mentor edit: code removed


IndexError Traceback (most recent call last)
File ~/work/w1_unittest.py:290, in test_L_of_omega_array(target_L_of_omega_array)
289 try:
→ 290 assert isclose(result[i], test_case_i[“L_of_omega”], abs_tol=1e-5)
291 successful_cases += 1

IndexError: index 11 is out of bounds for axis 0 with size 11

During handling of the above exception, another exception occurred:

IndexError Traceback (most recent call last)
Cell In [25], line 1
----> 1 w1_unittest.test_L_of_omega_array(L_of_omega_array)

File ~/work/w1_unittest.py:298, in test_L_of_omega_array(target_L_of_omega_array)
291 successful_cases += 1
293 except:
294 failed_cases.append(
295 {
296 “name”: test_case[“name”],
297 “expected”: test_case_i[“L_of_omega”],
→ 298 “got”: result[i],
299 }
300 )
301 print(
302 f"Test case "{failed_cases[-1].get(‘name’)}". Wrong output of L_of_omega_array for omega_array = \n{test_case[‘input’][‘omega_array’]}\nTest for index i = {i}. \n\tExpected: \n{failed_cases[-1].get(‘expected’)}\n\tGot: \n{failed_cases[-1].get(‘got’)}"
303 )
305 if len(failed_cases) == 0:

IndexError: index 11 is out of bounds for axis 0 with size 11

1 Like

You’ve posted in the Machine Learning Specialization course area, but it doesn’t look to me like this is from the MLS course.

Are you maybe attending the Math for Machine Learning Specialization (M4ML)?

1 Like

If I am not confused, it should be the Calculus for Machine Learning, which is the second course from the specialization, is the W1

1 Like

The “Calculus for Machine Learning and Data Science” course is C2 of the M4ML Specialization. The top of the notebook looks like this, if I am correct:

C2 of MLS is about “Advanced Algorithms”.

1 Like

I’ll move the thread to the correct forum area.

The first problem I see is that this part of your code is not correct.
image

It should look like this (notice that the parameters are named differently):
image

Did you modify the function definition, or are you maybe using an obsolete copy of the assignment notebook?

1 Like

These are my variables:

Now that you mentioned, I followed the steps to get the latest version and rebooted my notebook twice, I believe I have already the most recent version of the files, but I am not sure

1 Like

I managed solve it, but know the issue is showing in the Exercise 3 where the length of the array does not match the expected:

# mentor edit: code removed

IndexError Traceback (most recent call last)
File ~/work/w1_unittest.py:290, in test_L_of_omega_array(target_L_of_omega_array)
289 try:
→ 290 assert isclose(result[i], test_case_i[“L_of_omega”], abs_tol=1e-5)
291 successful_cases += 1

IndexError: index 11 is out of bounds for axis 0 with size 11

During handling of the above exception, another exception occurred:

IndexError Traceback (most recent call last)
Cell In [50], line 1
----> 1 w1_unittest.test_L_of_omega_array(L_of_omega_array)

File ~/work/w1_unittest.py:298, in test_L_of_omega_array(target_L_of_omega_array)
291 successful_cases += 1
293 except:
294 failed_cases.append(
295 {
296 “name”: test_case[“name”],
297 “expected”: test_case_i[“L_of_omega”],
→ 298 “got”: result[i],
299 }
300 )
301 print(
302 f"Test case "{failed_cases[-1].get(‘name’)}". Wrong output of L_of_omega_array for omega_array = \n{test_case[‘input’][‘omega_array’]}\nTest for index i = {i}. \n\tExpected: \n{failed_cases[-1].get(‘expected’)}\n\tGot: \n{failed_cases[-1].get(‘got’)}"
303 )
305 if len(failed_cases) == 0:

IndexError: index 11 is out of bounds for axis 0 with size 11

1 Like

I did not ask about your variable names.

I pointed out that your function definition for L_of_omega_array() is incorrect. It is different from what is in the current version of the notebook.

I am curious why your function definition is wrong. There are two possibilities:

  • Maybe you modified the function definition. That would be bad, because you are not supposed to modify anything in the notebook outside of the YOUR CODE HERE areas.
  • You could be using an obsolete copy of the notebook. That is also bad. But there is a way to fix it.

Like I said, I am not sure what version I am currently using, I rebooted the notebook and reset everything to get the latest version of it, I have not modify anything besides the spaces I am allowed to modify, I see what your saying about how the variables are named, but in my notebook the examples are “prices_A” instead “pA”.

I had to change all the variables names from “pA” to “prices_A” and It worked, but I am confused, I had to modify the section that I am not supposed to change (the name variables in the functions), I just wanted to understand when I am supposed to change the current version of the notebook.

I think you have an obsolete copy of the notebook.

I recommend you use the procedure in the M4ML FAQ (Topic 1b) and start over.
Be sure you delete both your notebook and your w1_unittest.py file.

Then, only make changes in the areas marked “START CODE HERE”.

1 Like

in the middle of the code, try changing to L_array = L_of_omega not L_of_omega_array

It seems it worked with the variables names I was using, it was just a confusion in between how I was calling those, thank you so much :slight_smile:

I updated the code post from yesterday, to fix the formatting (If you must post some code, please use the “preformatted text” tag).

Then I marked up the errors here:
image

Then I deleted all the other student code from this thread, because it breaks the Code of Conduct.