FAQ: Logistic Regression - Linear Regression Approach

This community-built FAQ covers the “Linear Regression Approach” exercise from the lesson “Logistic Regression”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Machine Learning

FAQs on the exercise Linear Regression Approach

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

When doing offline exercise Reggie’s Linear Regression, I found something interesting.

I think the correct result shoud be like “-10.0, -9.9, -9.8, -9.7, -9.6, -9.5, -9.4, -9.3, …”.
Does anyone know why?

if you use round, yes.

the problem you run into is explained in the python docs:

14. Floating Point Arithmetic: Issues and Limitations — Python 2.7.18 documentation

I agree that you should be getting the numbers without the long tails. I (and it seems many other users) used
i / 10 for i in range (-100,101) while the authors used:
i * 0.1 for i in range…

Yes you can/should read about floating points & why that happened, but I am wondering why the authors don’t correct this as our method finds a lower error AND the list comprehension creates the desired list (-10 to 10 in .1 increments)
Thoughts CA community?

the creator of the course wanted float values for some reason, multiplying by 0.1 achieves this

using i / 10 doesn’t guarantee float numbers across both major python versions. In programming, every approach has its advantages and drawbacks.

But not being aware of Floating Point Arithmetic limitations is also not good.

1 Like

I’m trying to run this calculate errors function over a list of data points in the Reggie’s linear regression exercise.

I seem to be able to create lists that take x and y coordinates out of the list but I can’t get the error to sum correctly.

I think it’s because the variable “line_value” is not iterating.

The expected output for the below code is 0. I get an answer of 12.

Here is my code:

`#Write your calculate_all_error function here
def calculate_all_error(m, b, points):
    x_value = [x[0] for x in points]
    y_value = [y[1] for y in points]
    for i in x_value:
        total_error = 0 
        line_value = m * i + b
        for z in y_value: 
        total_error += abs((line_value - z))
    return total_error

#every point in this dataset lies upon y=x, so the total error should be zero:
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
print(calculate_all_error(1, 0, datapoints))`


Hey there, I am trying to run this code on my own compiler (I use PyCharm) and I downloaded the necessary file, but I still cant run my code, because of the exam file, which must be imported. I downloaded the file, but it still doesnt work.

This is the code:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from exam import hours_studied, passed_exam
from plotter import plot_data

# Create linear regression model
model = LinearRegression()

# Plug sample data into fitted model
sample_x = np.linspace(-16.65, 33.35, 300).reshape(-1,1)
probability = model.predict(sample_x).ravel()

# Function to plot exam data and linear regression curve

# Show the plot

# Define studios and slacker here
slacker = -0.1
studious = 1.1

This is my error:

Traceback (most recent call last):
  File "D:/Python Projekte/script.py", line 5, in <module>
    from exam import hours_studied, passed_exam
ImportError: cannot import name 'hours_studied' from 'exam' (D:\Python Projekte\venv\lib\site-packages\exam\__init__.py)
1 Like