Brief Summary of Logistic Regression:
Logistic Regression is Classification algorithm commonly used in Machine Learning. It allows categorizing data into discrete classes by learning the relationship from a given set of labeled data. It learns a linear relationship from the given dataset and then introduces a non-linearity in the form of the Sigmoid function.
In case of Logistic regression, the hypothesis is the Sigmoid of a straight line, i.e,
Where the vector
w represents the Weights and the scalar
b represents the Bias of the model.
Let us visualize the Sigmoid Function –
Note that the range of the Sigmoid function is (0, 1) which means that the resultant values are in between 0 and 1. This property of Sigmoid function makes it a really good choice of Activation Function for Binary Classification. Also
for z = 0, Sigmoid(z) = 0.5 which is the midpoint of the range of Sigmoid function.
Just like Linear Regression, we need to find the optimal values of w and b for which the cost function J is minimum. In this case, we will be using the Sigmoid Cross Entropy cost function which is given by
This cost function will then be optimized using Gradient Descent.
We will start by importing the necessary libraries. We will use Numpy along with Tensorflow for computations, Pandas for basic Data Analysis and Matplotlib for plotting. We will also be using the preprocessing module of
Scikit-Learn for One Hot Encoding the data.
Data Shape: (100, 4) 0 1 2 3 0 0 5.1 3.5 1 1 1 4.9 3.0 1 2 2 4.7 3.2 1 3 3 4.6 3.1 1 4 4 5.0 3.6 1
Now let’s get the feature matrix and the corresponding labels and visualize.
Shape of Feature Matrix: (100, 2) Shape Label Vector: (100, 1)
Visualize the given data.
Now we will be One Hot Encoding the data for it to work with the algorithm. One hot encoding transforms categorical features to a format that works better with classification and regression algorithms. We will also be setting the Learning Rate and the number of Epochs.
m = 100 n = 7 Learning Rate = 0.0035 Number of Epochs = 500
Now we will start creating the model by defining the placeholders
Y, so that we can feed our training examples
y into the optimizer during the training process. We will also be creating the trainable Variables
b which can be optimized by the Gradient Descent Optimizer.
Now declare the Hypothesis, Cost function, Optimizer and Global Variables Initializer.
Begin the training process inside a Tensorflow Session.
Epoch 100 Cost: 125.700202942 Epoch 200 Cost: 120.647117615 Epoch 300 Cost: 118.151592255 Epoch 400 Cost: 116.549999237 Accuracy: 91.0000026226 %
Let’s plot the change of cost over the epochs.
Plot the change of accuracy over the epochs.
Now we will be plotting the Decision Boundary for our trained classifier. A decision boundary is a hypersurface that partitions the underlying vector space into two sets, one for each class.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- ML | Linear Regression vs Logistic Regression
- Identifying handwritten digits using Logistic Regression in PyTorch
- ML | Logistic Regression using Python
- ML | Kaggle Breast Cancer Wisconsin Diagnosis using Logistic Regression
- ML | Heart Disease Prediction Using Logistic Regression .
- Logistic Regression using Statsmodels
- Implementation of Logistic Regression from Scratch using Python
- Understanding Logistic Regression
- ML | Why Logistic Regression in Classification ?
- ML | Cost function in Logistic Regression
- ML | Logistic Regression v/s Decision Tree Classification
- Logistic Regression in R Programming
- Differentiate between Support Vector Machine and Logistic Regression
- Advantages and Disadvantages of Logistic Regression
- Linear Regression Using Tensorflow
- Softmax Regression using TensorFlow
- COVID-19 Peak Prediction using Logistic Function
- Python - Logistic Distribution in Statistics
- sympy.stats.Logistic() in python
- numpy.random.logistic() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet