0% found this document useful (0 votes)
4 views

Logistic Regression

Uploaded by

cosmicdestroyer7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Logistic Regression

Uploaded by

cosmicdestroyer7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Logistic Regression

Logistic regression is one of the most popular Machine


Learning algorithms, which comes under the Supervised
Learning technique. It is used for predicting the
categorical dependent variable using a given set of
independent variables.

•Logistic regression predicts the output of a categorical dependent


variable. Therefore the outcome must be a categorical or discrete value.
It can be either Yes or No, 0 or 1, true or False, etc. but instead of giving
the exact value as 0 and 1, it gives the probabilistic values which lie
between 0 and 1.

•Logistic Regression is much similar to the Linear Regression except that


how they are used. 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).

•The curve from the logistic function indicates the likelihood of something
such as whether the cells are cancerous or not, a mouse is obese or not
based on its weight, etc.

•Logistic Regression is a significant machine learning algorithm because it


has the ability to provide probabilities and classify new data using
continuous and discrete datasets.

•Logistic Regression can be used to classify the observations using


different types of data and can easily determine the most effective
variables used for the classification. The below image is showing the
logistic function:
Logistic regression uses the concept of predictive modeling as regression;
therefore, it is called logistic regression, but is used to classify samples; Therefore, it
falls under the classification algorithm.

Logistic Function (Sigmoid Function):

•The sigmoid function is a mathematical function used to map the


predicted values to probabilities.

•It maps any real value into another value within a range of 0 and 1.

•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.

•In logistic regression, we use the concept of the threshold value, which
defines the probability of either 0 or 1. Such as values above the
threshold value tends to 1, and a value below the threshold values tends
to 0

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:

•We know the equation of the straight line can be written as:

•In Logistic Regression y can be between 0 and 1 only, so for this let's
divide the above equation by (1-y):

•But we need range between -[infinity] to +[infinity], then take logarithm


of the equation it will become:

The above equation is the final equation for Logistic Regression.


Type of Logistic Regression:
On the basis of the categories, Logistic Regression can be classified into three
types:

•Binomial: In binomial Logistic regression, there can be only two possible


types of the dependent variables, such as 0 or 1, Pass or Fail, etc.

•Multinomial: In multinomial Logistic regression, there can be 3 or more


possible unordered types of the dependent variable, such as "cat",
"dogs", or "sheep"

•Ordinal: In ordinal Logistic regression, there can be 3 or more possible


ordered types of dependent variables, such as "low", "Medium", or
"High".

import numpy as np
from sklearn.linear_model import LinearRegression
X = numpy.array([0,1,2,3,4,5,6,7,8,9]).reshape(-1,1)
y = numpy.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
print(X)
print(y)
model = LogisticRegression(solver='liblinear', random_state=0)
model.fit(X, y)
print(model.intercept_)
print( model.coef_)

y_pred = model.predict(X)
print(y_pred)

cm = confusion_matrix(y,y_pred)
ac = accuracy_score(y,y_pred)
pr= precision_score(y,y_pred)
re= recall_score(y,y_pred)
print(cm)
print(ac)
print(pr)
print(re)

OUTPUT:
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]
[0 0 0 0 1 1 1 1 1 1]
[-1.04608067]
[[0.51491375]]
[0 0 0 1 1 1 1 1 1 1]
[[3 1]
[0 6]]
0.9
0.8571428571428571
1.0

Output Analysi:

https://keisan.casio.com/exec/system/1223447896

β0=-1.04608067 β1= 0.51491375

for X=2 by substituting above values in p the predicted class is ‘0’


originally value is 0.4959(i.e value is <threshold limit 0.5)

for X=3 by substituting above values in p the predicted class is ‘1’


originally value is 0.62221 (i.e value is >threshold limit 0.5)
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset


dataset = pd.read_csv('/home/student/Desktop/DataScience with
Python/Unit6/Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, -1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size =
0.20, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train)
print(X_test)

# Training the K-NN model on the Training set


from sklearn.linear_model import LogisticRegression
classifier =LogisticRegression(solver='liblinear', random_state=0)
classifier.fit(X_train, y_train)

print(classifier.intercept_)
print( classifier.coef_)

# Predicting the Test set results


y_pred = classifier.predict(X_test)

# Making the Confusion Matrix


from sklearn.metrics import confusion_matrix,
accuracy_score,precision_score,recall_score

cm = confusion_matrix(y_test, y_pred)
ac = accuracy_score(y_test, y_pred)
pr= precision_score(y_test, y_pred)
re= recall_score(y_test, y_pred)

print(cm)
print(ac)
print(pr)
print(re)
OUTPUT:

[-0.84765641]
[[1.99162298 1.07582244]]
[[56 2]
[ 5 17]]
0.9125
0.8947368421052632
0.7727272727272727

You might also like