### How to get probabilities along with classification in LogisticRegression?

Written by- Sharif
4844 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 LogisticRegressionYou can also use the predict method to get the predicted class labels, if you just want the classifications and not the probabilities.

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]

# Get the predicted class labels of the test setKeep 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.

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]