Logistic Regression Practical Implementation in Python

Alok Choudhary
2 min readMay 1, 2024

--

Logistic regression is a powerful statistical method commonly employed in scenarios with binary dependent variables. Its primary function is to predict the probability that a given input belongs to one group rather than another, or to other groups, under various circumstances.

Understand Dataset

Example of Data

Logistic regression finds application in machine learning, particularly in binary classification problems where the target variable has two classes at most. This article provides an overview of logistic regression, focusing on its key components: the sigmoid function and cost function, and also explores its derivation from linear regression.

Understanding Mathematics behind Logistic Regression:

Before diving into Practical, Recap Logistic Regression :- Click here to get complete Mathematical Intuition behind Logistic regression.

Practical Implementation in Python

import seaborn as sns
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
# Load the iris dataset from scikit-learn
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = iris.target_names[iris.target]
df.head()
df['species'].unique()
df.isnull().sum()
# replace setosa
df=df[df['species']!='setosa']
df['species']=df['species'].map({'versicolor':0,'virginica':1})
# Split dataset into independent and dependent features
X=df.iloc[:,:-1]
y=df.iloc[:,-1]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42)
from sklearn.linear_model import LogisticRegression
classifier=LogisticRegression()
# GridSearchCV
from sklearn.model_selection import GridSearchCV
parameter={'penalty':['l1','l2','elasticnet'],'C':[1,2,3,4,5,6,10,20,30,40,50],'max_iter':[100,200,300]}
classifier_regressor=GridSearchCV(classifier,param_grid=parameter,scoring='accuracy',cv=5)
classifier_regressor.fit(X_train,y_train)
#best combination find
print(classifier_regressor.best_params_)
print(classifier_regressor.best_score_)

0.9733333333333334

#prediction
y_pred=classifier_regressor.predict(X_test)
# accuracy score
from sklearn.metrics import accuracy_score,classification_report
score=accuracy_score(y_pred,y_test)
print(score)

0.92

print(classification_report(y_pred,y_test))
classification report
print("Thank-You!Happy Learning!")

I hope this gives you a comprehensive understanding of logistic regression, from the mathematical intuition with Practical Implementation in Python.

Join me in exploring these pillars of technological evolution. Let’s unravel the mysteries, debunk the myths, and harness the power of data to shape our future. Follow my journey, engage with the visuals, and let’s decode the future, one pixel at a time.

--

--

Alok Choudhary
Alok Choudhary

Written by Alok Choudhary

My Self Alok Choudhary, a Data Science scholar at IIT Patna, is pioneering in AI, ML, DS, and DL, crafting algorithms that redefine the tech landscape.

No responses yet