Quick Start
Language / 언어: English | 한국어
Get up and running with infer-ci in 5 minutes.
Installation
infer-ci requires Python 3.10 or newer. For virtual environment setup and other installation options, see Installation.
Your First Evaluation
MetricEvaluator.evaluate() takes your true labels and model predictions and returns the metric value together with its confidence interval (CI).
Inputs
| Argument | Description |
|---|---|
y_true |
Ground-truth values. A 1-D NumPy array or list. For binary classification, use 0/1 labels. For regression, use numbers. |
y_pred |
Model predictions, in the same order and with the same length as y_true. For classification, use predicted labels (not probabilities). |
task |
Type of problem: 'classification', 'regression' or 'detection'. |
metric |
Metric to compute, for example 'accuracy' or 'mae'. See Available Metrics. |
Main options
| Option | Default | Description |
|---|---|---|
method |
'bootstrap_bca' |
How the CI is calculated. Analytical methods such as 'wilson' give the same result every run. Bootstrap methods ('bootstrap_*') resample the data randomly. See Available CI Methods. |
confidence_level |
0.95 |
Confidence level of the interval. 0.95 gives a 95% CI. |
n_resamples |
9999 |
Number of bootstrap resamples. Used only by bootstrap methods. |
random_state |
None |
Random seed for bootstrap methods. Set it (for example random_state=42) to reproduce the CI with the same data and library versions. |
compute_ci |
True |
If False, only the metric value is returned. |
Return value
For the classification and regression examples below, compute_ci=True (the default) returns a tuple:
value: the metric computed on your data.(lower, upper): the lower and upper bounds of the confidence interval.
The examples below unpack the interval as ci and read the bounds with ci[0] and ci[1]. With compute_ci=False, only value is returned.
Classification Example
from infer_ci import MetricEvaluator
import numpy as np
# Sample binary classification data
y_true = np.array([0, 1, 1, 0, 1, 1, 0, 0, 1, 1])
y_pred = np.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 1])
# Initialize evaluator
evaluator = MetricEvaluator()
# Compute accuracy with Wilson interval
accuracy, ci = evaluator.evaluate(
y_true=y_true,
y_pred=y_pred,
task='classification',
metric='accuracy',
method='wilson'
)
print(f"Accuracy: {accuracy:.3f}")
print(f"95% CI: [{ci[0]:.3f}, {ci[1]:.3f}]")
Output:
The Wilson interval is calculated with a formula, so this example prints the same result every time.
Regression Example
from infer_ci import MetricEvaluator
import numpy as np
# Sample regression data
y_true = np.array([3.0, -0.5, 2.0, 7.0, 5.0])
y_pred = np.array([2.5, 0.0, 2.0, 8.0, 4.5])
# Initialize evaluator
evaluator = MetricEvaluator()
# Compute MAE with bootstrap
mae, ci = evaluator.evaluate(
y_true=y_true,
y_pred=y_pred,
task='regression',
metric='mae',
method='bootstrap_bca',
n_resamples=2000,
random_state=42
)
print(f"MAE: {mae:.3f}")
print(f"95% CI: [{ci[0]:.3f}, {ci[1]:.3f}]")
Output:
Bootstrap methods resample the data randomly. With random_state=42, the CI is reproducible with the same data and library versions. Without random_state, the CI can change slightly from run to run, while the MAE itself stays the same.
While the bootstrap runs, a progress bar such as Bootstrap CI (BCA): 100%|...| 2006/2006 is also displayed. For the BCa method the total is n_resamples + number of samples + 1 (here 2000 + 5 + 1), because BCa also runs extra calculations to adjust the interval.
Alternative: Direct Function Calls
You can also use metric functions directly:
from infer_ci import accuracy_score, mae
import numpy as np
# Classification
y_true = np.array([0, 1, 1, 0, 1])
y_pred = np.array([0, 1, 1, 0, 0])
acc, ci = accuracy_score(y_true, y_pred, method='wilson')
print(f"Accuracy: {acc:.3f} [{ci[0]:.3f}, {ci[1]:.3f}]")
# Regression
y_true = np.array([3.0, -0.5, 2.0, 7.0])
y_pred = np.array([2.5, 0.0, 2.0, 8.0])
mae_val, ci = mae(y_true, y_pred)
print(f"MAE: {mae_val:.3f} [{ci[0]:.3f}, {ci[1]:.3f}]")
Common Workflows
1. Evaluating Multiple Metrics
from infer_ci import MetricEvaluator
import numpy as np
y_true = np.array([0, 1, 1, 0, 1, 1, 0, 0, 1, 1])
y_pred = np.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 1])
evaluator = MetricEvaluator()
metrics = ['accuracy', 'precision', 'recall', 'f1']
for metric in metrics:
value, ci = evaluator.evaluate(
y_true=y_true,
y_pred=y_pred,
task='classification',
metric=metric,
method='wilson'
)
print(f"{metric.capitalize():12s}: {value:.3f} [{ci[0]:.3f}, {ci[1]:.3f}]")
Output:
Accuracy : 0.900 [0.597, 0.983]
Precision : 0.857 [0.485, 0.981]
Recall : 1.000 [0.661, 1.000]
F1 : 0.923 [0.640, 0.989]
2. Comparing CI Methods
from infer_ci import accuracy_score
import numpy as np
y_true = np.array([0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1])
y_pred = np.array([0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1])
methods = ['wilson', 'normal', 'bootstrap_bca']
for method in methods:
acc, ci = accuracy_score(y_true, y_pred, method=method, n_resamples=2000)
width = ci[1] - ci[0]
print(f"{method:15s}: [{ci[0]:.3f}, {ci[1]:.3f}] (width: {width:.3f})")
Output:
wilson : [0.519, 0.938] (width: 0.419)
normal : [0.596, 1.004] (width: 0.408)
bootstrap_bca : [0.533, 0.933] (width: 0.400)
3. Multi-Class Classification
from infer_ci import classification_report_with_ci
import numpy as np
y_true = [0, 1, 2, 2, 2, 1, 1, 1, 0, 2, 2, 1, 0, 2, 2, 1, 2, 2, 1, 1]
y_pred = [0, 1, 0, 0, 2, 1, 1, 1, 0, 2, 2, 1, 0, 1, 2, 1, 2, 2, 1, 1]
classification_report_with_ci(y_true, y_pred)
Output:
Class Precision Recall F1-Score Precision CI Recall CI F1-Score CI Support
0 Class 0 0.600 1.000 0.750 (0.231, 0.882) (0.439, 1.0) (0.408, 1.092) 3
1 Class 1 0.889 1.000 0.941 (0.565, 0.98) (0.676, 1.0) (0.796, 1.086) 8
2 Class 2 1.000 0.667 0.800 (0.61, 1.0) (0.354, 0.879) (0.562, 1.038) 9
3 micro 0.850 0.850 0.850 (0.694, 1.006) (0.694, 1.006) (0.694, 1.006) 20
4 macro 0.830 0.889 0.830 (0.702, 0.958) (0.775, 1.002) (0.548, 1.113) 20
Available Metrics
Classification Metrics
from infer_ci import MetricEvaluator
evaluator = MetricEvaluator()
print(evaluator.get_available_metrics('classification'))
accuracy- Overall accuracyprecision- Positive Predictive Valuerecall- True Positive Ratef1- F1 Scorespecificity- True Negative Ratenpv- Negative Predictive Valuefpr- False Positive Rateauc- ROC AUC Score
Regression Metrics
mae- Mean Absolute Errormse- Mean Squared Errorrmse- Root Mean Squared Errorr2- R² Scoremape- Mean Absolute Percentage Erroriou- Intersection over Union
Detection Metrics
map- mean Average Precision @0.5:0.95map50- mean Average Precision @0.5precision- Detection precisionrecall- Detection recall
Available CI Methods
For Classification
Analytical methods:
- wilson - Wilson score (recommended)
- normal - Normal approximation
- agresti_coull - Agresti-Coull interval
- beta - Clopper-Pearson
- jeffreys - Jeffreys interval
Bootstrap methods:
- bootstrap_bca - BCa (recommended)
- bootstrap_percentile - Percentile
- bootstrap_basic - Basic
Other:
- jackknife - Jackknife resampling
- takahashi - For F1/precision/recall (multi-class)
For Regression
bootstrap_bca- Bootstrap BCa (default)bootstrap_percentile- Bootstrap percentilejackknife- Jackknife resampling
Real-World Example
Evaluating a Scikit-Learn Model
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from infer_ci import MetricEvaluator
import numpy as np
# Load data
iris = load_iris()
X, y = iris.data, iris.target
# Binary classification (class 0 vs rest)
y_binary = (y == 0).astype(int)
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y_binary, test_size=0.3, random_state=42
)
# Train model
clf = RandomForestClassifier(random_state=42)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
# Evaluate with confidence intervals
evaluator = MetricEvaluator()
for metric in ['accuracy', 'precision', 'recall', 'f1']:
value, ci = evaluator.evaluate(
y_true=y_test,
y_pred=y_pred,
task='classification',
metric=metric,
method='wilson'
)
print(f"{metric.capitalize():12s}: {value:.3f} [{ci[0]:.3f}, {ci[1]:.3f}]")
Next Steps
Common Parameters
MetricEvaluator.evaluate() and the metric functions accept these parameters (see Main options for details):
method- CI calculation method.evaluate()uses'bootstrap_bca'by default. Metric functions called directly have their own defaults; for example,accuracy_score()uses'wilson'andmae()uses'bootstrap_bca'.confidence_level- Confidence level (default: 0.95)n_resamples- Number of bootstrap resamples (default: 9999, bootstrap methods only)random_state- Random seed for bootstrap methods (default: None)compute_ci- Whether to compute CI (default: True)plot- Generate visualization (default: False, bootstrap only)
Tips
- Wilson method is recommended for binary classification metrics
- Bootstrap BCa is the default method of
evaluate()and works well for most cases - Use
random_statefor reproducible results - The default
n_resamples=9999gives stable CIs; use a smaller value (e.g., 2000) for faster runs - Use
plot=Truewith bootstrap methods to visualize the distribution