Skip to main content

sklears_python/
lib.rs

1//! Python bindings for the sklears machine learning library
2//!
3//! This crate provides PyO3-based Python bindings for sklears, enabling
4//! seamless integration with the Python ecosystem while maintaining
5//! Rust's performance advantages.
6//!
7//! # Features
8//!
9//! - Drop-in replacement for scikit-learn's most common algorithms
10//! - Pure Rust implementation with ongoing performance optimization
11//! - Full NumPy array compatibility
12//! - Comprehensive error handling with Python exceptions
13//! - Memory-safe operations with automatic reference counting
14//!
15//! # Example
16//!
17//! ```python
18//! import sklears_python as skl
19//! import numpy as np
20//!
21//! # Create sample data
22//! X = np.random.randn(100, 4)
23//! y = np.random.randn(100)
24//!
25//! # Train a linear regression model
26//! model = skl.LinearRegression()
27//! model.fit(X, y)
28//! predictions = model.predict(X)
29//! ```
30
31#[allow(unused_imports)]
32use pyo3::prelude::*;
33
34// Import modules
35mod clustering;
36mod datasets;
37mod ensemble;
38mod linear;
39mod metrics;
40mod model_selection;
41mod naive_bayes;
42mod neural_network;
43// `preprocessing::common::PreprocessingResult` is an unused convenience alias
44// (the transformers use `PyResult` directly); allowed here rather than
45// editing the preprocessing submodule files, which are out of scope for
46// this change.
47#[allow(dead_code)]
48mod preprocessing;
49mod tree;
50mod utils;
51
52// Re-export main classes
53pub use clustering::*;
54pub use ensemble::*;
55pub use linear::*;
56pub use metrics::*;
57pub use model_selection::*;
58pub use naive_bayes::*;
59pub use neural_network::*;
60pub use preprocessing::*;
61pub use tree::*;
62pub use utils::*;
63
64/// Python module for sklears machine learning library
65#[pymodule]
66fn _sklears(m: &Bound<'_, PyModule>) -> PyResult<()> {
67    // Set module metadata
68    m.add("__version__", env!("CARGO_PKG_VERSION"))?;
69    m.add(
70        "__doc__",
71        "High-performance machine learning library with scikit-learn compatibility",
72    )?;
73
74    // Linear models
75    m.add_class::<linear::PyLinearRegression>()?;
76    m.add_class::<linear::PyRidge>()?;
77    m.add_class::<linear::PyLasso>()?;
78    m.add_class::<linear::PyElasticNet>()?;
79    m.add_class::<linear::PyBayesianRidge>()?;
80    m.add_class::<linear::PyARDRegression>()?;
81    m.add_class::<linear::PyLogisticRegression>()?;
82
83    // Ensemble methods
84    m.add_class::<ensemble::PyGradientBoostingClassifier>()?;
85    m.add_class::<ensemble::PyGradientBoostingRegressor>()?;
86    m.add_class::<ensemble::PyAdaBoostClassifier>()?;
87    m.add_class::<ensemble::PyVotingClassifier>()?;
88    m.add_class::<ensemble::PyBaggingClassifier>()?;
89
90    // Neural networks
91    m.add_class::<neural_network::PyMLPClassifier>()?;
92    m.add_class::<neural_network::PyMLPRegressor>()?;
93
94    // Tree-based models - Temporarily disabled to test ensemble
95    // m.add_class::<tree::PyDecisionTreeClassifier>()?;
96    // m.add_class::<tree::PyDecisionTreeRegressor>()?;
97    // m.add_class::<tree::PyRandomForestClassifier>()?;
98    // m.add_class::<tree::PyRandomForestRegressor>()?;
99
100    // Naive Bayes
101    m.add_class::<naive_bayes::PyGaussianNB>()?;
102    m.add_class::<naive_bayes::PyMultinomialNB>()?;
103    m.add_class::<naive_bayes::PyBernoulliNB>()?;
104    m.add_class::<naive_bayes::PyComplementNB>()?;
105
106    // Clustering
107    m.add_class::<clustering::PyKMeans>()?;
108    m.add_class::<clustering::PyDBSCAN>()?;
109
110    // Preprocessing
111    m.add_class::<preprocessing::PyStandardScaler>()?;
112    m.add_class::<preprocessing::PyMinMaxScaler>()?;
113    m.add_class::<preprocessing::PyLabelEncoder>()?;
114
115    // Metrics - Regression
116    m.add_function(wrap_pyfunction!(metrics::mean_squared_error, m)?)?;
117    m.add_function(wrap_pyfunction!(metrics::mean_absolute_error, m)?)?;
118    m.add_function(wrap_pyfunction!(metrics::r2_score, m)?)?;
119    m.add_function(wrap_pyfunction!(metrics::mean_squared_log_error, m)?)?;
120    m.add_function(wrap_pyfunction!(metrics::median_absolute_error, m)?)?;
121
122    // Metrics - Classification
123    m.add_function(wrap_pyfunction!(metrics::accuracy_score, m)?)?;
124    m.add_function(wrap_pyfunction!(metrics::precision_score, m)?)?;
125    m.add_function(wrap_pyfunction!(metrics::recall_score, m)?)?;
126    m.add_function(wrap_pyfunction!(metrics::f1_score, m)?)?;
127    m.add_function(wrap_pyfunction!(metrics::confusion_matrix, m)?)?;
128    m.add_function(wrap_pyfunction!(metrics::classification_report, m)?)?;
129
130    // Model selection
131    m.add_function(wrap_pyfunction!(model_selection::train_test_split, m)?)?;
132    m.add_class::<model_selection::PyKFold>()?;
133
134    // Dataset functions
135    datasets::register_dataset_functions(m)?;
136
137    // Utility functions
138    m.add_function(wrap_pyfunction!(utils::get_version, m)?)?;
139    m.add_function(wrap_pyfunction!(utils::get_build_info, m)?)?;
140    m.add_function(wrap_pyfunction!(utils::get_hardware_info, m)?)?;
141    m.add_function(wrap_pyfunction!(utils::benchmark_basic_operations, m)?)?;
142
143    Ok(())
144}