I'm using Codecademy to learn about logistic regression and there are some holes in my understanding of this topic.

import numpy as np

hours_studied = np.array([[ 0],[ 1],[ 2],[ 3],[ 4],[ 5],[ 6],[ 7],[ 8],[ 9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19]])
calculated_coefficients = np.array([[0.20678491]])
intercept = np.array([-1.76125712])

def log_odds(features, coefficients,intercept):
  return np.dot(features,coefficients) + intercept
# z = b0 + b1x1 + b2x2 + ....
# log odds measures how likely it is that the data sample belongs to the positive class

def sigmoid(z):
    denominator = 1 + np.exp(-z)
    return 1/denominator
## In order to map predicted values to probabilities, we use the sigmoid function. 
# The function maps any real value into another value between 0 and 1. 
# In machine learning, we use sigmoid to map predictions to probabilities

# Create predict_class() function here
# features = matrix
# coefficients = vector
def predict_class(features, coefficients, intercept, threshold):
  calculated_log_odds = log_odds(hours_studied, calculated_coefficients, intercept)
  probabilities = sigmoid(calculated_log_odds)
  return np.where(probabilities >= threshold, 1, 0)
# If a value in array_to_check is above threshold, the output is 1. If a value in array_to_check is below threshold, the output is 0.

# Make final classifications on Codecademy University data here
final_results = predict_class(hours_studied, calculated_coefficients, intercept, .5)

hours_studied, calculated_coefficients and intercept were given by codecademy.

I'm not sure how to get these inputs myself to plug into the model and codecademy didn't explain either. Hours_studied I understand.

Maybe could someone help explain what are the possible steps before I define log_odds, sigmoid, etc to get coefficients and intercept?


1 Answer 1


Maybe this part of the code is trying to teach you activities post ML training i.e. prediction
The coefficient and intercept are the parameters of the Model. These are determined by using Training data (Features and Labels) and training process

You follow these steps(Very high level) -
Get data - X , Y
Define model i.e. Logistics Regression
Train Model using the data - Here you get the Coef/Intercept
Predict using the Model

In your work, you are at the last step.
You have a model ready with the parameters and you also got the data i.e. Features(X)
It is as good as calling a function i.e. get_answer(x) and get_answer is your model with it's parameters.

Then you say
if answer > THRESOLD(normally 0.5 but we get the best by trials) it's Class-A else it's Class-B

You may learn Logistics regression from scratch form Blog-machinelearningmastery


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.