- Best AI Text Generators for High Quality Content Writing
- Tensorflow Error on Macbook M1 Pro - NotFoundError: Graph execution error
- How does GPT-like transformers utilize only the decoder to do sequence generation?
- How to set all tensors to cuda device?
- How should I use torch.compile properly?
- How do I check if PyTorch is using the GPU?
- WARNING:tensorflow:Using a while_loop for converting cause there is no registered converter for this op
- How to use OneCycleLR?
- Error in Python script "Expected 2D array, got 1D array instead:"?
- How to save model in .pb format and then load it for inference in Tensorflow?
- Top 6 AI Logo Generator Up Until Now- Smarter Than Midjourney
- Best 9 AI Story Generator Tools
- The Top 6 AI Voice Generator Tools
- Best AI Low Code/No Code Tools for Rapid Application Development
- YOLOV8 how does it handle different image sizes
- Best AI Tools For Email Writing & Assistants
- 8 Data Science Competition Platforms Beyond Kaggle
- Data Analysis Books that You Can Buy
- Robotics Books that You Can Buy
- Data Visualization Books that You can Buy
How to get probabilities along with classification in LogisticRegression?
Written by- Aionlinecourse710 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]