You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询在Keras中获取各类别正确预测数的实现方法

How to Get Per-Class Correct Predictions (Plus Kappa Score) for Imbalanced Datasets

Absolutely! Getting per-class correct predictions is totally doable, and it’s super critical when dealing with highly imbalanced data like your 1:93 ratio. Combining this with the kappa metric makes perfect sense too—since kappa accounts for chance agreement, it’s way more reliable than raw accuracy for imbalanced cases.

Here’s a step-by-step guide using Python’s scikit-learn (the go-to library for this kind of evaluation):

Step 1: Calculate the Confusion Matrix

The confusion matrix breaks down true vs. predicted labels, and its diagonal elements are exactly the number of correct predictions for each class.

Step 2: Extract Per-Class Correct Counts

Once you have the confusion matrix, you can pull out the diagonal values to get your per-class correct prediction numbers.

Step 3: Compute the Kappa Score

While you’re at it, calculating the kappa score is straightforward with scikit-learn’s built-in function.

Full Code Example

from sklearn.metrics import confusion_matrix, cohen_kappa_score
import numpy as np

# Replace these with your actual true and predicted labels
y_true = np.array([0]*10 + [1]*930)  # Simulating your 1:93 class ratio
y_pred = np.array([0]*8 + [1]*2 + [1]*925 + [0]*5)  # Example predictions

# Calculate confusion matrix
confusion_mat = confusion_matrix(y_true, y_pred)

# Extract per-class correct predictions (diagonal of the matrix)
per_class_correct = np.diag(confusion_mat)

# Replace these with your actual class names/labels
class_names = ["Minority Class", "Majority Class"]

# Print results for each class
for class_name, correct_count in zip(class_names, per_class_correct):
    print(f"{class_name}: {correct_count} correct predictions")

# Calculate and print Kappa score
kappa_score = cohen_kappa_score(y_true, y_pred)
print(f"\nKappa Score: {kappa_score:.4f}")

What This Does

  • The confusion_matrix outputs a matrix where rows represent true labels and columns represent predicted labels. For binary classification, it’s a 2x2 matrix.
  • np.diag(confusion_mat) grabs the values where true labels match predicted labels—these are your correct counts per class.
  • cohen_kappa_score computes the kappa metric, which adjusts for chance agreement (so you don’t get tricked by a model that just predicts the majority class all the time).

For Multi-Class Datasets

If you ever work with more than two classes, this method still works perfectly! np.diag(confusion_mat) will return an array of correct counts for every class in your dataset.

This setup gives you both the granular per-class performance you need and the robust kappa score to assess overall model quality—ideal for your imbalanced dataset scenario.

内容的提问来源于stack exchange,提问作者user5722540

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:14:19