A native Swift inference library for scikit-learn models. Train your models in Python, run predictions natively in Swift.
- Train in Python, Deploy in Swift: Use familiar sklearn workflows, then run inference on Apple platforms without Python dependencies
- Native Performance: Built on Apple's Accelerate framework (vDSP) for vectorized operations
- No Runtime Dependencies: Pure Swift - no Python bridge, no CoreML conversion
- Type Safe: Leverages Swift's type system for compile-time safety
- Train your model in Python using scikit-learn
- Export model weights to JSON/H5
- Load weights in Swift and run
predict/predict_proba
# Python: Train and export
from sklearn.preprocessing import StandardScaler
import json
scaler = StandardScaler()
scaler.fit(X_train)
# Export weights
weights = {
"mean_": scaler.mean_.tolist(),
"scale_": scaler.scale_.tolist()
}
json.dump(weights, open("scaler.json", "w"))// Swift: Load and predict
import SwiftLearn
let scaler = try StandardScaler.load(from: "scaler.json")
let predictions = try scaler.transform(newData)Add to your Package.swift:
dependencies: [
.package(url: "https://github.com/aaryanshsahay/SwiftLearn.git", branch: "dev")
]Or in Xcode: File > Add Package Dependencies > Enter the repository URL.
import SwiftLearn
// Create data
let X = Matrix([
[0.0, 0.0],
[0.0, 0.0],
[1.0, 1.0],
[1.0, 1.0]
])
// StandardScaler example
var scaler = StandardScaler()
let scaled = try scaler.fitTransform(X)
print(scaler.mean_!) // [0.5, 0.5]
print(scaler.scale_!) // [0.5, 0.5]
// Transform new data
let newData = Matrix([[2.0, 2.0]])
let result = try scaler.transform(newData)| Class | Description |
|---|---|
StandardScaler |
Standardize features by removing mean and scaling to unit variance |
| Type | Description |
|---|---|
Matrix |
2D array with row-major storage, supports subscripting and column access |
- StandardScaler
- MinMaxScaler
- LabelEncoder
- OneHotEncoder
- Load model weights from JSON
- Load model weights from H5
- Linear Regression (
predict) - Logistic Regression (
predict,predict_proba) - K-Nearest Neighbors (
predict,predict_proba) - Decision Trees (
predict,predict_proba) - Random Forest (
predict,predict_proba) - SVM (
predict,decision_function)
- Train/Test Split
- Accuracy, Precision, Recall metrics
- Swift 5.9+
- macOS 10.15+ / iOS 13+ / tvOS 13+ / watchOS 6+
Contributions are welcome! Please open an issue or submit a pull request.
MIT License - see LICENSE file for details.