Logistic Regression using Python
Last Updated : 10 Feb, 2026
Logistic Regression is a widely used supervised machine learning
algorithm used for classification tasks. In Python, it helps model the
relationship between input features and a categorical outcome by
estimating class probabilities, making it simple, efficient and easy to
interpret.
Used for binary and multiclass classification
Predicts probabilities using the logistic (sigmoid) function
Logistic Regression
We will build a classifier that predicts whether a tumour is malignant or
benign, based on medical measurements using Python.
Step 1: Import Required Libraries
numpy: Handles numerical computations and arrays
pandas: Used for data handling and tabular structures
[Link]: Helps visualize data and results
Scikit Learn: Used for model building
import numpy as np
import pandas as pd
import [Link] as plt
from [Link] import load_breast_cancer
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score,
confusion_matrix,precision_score, recall_score,f1_score,roc_curve,
roc_auc_score
Step 3: Convert Data to a Pandas DataFrame
The raw dataset is converted into pandas structures for easier handling
and analysis. Using a DataFrame improves readability,
supports exploratory data analysis and aligns with real world data
workflows commonly used in production.
X represents the input features
y represents the target variable
X = [Link]([Link], columns=data.feature_names)
y = [Link]([Link])
Step 4: Train and Test Split
This step splits the data into training and testing sets. Here we will use
25% of data for testing and rest for training.
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=29
Step 5: Feature Scaling
This step standardizes feature values so they are on a similar scale.
Logistic Regression uses gradient based optimization which is sensitive to
feature magnitudes, so scaling helps the model train correctly. The scaler
is fit only on training data and then applied to test data to avoid data
leakage.
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
Step 6: Train the Logistic Regression Model
This step trains the Logistic Regression model on the scaled training data.
Here:
The model learns feature weights that separate the classes,
Applies the sigmoid function to compute probabilities.
Assigns a class based on those probabilities.
model = LogisticRegression(max_iter=1000)
[Link](X_train, y_train)
Step 7: Make Predictions
At this stage, the trained model is used to make predictions on unseen
test data. It computes probabilities for each class and applies a threshold
(default = 0.5) to convert them into class labels.
1: Predicted as benign
0: Predicted as malignant
y_pred = [Link](X_test)
y_pred
Output:
Predicted Labels
Step 8: Model Evaluation
At this stage, predictions are available. We now evaluate how well the
model performs using multiple metrics, since each metric highlights a
different aspect of performance.
Accuracy Score
Accuracy measures how often the model makes correct predictions
overall.
Gives a quick idea of overall correctness
Works well when classes are balanced
Can be misleading for imbalanced datasets
accuracy = accuracy_score(y_test, y_pred)
print(f"accuracy: {round(accuracy,2)}")
Output:
accuracy: 0.98
Confusion Matrix
A confusion matrix shows where the model is right and where it makes
mistakes by comparing predicted labels with actual labels.
Shows exact error types, not just a score
Helps understand false positives vs false negatives
Critical in domains like healthcare, where mistakes have different
costs
cm = confusion_matrix(y_test, y_pred)
cm
Output:
Confusion Matrix
Precision Score
Precision shows how many predicted positives are actually correct.
Focuses on reducing false positives
Important when wrong positives are costly
Commonly used in spam and fraud detection
precision = precision_score(y_test, y_pred)
f"Precision: {round(precision,2)}"
Output:
Precision: 0.99