Expand description
§sklears-core - Core Traits and Utilities
This crate provides the foundational traits, types, and utilities that power the entire sklears machine learning ecosystem.
§Overview
sklears-core defines the essential building blocks for machine learning in Rust:
- Core Traits:
Estimator,Fit,Predict,Transform,Score - Type System: Type-safe state machines (Untrained/Trained)
- Error Handling: Comprehensive error types with context
- Validation: Input validation and consistency checks
- Utilities: Common helper functions and types
- Parallel Processing: Abstractions for parallel algorithms
- Dataset Handling: Data loading, splitting, and manipulation
§Core Traits
§Estimator
The base trait for all machine learning models:
pub trait Estimator {
type Config;
type Error;
}§Fit
Training an estimator on data:
pub trait Fit<X, Y> {
type Fitted;
fn fit(self, x: &X, y: &Y) -> Result<Self::Fitted, Self::Error>;
}§Predict
Making predictions with a trained model:
pub trait Predict<X, Y> {
fn predict(&self, x: &X) -> Result<Y, Self::Error>;
}§Transform
Transforming data (for preprocessing and dimensionality reduction):
pub trait Transform<X> {
fn transform(&self, x: &X) -> Result<X, Self::Error>;
}§Type-Safe State Machines
Models use phantom types to track training state at compile time:
pub struct Untrained;
pub struct Trained;
pub struct Model<State = Untrained> {
config: ModelConfig,
state: PhantomData<State>,
weights: Option<Weights>, // Only Some in Trained state
}This ensures:
- ✅ Can’t predict with an untrained model (compile error)
- ✅ Can’t accidentally re-train a trained model
- ✅ Type system enforces correct usage patterns
§Error Handling
Comprehensive error types with rich context:
pub enum SklearsError {
InvalidInput(String),
ShapeMismatch { expected: Shape, got: Shape },
NotFitted,
ConvergenceError { iterations: usize },
// ... and many more
}§Validation
Input validation utilities ensure data consistency:
use sklears_core::validation;
// Check that X and y have compatible shapes
validation::check_consistent_length(x, y)?;
// Check for NaN/Inf values
validation::check_array(x)?;
// Validate classification targets
validation::check_classification_targets(y)?;§Parallel Processing
Abstractions for parallel algorithm execution:
use sklears_core::parallel::ParallelConfig;
use rayon::prelude::*;
let config = ParallelConfig::new().n_jobs(-1); // Use all cores
data.par_iter()
.map(|sample| process(sample))
.collect()§Feature Flags
simd- Enable SIMD optimizationsgpu_support- GPU acceleration supportarrow- Apache Arrow interoperabilitybinary- Binary serialization support
§Examples
See individual module documentation for detailed examples.
§Integration
This crate is re-exported by the main sklears crate, so you typically don’t
need to depend on it directly unless you’re building custom estimators.
Modules§
- advanced_
array_ ops - advanced_
benchmarking - Advanced Benchmarking Suite with Performance Regression Detection
- algorithm_
markers - api_
analyzers - API Analysis Engines and Validation Components
- api_
data_ structures - Core Data Structures for API Reference Generation
- api_
formatters - Output Formatters and Document Generators
- api_
generator_ config - API Generator Configuration Module
- async_
traits - auto_
benchmark_ generation - Automatic Benchmark Generation System
- autodiff
- benchmarking
- code_
coverage - compatibility
- compile_
time_ macros - Compile-Time Model Verification and Macro System
- compile_
time_ validation - contribution
- dataset
- dependency_
audit - dependent_
types - Dependent Type Experiments for sklears-core
- derive_
macros - distributed
- distributed_
algorithms - Distributed Machine Learning Algorithms
- dsl_
impl - Domain-Specific Language (DSL) implementation for machine learning pipelines
- effect_
types - ensemble_
improvements - error
- exhaustive_
error_ handling - exotic_
hardware - Auto-generated module structure
- exotic_
hardware_ impls - Concrete Exotic Hardware Implementations
- fallback_
strategies - features
- formal_
verification - Formal Verification System for Machine Learning Algorithms
- format_
io - formatting
- input_
sanitization - interactive_
api_ reference - Interactive API Reference Generator
- interactive_
playground - Auto-generated module structure
- macros
- memory_
safety - mock_
objects - parallel
- performance_
profiling - Advanced Performance Profiling and Optimization Framework
- performance_
reporting - plugin
- Plugin System Module
- plugin_
marketplace_ impl - Concrete Plugin Marketplace Implementation
- prelude
- public
- refinement_
types - Refinement Types System for sklears-core
- search_
engines - streaming_
lifetimes - trait_
explorer - Trait Explorer Module
- traits
- tutorial_
examples - Concrete Tutorial Examples and Learning Paths
- tutorial_
system - types
- unsafe_
audit - utils
- validation
- validation_
examples - wasm_
playground_ impl - WebAssembly Playground Implementation
Macros§
- auto_
benchmark - Macro to automatically generate benchmarks for a type
- benchmark_
suite - Creates comprehensive benchmarking suite for ML algorithms
- cfg_
feature - Macro for conditional compilation based on feature flags
- cfg_
impl - Macro for feature-gated function implementations
- cfg_
type - Macro for conditional type definitions based on features
- define_
algorithm_ category - Macro for defining algorithm categories with compile-time checking
- define_
estimator - Advanced macro for creating ML estimators with builder pattern and validation
- define_
ml_ algorithm - Creates a complete ML algorithm with all necessary boilerplate
- define_
ml_ float_ bounds - Helper macro for creating trait bound combinations commonly used in ML
- destructure
- Macro for easy destructuring of complex types
- error_
context - Macro for adding location context automatically
- estimator_
test_ suite - Creates a test suite for an estimator implementation
- impl_
algorithm_ markers - Macro for implementing multiple marker traits at once
- impl_
default_ config - Helper macro for creating default trait implementations
- impl_
ml_ traits - Implements standard machine learning traits for an estimator
- io_
effect - parameter_
map - Creates a simple parameter mapping for algorithm configurations
- pattern_
guard - Macro for creating pattern guards with custom validation logic
- pure_
effect - Convenience macros for effect creation
- quick_
dataset - Advanced macro definitions for sklears-core
- random_
effect - refinement_
predicate - Macro to create a custom refinement predicate
- simd_
operations - Creates SIMD-optimized operation implementations
- validate
- Convenience macro for validation
- validate_
performance - Macro for performance validation
- validated_
param - Macro for creating compile-time validated parameters
- validation_
rules - Macro for creating type-safe validation rules
- verify_
dimensions - Macro for dimension verification
- verify_
model - Macro for model verification
- with_
fallback - Convenience macro for executing operations with fallback