Index error: invalid index to scalar variable

Hi at all,
i’m a student fo the course" Machine Learning Specialization"
i’m doing the assignment C1w3_Logistic_regression in the lab jeorpjinpzof
The ex UNQ_c3 runs, but running the next box i recieve the error:
" IndexError: invalid index to scalar variable."
how i can fix it?
thanks a lot
Davide

Debugging is part of the job anytime you are writing code, right? And the first law of debugging is “Believe the error message”. If you don’t understand what it is telling you, that is the first problem you need to solve. :nerd_face:

When you get an exception like that, there should be a complete stack backtrace that leads back to the actual line that “threw” (caused the exception). So look carefully at the line that threw. Which variable is being indexed in that line of code? What is the type of that variable? Then you have to work from there. There are two basic possibilities:

  1. It was logically correct to be indexing that variable at that point in the computation, but it fails because the variable is a scalar and it should have been an array or list.
  2. The mistake is that indexing is not required at that point in the code.

So that level of analysis boils down to understanding what your code needs to be doing in order to express the mathematical formulas that you are implementing.

If all the above is too generic to help, then the next step would be for you to show the complete exception trace. Note that we are not allowed to publicly share solution code, even if it is incorrect, but it’s ok to show an exception trace even though that does reveal a few lines of code.

1 Like

first of all, thanks for reply to me so fast, as you can immagine i’m not a developer but despite this I am following the course.
As you know the course use “Jupyter” with a limited possibility of debugging or having more precise logs.in any case, the log says:
“line ----> 5 dj_db, dj_dw = compute_gradient(X_train, y_train, initial_w, initial_b)”
" line —> 42 dj_dw[j] += dj_dw_ij"

thanks a lot in advance

Yes, the Jupyter environment is not as good for debugging as a real development environment, but the simple “add print statements” approach always works. It’s not as sophisticated as an interactive debugger, but it works. Once you have figured out what is wrong, you can either comment out the print statements or delete them.

So if that line labelled 42 is the one that is actually “throwing”, then that says that the variable dj_dw is a scalar. You can verify that by putting this print statement the line before the line that throws:

print(f"type(dj_dw) = {type(dj_dw)}")

What does that show you? Based on the exception, I’m guessing it will tell you that it is a scalar float. Then the next question is why it is the wrong type. Where is it defined?

This is how debugging works: you start from the exception message and then you work backwards one step at a time until you get to the actual cause of the problem.

You say you are “not a developer”, but please realize that MLS requires that you are a reasonably competent python programmer. These courses will not teach you python or teach you how to debug: that’s a prerequisite. We are here to help when you really get stuck or have trouble with any of the new concepts in the courses, but the job of debugging is a fundamental part of programming. I’m retired now from a career as a software engineer and I’ve been writing programs since I was 14. And I can still count on the fingers of one hand the times I wrote a program longer than 2 or 3 lines that worked correctly the first time I wrote it. So debugging is always involved and you can’t just assume it’s somebody else’s job. :nerd_face:

1 Like