TATECHATLAS
◎ English
Artificial intelligence / Guide

AI Precision and Recall Evaluation on Small Datasets

Learn how to effectively evaluate the precision and recall of AI models, especially when working with small question datasets. This guide covers the concepts, calculation methods, and practical considerations using scikit-learn.

On this page

To evaluate precision and recall on a small dataset of questions for an AI model, you need to define your ground truth (correct answers) and the model's predictions. Then, use libraries like scikit-learn to calculate these metrics. Precision measures the proportion of correctly identified positive predictions out of all positive predictions made by the model, answering 'Of all the items the AI predicted as relevant, how many were actually relevant?'. Recall measures the proportion of correctly identified positive predictions out of all actual positive instances, answering 'Of all the items that were actually relevant, how many did the AI find?'. For small datasets, careful manual annotation of ground truth is crucial, and understanding the implications of different averaging methods in scikit-learn is important for interpreting the results accurately.

Understanding Precision and Recall

Precision and recall are fundamental metrics for evaluating the performance of classification models, particularly in information retrieval and AI question-answering systems. Precision quantifies the accuracy of positive predictions, focusing on the ratio of true positives to the total number of predicted positives (true positives + false positives). It answers the question: "Of all the items the AI predicted as relevant, how many were actually relevant?" A high precision means the model is good at not labeling irrelevant items as relevant. Recall, on the other hand, measures the model's ability to find all the relevant items. It is calculated as the ratio of true positives to the total number of actual positive instances (true positives + false negatives). Recall answers: "Of all the items that were actually relevant, how many did the AI find?" A high recall indicates the model is effective at identifying most of the relevant items.

Calculating Precision and Recall

To calculate precision and recall, you first need a set of ground truth labels (the correct answers) and the predictions made by your AI model for a given set of questions. For a binary classification task (e.g., relevant/not relevant), precision is calculated as TP / (TP + FP), and recall is TP / (TP + FN), where TP is True Positives, FP is False Positives, and FN is False Negatives. Libraries like scikit-learn provide convenient functions, precision_score and recall_score, to compute these metrics directly. These functions take the true labels (y_true) and predicted labels (y_pred) as input. For small datasets, ensuring the accuracy of your y_true is paramount, as any errors will directly impact the calculated metrics.

Install scikit-learn and NumPy with pip install scikit-learn numpy. In this example, labels 1 and 2 both mean relevant, while 0 means irrelevant. Convert both arrays using that same rule before applying binary metrics. The resulting counts are TP=3, FP=0 and FN=1, so the illustrative output is precision 1.00 and recall 0.75. Selecting pos_label=1 alone would not convert the original multiclass arrays. The next section evaluates the original classes separately and therefore answers a different question.

from sklearn.metrics import precision_score, recall_score

y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 0, 1]
actual_relevant = [label in (1, 2) for label in y_true]
predicted_relevant = [label in (1, 2) for label in y_pred]
precision = precision_score(actual_relevant, predicted_relevant, zero_division=0)
recall = recall_score(actual_relevant, predicted_relevant, zero_division=0)
print(f"Precision (relevant): {precision:.2f}")
print(f"Recall (relevant): {recall:.2f}")

Handling Multiclass and Multilabel Scenarios

When dealing with AI question answering, the problem might not be strictly binary. You could have multiple levels of relevance or multiple correct answers for a single question. Scikit-learn's precision_score and recall_score functions can handle multiclass and multilabel targets. The average parameter is crucial here. Options include 'micro' (global calculation), 'macro' (unweighted average per class), 'weighted' (average weighted by support), 'samples' (per instance average, for multilabel), or None (returns scores for each class). For small datasets, using average=None is often insightful as it shows performance for each specific class or label, allowing for a granular understanding of where the AI excels or struggles.

from sklearn.metrics import precision_score, recall_score
import numpy as np

y_true_multi = [0, 1, 2, 0, 1, 2]
y_pred_multi = [0, 2, 1, 0, 0, 1]

# Calculate precision and recall for each class
precision_scores = precision_score(y_true_multi, y_pred_multi, average=None)
recall_scores = recall_score(y_true_multi, y_pred_multi, average=None)

print(f"Precision per class: {precision_scores}")
print(f"Recall per class: {recall_scores}")

# Example for weighted average
weighted_precision = precision_score(y_true_multi, y_pred_multi, average='weighted')
weighted_recall = recall_score(y_true_multi, y_pred_multi, average='weighted')

print(f"Weighted Precision: {weighted_precision:.2f}")
print(f"Weighted Recall: {weighted_recall:.2f}")

The Role of Zero Division

The next example explicitly evaluates positive class 1, which occurs in neither array. Both denominators are zero. With zero_division="warn", each metric returns 0.0 and emits an UndefinedMetricWarning; with 0 or 1 it returns that value without a warning. NumPy NaN is supported from scikit-learn 1.3. These choices handle an undefined score; returning 1 does not demonstrate perfect model performance.

import numpy as np
from sklearn.metrics import precision_score, recall_score

y_true = [0, 0, 0]
y_pred = [0, 0, 0]
for policy in ("warn", 0, 1, np.nan):
    precision = precision_score(y_true, y_pred, pos_label=1, average="binary", zero_division=policy)
    recall = recall_score(y_true, y_pred, pos_label=1, average="binary", zero_division=policy)
    print(f"{policy}: precision={precision}, recall={recall}")

Interpreting Results on Small Datasets

Evaluating AI performance on small datasets requires careful interpretation. High precision might be achieved simply because the model makes very few positive predictions, and those it does make happen to be correct. Similarly, high recall might occur if the dataset has very few actual positive instances, and the model correctly identifies them. It's crucial to consider both metrics together. A common approach is to look at the Precision-Recall curve, although for very small datasets, this might not be as informative. For question-answering systems, a small dataset might mean manually verifying each prediction. If the dataset is too small to be representative, the calculated precision and recall might not generalize well to larger, unseen data. Consider augmenting your dataset or using techniques like cross-validation if feasible.

Precision vs. Recall Trade-off

There is often a trade-off between precision and recall. Increasing one can sometimes decrease the other. For instance, a model that is very conservative and only predicts an answer as relevant when it's extremely confident will likely have high precision but low recall (it misses many relevant answers). Conversely, a model that tries to find every possible relevant answer, even with lower confidence, will likely have high recall but lower precision (it includes many irrelevant answers). The optimal balance depends on the specific application. For a question-answering AI, is it more important to provide only correct answers (high precision), or to ensure that all possible correct answers are found, even if some irrelevant ones are included (high recall)? This decision guides how you prioritize these metrics.

Practical Considerations for AI Question Answering

For retrieval, define the evaluation unit and label query-document pairs using an independent relevance criterion. Vector similarity ranks candidates; it does not provide ground truth. A nearby vector can belong to a document that fails to answer the question. Compare the returned documents with fixed relevance labels to compute precision and recall. State the judged collection and what the recall denominator covers. Assess the correctness and support of a generated answer separately: good document retrieval alone does not establish a correct answer. The same independent labels can compare vector and hybrid retrieval without treating either system’s score as the truth.

Using precision_recall_fscore_support

Scikit-learn offers a utility function, precision_recall_fscore_support, which computes precision, recall, F1-score, and the number of true instances (support) for each class. This function is particularly useful when you want a comprehensive overview of your model's performance across all classes, especially in multiclass scenarios. For small datasets, seeing the 'support' for each class can be very informative, highlighting which classes have few examples, which might explain low recall or precision for those specific classes. This function simplifies the process of getting multiple key metrics at once.

from sklearn.metrics import precision_recall_fscore_support

y_true_multi = [0, 1, 2, 0, 1, 2]
y_pred_multi = [0, 2, 1, 0, 0, 1]

precision, recall, fscore, support = precision_recall_fscore_support(y_true_multi, y_pred_multi, average=None)

print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1-Score: {fscore}")
print(f"Support: {support}")

Limitations with Small Datasets

The primary limitation when evaluating precision and recall on small datasets is the lack of statistical significance. Metrics calculated on a small sample may not accurately reflect the model's performance on a larger, more diverse dataset. A model might perform exceptionally well on a small, curated set of questions simply due to chance or overfitting to the specific examples. Conversely, a model might appear to perform poorly if the small dataset happens to contain challenging edge cases. It's difficult to generalize findings from very small datasets. Furthermore, the definition of 'ground truth' itself can be subjective, especially in nuanced AI tasks like question answering, and annotating a small dataset might not capture the full spectrum of possible answers or interpretations.

Things to check

  • Ensure y_true (ground truth) is accurately annotated for the small dataset.
  • Verify that y_pred (model predictions) correspond correctly to the y_true labels.
  • Understand the average parameter's impact on multiclass/multilabel results.
  • Consider the zero_division parameter's behavior for edge cases.
  • Analyze both precision and recall together, not in isolation.
  • Be aware that results from small datasets may not generalize well.

These metrics are most reliable when applied to datasets that are representative of the real-world data the AI will encounter. For very small datasets, the calculated precision and recall may not accurately reflect true performance due to statistical limitations and potential overfitting. Results should be interpreted with caution, and efforts should be made to expand the dataset or use cross-validation if possible.

Sources

  1. scikit-learn: precision_score ↗
  2. scikit-learn: recall_score ↗
  3. Microsoft Learn: relevance in vector search ↗
Back to top ↑