Hi there, would appreciate any thoughts/help from someone knowledgeable with the internal logic structure in Python. Any thoughts?
Wrote the following code:
“”
for i in range(m):
f_wb = w*x[i]+b
dj_dw_i = (f_wb-y[i])*x[i]
dj_db_i = f_wb-y[i]
dj_dw+= dj_dw_i
dj_db+= dj_db_i
dj_dw =dj_dw/m
dj_db =dj_db/m
“”
…and got wrong results. Nevertheless, when I took out of the loop the last two command lines (as below), the code executed correctly. Any help/thoughts on why this happened please?
Code as written divides by m each iteration. You probably want to just divide by m once only to get an average. (I assume the lack of indentation is an artifact of pasting the code in here, not what it looks like in your editor)
You will definitely get different results depending on whether or not the lines with /m are indented, or grouped within the loop. If you’re new to Python (my inference) you might find the links