12/20/25, 7:57 PM LogisticRegression.
ipynb - Colab
keyboard_arrow_down Logistic Regression
Logistic regression is a supervised machine learning algorithm in data science. It is a type of classification algorithm that predicts a
discrete or categorical outcome. For example, we can use a classification model to determine whether a loan is approved or not based on
predictors such as savings amount, income and credit score.
In logistic regression, the model predicts the probability that a specific outcome occurs. For instance, given someone’s financial profile,
we might predict the probability that their loan is approved. The output of the model is a value between 0 and 1. Based on a threshold—
often 0.5—we classify the outcome as either "approved" or "not approved." Instead of drawing a straight line through the data as we
would in linear regression, logistic regression fits an S-shaped curve to map input values to a probability.
Segmoid Function
The sigmoid function is important in logistic regression because it converts the linear combination of input features into a probability
between 0 and 1. This allows the model to predict binary outcomes (e.g., yes/no) and interpret results as probabilities, making it ideal for
classification tasks.
Now Performing Logistic Regression
import pandas as pd
import [Link] as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
iris = sns.load_dataset("iris")
[Link]()
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
iris = iris[[Link] != "virginica"]
[Link]()
array(['setosa', 'versicolor'], dtype=object)
iris['species'] = iris['species'].map({'setosa': 0, 'versicolor': 1})
[Link]()
[Link]()
<class '[Link]'>
Index: 100 entries, 0 to 99
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 sepal_length 100 non-null float64
1 sepal_width 100 non-null float64
2 petal_length 100 non-null float64
3 petal_width 100 non-null float64
4 species 100 non-null int64
dtypes: float64(4), int64(1)
memory usage: 4.7 KB
[Link] 1/3
12/20/25, 7:57 PM [Link] - Colab
[Link]("sepal_width", axis=1, inplace=True)
x = [Link][:,:-1]
y = [Link][:,-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.2, random_state=42)
LR_Classifier = LogisticRegression()
LR_Classifier.fit(x_train, y_train)
▾ LogisticRegression i ?
LogisticRegression()
y_test_pred = LR_Classifier.predict(x_test)
y_test_pred
array([1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0])
from [Link] import confusion_matrix
confusion_matrix(y_test, y_test_pred)
array([[12, 0],
[ 0, 8]])
[Link](confusion_matrix(y_test, y_test_pred))
<Axes: >
from [Link] import accuracy_score, precision_score, recall_score, f1_score
print("Testing Accuracy: ", accuracy_score(y_test, y_test_pred))
print("Precision Score: ", precision_score(y_test, y_test_pred))
print("Recall Score: " , recall_score(y_test, y_test_pred))
print("F1 Score: ", f1_score(y_test, y_test_pred))
Testing Accuracy: 1.0
Precision Score: 1.0
Recall Score: 1.0
F1 Score: 1.0
[Link] 2/3
12/20/25, 7:57 PM [Link] - Colab
Start coding or generate with AI.
[Link] 3/3