- How to choose the number of units for the Dense layer in the Convoluted neural network for a Image classification problem?
- How to use pydensecrf in Python3.7?
- How to set class weights in DecisionTreeClassifier for multi-class setting
- How to Extract Data from tmdB using Python
- How to add attention layer to a Bi-LSTM
- How to include SimpleImputer before CountVectorizer in a scikit-learn Pipeline?
- How to load a keras model saved as .pb
- How to train new classes on pretrained yolov4 model in darknet
- How To Import The MNIST Dataset From Local Directory Using PyTorch
- how to split up tf.data.Dataset into x_train, y_train, x_test, y_test for keras
- How to plot confusion matrix for prefetched dataset in Tensorflow
- How to Use Class Weights with Focal Loss in PyTorch for Imbalanced dataset for MultiClass Classification
- How to solve "ValueError: y should be a 1d array, got an array of shape (3, 5) instead." for naive Bayes?
- How to create image of confusion matrix in Python
- What are the numbers in torch.transforms.normalize and how to select them?
- How to assign a name for a pytorch layer?
- How to solve dist.init_process_group from hanging or deadlocks?
- How to use sample weights with tensorflow datasets?
- How to Fine-tune HuggingFace BERT model for Text Classification
- How to Convert Yolov5 model to tensorflow.js
How to get probabilities along with classification in LogisticRegression?
Written by- Aionlinecourse976 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]