Logistic Regression Code in Python for absolute beginners

 

Brief about Logistic Regression Algorithm

When we need to predict o the class where the predicted variable has a set of defined values like yes or no, we use Logistic Regression. 

Difference between Logistic and Linear Regression

In Linear Regression, the value predicted variable is continuous whereas, in Logistic Regression, the value predicted variable is discrete.

Maths behind Logistic Regression

Logistic Regression uses the Sigmoid function. 


Its maximum value is 1 & the minimum value is 0 and 'e' is Euler's number which is equal to 2.71828.


now to perform classification in Logistic Regression, we replace z by the equation of a straight line, modifying the function to



Note- to understand the equation of a straight line read Linear Regression Code in Python for absolute beginners.

How Classification is done?

When the value of the function comes out to be more than 0.5 then it will be class A and when less than 0.5 then class B.

Code for Logistic Regression on Jupyter Notebook using Python

Step1 Importing all required libraries



Step2 Reading data in Jupyter Notebook

within ' ' enter the path to the data set in your system. 

.head() will show the first 5 rows of the data set


Step3 Separating Dependent and Independent Variable

The .drop function will remove the column mentioned in ' ' and store the remaining data in x.

[[' "]] will store that column in y.


Step4 Preparing training and testing data set

train_size will tell the machine what percent of data is considered as a training set and the remaining will be testing data set. The training data will be chosen randomly and not in sequence.

Here train_size is 0.8 which means 80% of data will be used for training the machine and the rest 20% will be used for testing.


Step5 Importing Logistic Regression and making it class object


Step6 Fitting training data set in our model


Step7 Predicting the class of Testing Data


Step8 Calculating accuracy and precision of predictions

Classification_report will give the accuracy and precision of the model. 



This is the easiest code to run the Logistic Regression algorithm. If you need an example of fitting actual data then write in the comment and I will make a separate blog for it.

Suggested post to read- Linear Regression Code in Python for absolute beginners. 

Comments

Popular posts from this blog

Linear Regression Code in Python for absolute beginners.