How to get probabilities along with classification in LogisticRegression?

Written by - Aionlinecourse1171 times views

In scikit-learn, you can use the predict_proba method of a trained logistic regression model to get the probabilities of each class. Here is an example of how you can use it:

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

# Load the data and split it into training and test sets
X, y = load_data()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train the logistic regression model
model = LogisticRegression()
model.fit(X_train, y_train)

# Get the predicted probabilities of the test set
probs = model.predict_proba(X_test)

# The probs array will have shape (n_samples, n_classes)
# and contain the probability of each sample belonging to each class.
# For example, the probability of the first sample belonging to class 0 is probs[0, 0]
# and the probability of the first sample belonging to class 1 is probs[0, 1]

You can also use the predict method to get the predicted class labels, if you just want the classifications and not the probabilities.

# Get the predicted class labels of the test set
predictions = model.predict(X_test)

# The predictions array will have shape (n_samples,) and contain the predicted class labels
# For example, the predicted class label of the first sample is predictions[0]

Keep in mind that the predict_proba method is only available for logistic regression models that are trained with the "ovr" (one-versus-rest) or "multinomial" multi-class strategies. If the model was trained with the "binary" strategy, it will only have two classes and predict_proba will only return the probability of the positive class.

Recommended Projects

Deep Learning Interview Guide

Topic modeling using K-means clustering to group customer reviews

Have you ever thought about the ways one can analyze a review to extract all the misleading or useful information?...

Natural Language Processing
Deep Learning Interview Guide

Medical Image Segmentation With UNET

Have you ever thought about how doctors are so precise in diagnosing any conditions based on medical images? Quite simply,...

Computer Vision
Deep Learning Interview Guide

Build A Book Recommender System With TF-IDF And Clustering(Python)

Have you ever thought about the reasons behind the segregation and recommendation of books with similarities? This project is aimed...

Machine LearningDeep LearningNatural Language Processing
Deep Learning Interview Guide

Automatic Eye Cataract Detection Using YOLOv8

Cataracts are a leading cause of vision impairment worldwide, affecting millions of people every year. Early detection and timely intervention...

Computer Vision
Deep Learning Interview Guide

Crop Disease Detection Using YOLOv8

In this project, we are utilizing AI for a noble objective, which is crop disease detection. Well, you're here if...

Computer Vision
Deep Learning Interview Guide

Vegetable classification with Parallel CNN model

The Vegetable Classification project shows how CNNs can sort vegetables efficiently. As industries like agriculture and food retail grow, automating...

Machine LearningDeep Learning
Deep Learning Interview Guide

Banana Leaf Disease Detection using Vision Transformer model

Banana cultivation is a significant agricultural activity in many tropical and subtropical regions, providing a vital source of income and...

Deep LearningComputer Vision