VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/ml-logistic-regression-using-python/

⇱ Logistic Regression using Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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_making_decisions
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

Step 2: Load the Dataset

This step loads the Breast Cancer dataset from scikit learn. The dataset is provided as a structured object that bundles everything needed for a classification task. It contains:

  • Feature matrix: Numerical measurements used as input to the model
  • Target labels: The class to predict
  • Feature names: Descriptions of each input feature
  • Metadata: Additional information about the dataset

This structure makes the dataset easy to explore, preprocess and use directly for model training.

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

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.

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.

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.

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

Output:

👁 Output-LR
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

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

Output:

👁 Confusion-matrix
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

Output:

Precision: 0.99

Recall Score

Recall measures how many actual positives the model correctly identifies.

  • Focuses on reducing false negatives
  • Important when missing positives is risky
  • Used in healthcare and safety systems

Output:

Recall: 0.98

F1 Score

F1 score balances precision and recall into a single metric.

  • Useful when data is imbalanced
  • Penalizes extreme precision or recall values
  • Preferred when both false positives and false negatives matter

Output:

f1 score: 0.98

ROC-AUC Score

ROC-AUC shows how well the model separates classes across thresholds.

  • Measures ranking ability, not exact predictions
  • Higher value means better class separation
  • Works well for comparing classification models

Output:

👁 ROC-AUC-curve
ROC-AUC Curve

You can download the python notebook from here

Comment