FAQ: Logistic Regression - Scikit-Learn

This community-built FAQ covers the “Scikit-Learn” 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 Scikit-Learn

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!

Can you please explain when to use .predict() vs. .predict_proba() in question 4 and question 6? What keywords should I see in the questions to help me know when to use the correct method?

the keyword you are looking for here is ‘probability’.

Use model to predict the probability that each student will pass the final exam, and save the probabilities to passed_predictions.

Since .predict() will only give you 0 or 1 (pass or no pass), you can’t extract the probability information from it, so using predict_proba would be better.

.predict_proba() will output the probability for 0 and for 1, side by side… like this:

[0.92251808 0.07748192]
 [0.92251808 0.07748192]
 [0.86790976 0.13209024]

for the first and second line, it means that the probability for 0 is 92% and for 1 is 8% (rounded values);
third line, 0 = 87%, 1 = 13%… and so on.

2 Likes

Excellent. Thank you so much for clarifying this topic.

I wrote the code in Steps 1 and 2 as follows:

model = LogisticRegression()
model.fit(hours_studied_scaled, passed_exam)

However, the following message is displayed and I didn’t get passed Step 2.

Did you use .fit() to train the model on hours_studied_scaled as the training data and passed_exam as the labels?

Did I make a mistake? Or should this be reported as a bug?

After displaying the solution with the “View Solution” button, copying the code, resetting the exercise with the “Reset Exercise (alt + g)” button, and pasted the copied code. Still I didn’t pass Step 2. It must be a bug, so I reported it.

Per the lesson, with our own data we’ll need to normalize the data using Regularization. Will that be taught here? If not, how do we learn when and how to do that?

Logistic Regression requires normalized feature data due to a technique called Regularization that it uses under the hood. Regularization is out of the scope of this lesson,

You can see the Normalization topic in the Linear Regression excercises.

Evaluating and Improving Your Model | Codecademy

They used the Z-score Normalization. I don’t know if there is a sklearn function so instead I created the following one:

def z_score(array):
mean = np.mean(array)
sd = np.std(array)
z_normal = (array - mean) / sd
return z_normal

The code is correct and worked perfectly for me. maybe the bug you reported was corrected. thanks.