Logistic regression

Foundations of Probability in Python

Alexander A. Ramírez M.

CEO @ Synergy Vision

Original data

Hours of study vs score data

Foundations of Probability in Python

New data

Pass or Fail test plot

Foundations of Probability in Python

Where would you draw the line?

Where to classify animation plot

Foundations of Probability in Python

Solution based on probability

Probability calculation with model

Foundations of Probability in Python

The logistic function

Logistic function plot

$$ logistic(t) = logistic(slope*x + intercept) $$

Foundations of Probability in Python

Changing the slope

slope parameter making sigmoid function steeper animation

Foundations of Probability in Python

Changing the intercept

Intercept parameter sigmoid function plot

Foundations of Probability in Python

From data to probability

Probability calculation with logistic model

Foundations of Probability in Python

Outcomes

Logistic model outcome plot

Foundations of Probability in Python

Misclassifications

Probability calculation and miss classifications

Foundations of Probability in Python

Logistic regression

# Import LogisticRegression
from sklearn.linear_model import LogisticRegression
# sklearn logistic model
model = LogisticRegression(C=1e9)
model.fit(hours_of_study, outcomes)
# Get parameters
beta1 = model.coef_[0][0]
beta0 = model.intercept_[0]
# Print parameters
print(beta1, beta0)
(1.3406531235010786, -15.05906237996095)
Foundations of Probability in Python

Predicting outcomes based on hours of study

hours_of_study_test = [[10]]
outcome = model.predict(hours_of_study_test)
print(outcome)
array([False])
Foundations of Probability in Python

Probability calculation

$$ $$

# Put value in an array
value = np.asarray(9).reshape(-1,1)
# Calculate the probability for 9 hours of study
print(model.predict_proba(value)[:,1])
array([0.04773474])
Foundations of Probability in Python

Let's practice!

Foundations of Probability in Python

Preparing Video For Download...