You are on page 1of 9

Machine Learning

19ME 3220
III/IV B.Tech
Odd Sem
for the Academic Year 2021-21

Sesion-25
Logistic Regression
S. Ramesh Kumar
Course Co-ordinator
Logistic Regression using Python
 Logistic Regression is a supervised learning
classification algorithm used to predict the
probability of a target variable.
 The nature of target or dependent variable is
dichotomous, which means there would be only two
possible classes.

 In logistic regression,
the dependent variable
is a binary variable that
contains data coded as
1 (yes, success, etc.) or
0 (no, failure, etc.).
Logistic Regression using Python

Logistic Regression is much similar to the Linear Regression.


Linear Regression is used for solving Regression problems,
whereas Logistic regression is used for solving the
classification problems.

In Logistic regression, instead of fitting a regression line, we fit


an "S" shaped logistic function, which predicts two maximum
values (0 or 1).
Logistic Regression using Python
Logistic Function (Sigmoid Function):
The value of the logistic regression must be between 0 and 1,
which cannot go beyond this limit, so it forms a curve like the
"S" form. The S-form curve is called the Sigmoid function or the
Logistic function.

Logistic Regression Equation:


The Logistic regression equation can be obtained from the Linear
Regression equation. The mathematical steps to get Logistic
Regression equations are given below:

e = Euler’s number ~ 2.71828


Sigmoid function converts input into range 0 and 1
Logistic Regression Equation
Logistic Regression using Python
age bought_insurance
22 0
25 0
47 1
52 0
46 1
56 1
55 0
60 1
62 1
61 1
18 0
28 0
27 0
29 0
49 1
55 1
25 1
58 1
19 0
18 0
21 0
26 0
40 1
45 1
50 1
54 1
23 0
CODE age
22
bought_insurance
0
25 0
47 1
Import pandas as pd
52 0
1 from matplotlib import pyplot as plt 46 1
%matplotlib inline 56 1
55 0
60 1
62 1
df = pd.read_csv(“insurance_data.csv”)
2 df.head()
61
18
1
0
28 0
27 0
29 0
49 1
55 1
25 1
58 1
19 0
18 0
21 0
26 0
40 1
plt.scatter(df.age, df.bought_insurance, marker=‘+’, color=‘red’) 45 1
3 50 1
54 1
23 0
CODE
df.shape age
4 ------------------
(27, 2)
10 18
26 23
22 40
from sklearn.model_selection import train_test_split
5 X_train, X_test, Y_train, Y_test = train_test_split(df[[‘age’]],df.bought_insurance,test_size-0.1)

X_test
age bought_insurance 29 0
22 0 49 1
25 0 55 1
47 1 25 1
52 0 58 1
6 X_train 46
56
1
1
19
18
0
0
55 0 21 0
60 1 26 0
62 1 40 1
61 1 45 1
18 0 50 1
28 0 54 1
27 0 23 0
from sklearn.linear_model import LogisticRegression
7 model = LogisticRegression()
model.fit(X_train,Y_train)
age
------------------
model.predict(X_test) 10 18
8 array[0, 0, 1], dtype=int64) 26 23
22 40

To test the accuracy of the


9 model.score(X_test,Y_test) model, it is 1, since the data set
1.0 is less it may show as accurate.

Model.predict(25) Model.predict(56)
10
Array([0], dtype=int64) Array([1], dtype=int64)

You might also like