如何从Python分类器获取各类别概率并验证准确率?
Got it, let's walk through this step by step with a practical example using scikit-learn—this approach works for any number of classes (including your 9-class use case). I’ll use a synthetic dataset to make things concrete, but you can swap it out with your own data easily.
Step 1: Setup and Train a Classifier
First, we’ll train a classification model that supports outputting class probabilities (most scikit-learn classifiers do, like LogisticRegression, RandomForestClassifier, or GradientBoostingClassifier).
import numpy as np from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score # Create a synthetic 9-class dataset (matches your use case) X, y = make_classification( n_samples=1000, n_features=20, n_informative=15, n_classes=9, random_state=42 ) # Split into training and test sets X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) # Train a classifier (Logistic Regression here, use your model of choice) model = LogisticRegression(multi_class="multinomial", max_iter=1000, random_state=42) model.fit(X_train, y_train)
Step 2: Get Class Probabilities for Each Sample
Use the predict_proba() method to get the probability distribution across all classes for each test sample. The output is a 2D array where each row corresponds to a sample, and each column is the probability of that sample belonging to a specific class.
# Get class probabilities for the test set class_probs = model.predict_proba(X_test) # Check the shape: (number of test samples, number of classes) print(f"Probabilities shape: {class_probs.shape}") # Should be (200, 9) for our synthetic data # Example: Print probabilities for the first 2 samples print("\nFirst sample probabilities:") print(class_probs[0]) print("\nSecond sample probabilities:") print(class_probs[1])
Step 3: Predict Class Labels from Probabilities
To get the predicted class label, we just take the index of the maximum probability in each row. You can do this manually with np.argmax(), or use the model’s built-in predict() method (which does this exact logic under the hood).
# Manual prediction from probabilities predicted_labels = np.argmax(class_probs, axis=1) # Compare with model's predict() method (should be identical) model_predicted_labels = model.predict(X_test) print("\nAre manual predictions same as model's predict()?", np.array_equal(predicted_labels, model_predicted_labels))
Step 4: Calculate Classification Accuracy
Now we can compare our predicted labels with the true test labels to compute the classification accuracy.
# Calculate accuracy accuracy = accuracy_score(y_test, predicted_labels) print(f"\nClassification Accuracy: {accuracy:.4f}")
Quick Notes
- Not all classifiers support
predict_proba()out of the box—for example,SVCin scikit-learn requires settingprobability=Trueduring initialization to enable this feature. - The order of classes in
class_probsmatches the order ofmodel.class_(you can runprint(model.class_)to see which index maps to which class label). - For multi-class problems, make sure your classifier is configured correctly (like using
multi_class="multinomial"for Logistic Regression).
内容的提问来源于stack exchange,提问作者Vidya Marathe

