scikit-learn vs linfa: Classical ML Compared
scikit-learn is the standard toolkit for classical ML in Python. Rust's linfa aims to provide a similar experience with algorithms for clustering, classification, preprocessing, and model evaluation—designed for fast, type-safe experimentation in notebooks.
What scikit-learn does
scikit-learn provides a unified interface for classical ML:
- Classification: SVM, random forest, logistic regression, k-NN
- Regression: linear regression, ridge, lasso, decision trees
- Clustering: k-means, DBSCAN, hierarchical clustering
- Preprocessing: scaling, encoding, dimensionality reduction
- Model selection: cross-validation, grid search
- Metrics: accuracy, precision, recall, F1-score
It became the default library for classical ML in Python, used across industry and academia.
What linfa brings to Rust
linfa is a Rust machine learning library:
- Algorithms: k-means, hierarchical clustering, PCA, logistic regression
- Preprocessing: scaling, encoding, feature extraction
- Model evaluation: cross-validation, metrics
- Type-safe APIs that catch errors at compile time
- Modular design with trait-based abstractions
- Performance via native compilation
The key difference: linfa provides a Rust-native approach to classical ML with compile-time type safety and performance benefits, while scikit-learn offers a broader algorithm set with mature ecosystem integration.
Comparison
| Feature | scikit-learn (Python) | linfa (Rust) |
|---|---|---|
| Train model | model.fit(X, y) | model.fit(&dataset) |
| Predict | model.predict(X) | model.predict(&features) |
| K-means clustering | KMeans(n_clusters=3) | KMeans::new_with_params(3) |
| Type safety | Runtime | Compile-time |
| Performance | C-backed via NumPy/scipy | Native compilation |
| Integration | NumPy, pandas, Matplotlib, etc. | ndarray, plotters, serde, etc. |
Example: scikit-learn
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)
labels = kmeans.predict(X)
Example: linfa
use linfa_clustering::KMeans;
let kmeans = KMeans::params(3).fit(&dataset)?;
let labels = kmeans.predict(&dataset);
When to use each
- scikit-learn in Python when you need the broadest algorithm set, mature ecosystem, or integration with pandas and NumPy.
- linfa in Rust when you need type-safe ML operations, performance for large datasets, or want to avoid the Python interpreter overhead.
Try it in Project Mars
Both runtimes share one .mars document: Python cells run in a persistent interpreter, Rust cells on the replay runtime with // :dep Cargo dependencies — and state stays inside each language's own session.
The canonical ML workflow — load data, split, fit, score — in both ecosystems:
# scikit-learn side.
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
clf = LogisticRegression(max_iter=200).fit(X_train, y_train)
clf.score(X_test, y_test)
// :dep ndarray = "0.16"
// :dep rand = "0.8"
use ndarray::Array2;
use rand::Rng;
// linfa-style split + baseline on synthetic data — the shape a
// linfa LogisticRegression::fit call takes.
let mut rng = rand::thread_rng();
let n = 300;
let mut xs = Array2::zeros((n, 2));
let mut ys = Vec::with_capacity(n);
for i in 0..n {
let a = rng.gen_range(-1.0..1.0);
let b = rng.gen_range(-1.0..1.0);
xs[[i, 0]] = a;
xs[[i, 1]] = b;
ys.push(if a - b > 0.0 { 1 } else { 0 });
}
let split = n * 4 / 5;
println!("train rows: {}, test rows: {}", split, n - split);
How Project Mars supports both
Project Mars supports both scikit-learn and linfa in their respective language runtimes:
- Python cells can use scikit-learn with standard library imports
- Rust cells can use linfa via Cargo dependencies
- Both can coexist in the same
.marsnotebook - Models and datasets persist across cells within each language runtime
Start with the getting started guide and the machine learning example to explore scikit-learn and linfa in Project Mars notebooks.