Confusing project in Codeacademy

see this screenshot, in the instruction, it is mentioned to use get_y() to get y value, but how?
really confusion for new like me.

but due to this , it forces me to look for solutions which i didn’t want to.
Is this function related for get_() in def calculate_error(m, b, point): ?

Hi there.

Can you provide a link to the full project please? Your screenshot only presents a small snapshot of the project’s tasks.

I would assume that at some point before then, you’ve either been asked to create a function called get_y() which computes the line equation or it has been done for you and it’s function explained.

The instruction

  1. Use get_y() to get the y-value that x_point would be on the line

is just asking you to use that function, get_y(), with the appropriate parameters for m, x and b to compute the expected value for y.

For example:

x_point = 3    # you are getting this value from the "point" variable, but i'm setting it to 3 for the purposes of this example
gradient = 2   # the "m" term in the equation of a line is its gradient
intercept = 4  # the "b" term in the equation of a line is its intercept, or the point at which it crosses the y-axis

# so, if we know these parameters, instruction #3 would be to call "get_y()" with these parameters:
calc_y_point = get_y(gradient, intercept, x_point)

# subsequently:
print(calc_y_point) # output: 10 (y = mx + b -> (2 * 3) + 4 -> 6 + 4 -> 10)

Thanks for posting the link.

One of the tasks you’re asked to do in that project is to complete the function definition for the function get_y(). I presume that you’ve done this step, so you ought to know what the parameters are for that function and what it returns.

It would seem that I guessed correctly in my previous post as to what the task is expecting of you:

If you’re attempting this project, one presumes that you’re comfortable with how functions work in Python. If you’re not comfortable with defining and calling functions, I would suggest that you re-visit the material which covers this before re-attempting Reggie’s Linear Regression.

Your function, in In [3], is calculating a value for y - but you’re doing it by manually providing an expression (m*x_point + b) where you have previously implemented a function (get_y()) to do that for you…

1 Like

Hi there. Thanks for starting the thread @thiyamsureshsingh and thanks for the explanation @thepitycoder.
I’ve got another question regarding this project… apologies in advance if this question was better put in a different thread.

On this same project I’ve noticed that the solution presents the following code:

def calculate_all_error(m, b, points):
  total_error = 0
  for point in datapoints:
    point_error = calculate_error(m, b, point)
    total_error += point_error
  return total_error

Shouldn’t the third line be for point in points: ?

This actually makes no difference in the final answer as the data points are actually stored on a global variable ‘datapoints’.

Shouldn’t make too much difference in the code here due to datapoints being passed to the function but if datapoints was changed to another list of a different size, there may be problems. I’ve done it slightly differently and might make a bit more sense:

def calculate_all_error(m, b, points):
    error_total = 0
    for i in range(0, len(points)):
        error_total += calculate_error(m, b, points[i])
    return error_total

I’d agree that it probably should be for point in points:. Relying on globals is often bad practice and in this case you have a function argument that does nothing. Unless you have a good reason for using indexes in Python it’s generally better to iterate through the sequence itself rather than indexing it.
for point in points: is the more “pythonic” way to do this.


sir, can you provide me your code for this first function “calculate_error” ?