You are on page 1of 13

Logistic Regression

Anubha Gupta, PhD.


Professor
SBILab, Dept. of ECE,
IIIT-Delhi, India
Contact: anubha@iiitd.ac.in; Lab: http://sbilab.iiitd.edu.in
Machine Learning in Hindi

Logistic Regression
Machine Learning in Hindi

Let us Try!
Question: Consider the dataset for average sugarcane crop heights in various regions of India over
different cultivation periods to predict whether the crop is ripened or not, depending on their height.
Using logistic regression in code, predict whether a sugarcane crop planted at a particular region with
an average height of 198 cm is ready for harvesting or not [Yes (Y) or No (N)]. Visualize the decision
boundary and print the learned parameters.

Average height of Ripened or not


sugarcane in cm
100 N

110 N

123 N

207 Y

236 Y

You may pause the video, try, and return later for the answer.
Machine Learning in Hindi

Let us Try!
Average height of Ripened or not
sugarcane in cm
100 N

110 N

123 N

207 Y

236 Y
Machine Learning in Hindi

Let us Try!
import numpy as np
from sklearn.linear_model import
LogisticRegression

# Define the dataset


X = np.array([[100], [110],
[123], [207], [236]])
y = np.array([0, 0, 0, 1, 1])

# Create and fit the logistic


regression model
model = LogisticRegression()
model.fit(X, y)
Machine Learning in Hindi

Let us Try!
# Predict the class for x=198
x_test = np.array([[198]])
y_pred = model.predict(x_test)

# Print the predicted class


print("Predicted class for x=198:",
y_pred[0])
Machine Learning in Hindi

Let us Try!
# Print the parameters learned by the
logistic regression model
print("Intercept:", model.intercept_)
print("Coefficients:", model.coef_)
Machine Learning in Hindi

Let us Try!
Question: The dataset contains the number of hours Study Hours Pass or Fail
studied (study hours) by a student for a particular subject
and the result of whether the student passed (class 1) or 39 P
failed (class 0) the exam for that subject. Using the logistic
regression classifier, answer the following questions. 47 P

28 P
a) Calculate the probability of a pass for the student who
studied 30 hours. 15 F
b) At least how many hours must a student study that
his/her passing probability is more than 90%? 24 F

You may pause the video and try.


Machine Learning in Hindi

Let us Try!
Program it and test it- Study Hours Pass or Fail
The model suggested by the optimizer is: 39 P
1
𝑦ො = 𝑝(𝑧) = 47 P
1 + 𝑒 −𝑧
28 P
where 𝑧 = −52 + 2 ∗ (study hours)
15 F
a) Calculate the probability of a pass for the student
who studied 30 hours. 24 F

Here, 𝑧 = −52 + 2 ∗ 30 = 8

𝑝 𝑧 = 0.99
So, if a student studies 30 hours, there is a 99% chance
that the student will pass the exam.
Machine Learning in Hindi

Let us Try!
b) At least how many hours must a student study Study Hours Pass or Fail
that his/her passing probability is more than 90%?
39 P
1
⟹p z = = 90% 47 P
1 + 𝑒 −𝑧
1 28 P
𝑒 −𝑧 =
9
15 F
𝑧 = 2.19 = −52 + 2 ∗ study hours
study hours ~ 27 hours 24 F

So, the student must study at least 27 hours so as to


pass the exam with a probability more than 90%.
Machine Learning in Hindi

Let us Try!
Study Hours Pass or Fail

39 P

47 P

28 P

15 F

10 F
Machine Learning in Hindi

Let us Try!
Study Hours Pass or Fail

39 P

47 P

34 P

15 F

10 F
Machine Learning in Hindi

To Summarize
What we learned in this module is as follows:

We worked out two examples using logistic regression.

You might also like