SciRS2 Metrics
Production-ready machine learning evaluation metrics library for the SciRS2 scientific computing ecosystem (v0.1.5). Following the SciRS2 POLICY, this comprehensive module provides high-performance functions to evaluate prediction performance across classification, regression, clustering, and specialized ML domains with ecosystem consistency and enhanced performance optimizations.
Features
Core Metrics
- Classification Metrics: Complete suite including accuracy, precision, recall, F1-score, ROC curves, AUC, Matthews correlation coefficient, Cohen's kappa, and specialized multi-class/multi-label metrics
- Regression Metrics: Comprehensive set including MSE, MAE, R² score, explained variance, Huber loss, quantile loss, and robust regression metrics
- Clustering Metrics: Full coverage of internal metrics (Silhouette, Calinski-Harabasz, Davies-Bouldin) and external metrics (Adjusted Rand Index, Normalized Mutual Information, V-measure)
- Ranking Metrics: Advanced ranking evaluation with NDCG, MAP, MRR, and specialized retrieval metrics
Specialized Domains
- Anomaly Detection: Comprehensive anomaly evaluation with distribution metrics, time-series anomaly detection, and NAB scoring
- Fairness & Bias Detection: Advanced fairness metrics, demographic parity, equalized odds, and bias analysis tools
- Computer Vision: Object detection, image segmentation, and classification-specific metrics
- Natural Language Processing: Text generation, classification, NER, and sentiment analysis metrics
- Time Series: Forecasting accuracy, trend analysis, and autocorrelation metrics
- Recommender Systems: Ranking, diversity, novelty, and rating prediction metrics
Advanced Capabilities
- Bayesian Evaluation: Model comparison with Bayes factors, information criteria (BIC, WAIC, LOO-CV), and posterior predictive checks
- Statistical Testing: McNemar's test, Friedman test, bootstrap confidence intervals, and cross-validation frameworks
- Hardware Acceleration: SIMD-optimized computations with automatic fallbacks for maximum performance
- Streaming Metrics: Memory-efficient online evaluation for large-scale and real-time applications
- Custom Metrics: Extensible framework for domain-specific metric development
Integration & Visualization
- Seamless Integration: First-class support for scirs2-neural and scirs2-optim with metric-based optimization
- Rich Visualizations: Interactive ROC curves, confusion matrices, calibration plots, and customizable dashboards
- Multiple Backends: Support for Plotters and Plotly rendering engines
- Export Capabilities: JSON, CSV, HTML, and image format exports for reports and presentations
Installation
Add to your Cargo.toml:
[]
= "0.1.5"
Feature Selection
Choose features based on your needs:
[]
# Default features (recommended for most users)
= "0.1.5"
# Minimal installation (core metrics only)
= { = "0.1.5", = false }
# Full installation with all capabilities
= { = "0.1.5", = ["neural_common", "optim_integration", "plotters_backend"] }
Available Features
plotly_backend(default): Interactive web-based visualizations with Plotlyoptim_integration(default): Integration with scirs2-optim for metric-based optimizationneural_common: Integration utilities for neural network trainingplotters_backend: Static plot generation with Plotters backenddashboard_server: HTTP server for real-time dashboard (requires tokio)
Usage
Basic usage examples:
use ;
use array;
use CoreResult;
// Classification metrics example
// Regression metrics example
// Clustering metrics example
Visualization
The visualization module provides utilities for creating informative visualizations of metrics results:
use array;
use ;
// Example: ROC Curve Visualization
// Example: Confusion Matrix Visualization
// Example: Custom visualization creation
Available Visualization Types
The visualization module supports multiple plot types:
- Line Plots: For time series, learning curves, loss curves, etc.
- Scatter Plots: For correlation, dimensionality reduction results, etc.
- Bar Charts: For feature importance, classification reports, etc.
- Heatmaps: For confusion matrices, correlation matrices, etc.
- Histograms: For value distributions, error distributions, etc.
Interactive Dashboard Server
When the dashboard_server feature is enabled, you can create a real-time web dashboard for monitoring metrics:
use ;
use start_http_server;
// Create dashboard configuration
let mut config = default;
config.title = "ML Training Dashboard".to_string;
config.refresh_interval = 2; // Refresh every 2 seconds
// Create interactive dashboard
let dashboard = new;
// Add metrics
dashboard.add_metric?;
dashboard.add_metric?;
dashboard.add_metric?;
// Start HTTP server (requires dashboard_server feature)
// Export dashboard data
let json_data = dashboard.export_to_json?;
let csv_data = dashboard.export_to_csv?;
let html_report = dashboard.generate_html?;
The dashboard provides:
- Real-time metric visualization with auto-refresh
- RESTful API endpoints (
/api/metrics,/api/metrics/names) - Interactive charts using Chart.js
- Export capabilities (JSON, CSV, HTML)
- Customizable themes and layouts
Custom Visualizations
Create custom visualizations with the VisualizationData and VisualizationMetadata structs:
use ;
// Create custom visualization data
let mut data = new;
data.x = vec!;
data.y = vec!;
// Add series names for better labeling
data.series_names = Some;
// Create metadata to describe the visualization
let metadata = line_plot;
// Get the default backend
let backend = default_backend;
// Save the visualization
let options = default;
backend.save_to_file?;
Integration with Other Modules
Neural Networks
The neural_common feature enables integration with the scirs2-neural module:
use ;
// Create metric adapters for neural networks
let accuracy = accuracy;
let precision = precision;
let f1_score = f1_score;
// Use metrics during neural network training
let metrics = vec!;
let callback = new;
// Visualize neural network metrics
let roc_viz = neural_roc_curve_visualization?;
let history_viz = training_history_visualization;
Optimization and Hyperparameter Tuning
The optim_integration feature enables integration with the scirs2-optim module for optimization and hyperparameter tuning:
use ;
// Create a metric-based learning rate scheduler
let mut scheduler = new;
// Update scheduler based on metric values
let new_lr = scheduler.step_with_metric;
// Hyperparameter tuning
let params = vec!;
let mut tuner = new;
// Run hyperparameter search
let result = tuner.random_search?;
// Get best parameters
println!;
for in result.best_params
When combined with scirs2-optim:
use ;
use ;
// Create SGD optimizer guided by metrics
let mut optimizer = new;
// Create scheduler that adjusts learning rate based on metrics
let mut scheduler = new;
// During training loop:
optimizer.update_metric;
scheduler.step_with_metric;
scheduler.apply_to;
Components
Classification Metrics
Functions for classification evaluation:
use ;
Regression Metrics
Functions for regression evaluation:
use ;
Clustering Metrics
Functions for clustering evaluation:
use ;
Evaluation Functions
General evaluation tools:
use ;
Visualization
Functions and types for visualization:
use ;
Production Readiness
SciRS2 Metrics is production-ready with comprehensive features and robust testing:
Quality Assurance
- 142+ comprehensive tests covering all metrics and edge cases
- Zero-warning policy enforced through CI/CD pipelines
- Numerical precision testing for numerical stability
- Performance regression testing to maintain optimization standards
- Cross-platform compatibility verified on major operating systems
Performance & Scalability
- SIMD-accelerated computations with automatic hardware detection
- Memory-efficient streaming algorithms for large datasets
- Parallel processing support via Rayon integration
- Chunked processing for memory-constrained environments
- Hardware-specific optimizations (SSE2, AVX2, AVX-512)
Reliability & Robustness
- Comprehensive error handling with descriptive error messages
- Edge case coverage including NaN, infinity, and empty data handling
- Type-safe implementations leveraging Rust's type system
- API stability with semantic versioning guarantees
- Extensive documentation with mathematical formulations and examples
Integration & Ecosystem
- Scikit-learn compatibility for seamless migration from Python
- Modular architecture allowing selective feature usage
- Extensible design for custom metric development
- Rich visualization support with multiple backend options
- Domain-specific metric collections for specialized applications
Contributing
See the CONTRIBUTING.md file for contribution guidelines.
License
This project is Licensed under the Apache License 2.0. See LICENSE for details.
You can choose to use either license. See the LICENSE file for details.