Reggie_Linear_regression NameError: name 'calculate_all_error' is not defined

Hi !

I receive this (in jupyter notebook) :

‘NameError: name ‘calculate_all_error’ is not defined’

when I run that code :

for index in range(len(datapoints)) :
    point = datapoints[index]
    calculate_all_error(m, b, point)
for i in range(len(all_error)):
    sum_all_error = sum_all_error + all_error[i]
    return sum_all_error

In the block above, I have my function and it seems to work (according to the results I get)

def calculate_error(m, b, point):
    x_point = point[0]
    y_point = point[1]
    difference = get_y(m,b, x_point) - y_point
    distance = abs(difference)
    return distance

and I don’t understand what I have to change…
Thanks for your help !

Are you sure the name of function is correct ? ==> calculate_all_error =/= calculate_error.
If that’s not the case maybe you didn’t run block with function inside it ? Then enter the block with calculate_all_error and press Shift+Enter or run option in menu.


Yes, there was this problem thank you, but also others. I fixed them. Now I have a new problem ^^

possible_ms = [x/10 for x in range(-100, 101)]
possible_bs = [x/10 for x in range (-200, 201)]
points = [(1, 2), (2, 0), (3, 4), (4, 4), (5, 3)]
best_m = 0
best_b = 0
smallest_error = float("inf")
for m in possible_ms:
    for b in possible_bs:
        if calculate_all_error (m, b, points) < smallest_error:
            best_m = m
            best_b = b
            smallest_error = calculate_all_error (m, b, points)         
            return best_m, best_b, smallest_error

It returns
SyntaxError: ‘return’ outside function” wherever I indent return, or
IndentationError: unexpected indent” when I indent it more… and I have no idea of how to resolve this SyntaxError !

SyntaxError: ‘return’ outside function ” --> you can not use return keyword outside functions, it must be inside def function(): block.

1 Like

Aaaah ! Thank you ! It seems obvious now (like always).