SwiftAI
Machine learning in pure Swift: train, evaluate and save models on device.
License: MIT
SwiftAI is a Swift package for classical machine learning on Apple platforms: classification, regression, clustering and dimensionality reduction on tables of numbers. You load data from CSV or arrays, split it, train a model with one function call, measure it and save it as JSON to load in your app, without a Python toolchain or a server. The package has no dependencies.
Features
- Eleven supervised models: linear and ridge regression, logistic regression, k-nearest neighbours, Gaussian naive Bayes, decision trees, random forests and neural networks, plus k-means clustering and principal component analysis
- Checked against scikit-learn 1.9.1: tests compare coefficients, probabilities and clusters (for example logistic regression probabilities to 1e-5, PCA to 1e-10)
- Errors instead of crashes: invalid data or settings throw a SwiftAIError that says what to change
- Safe to load: models, scalers and pipelines are Codable, and decoding checks shapes, values, class lists and tree structure
- Reproducible: every random step takes a seed, and the same seed gives the same model, also for forests trained in parallel
- Pipeline fits preprocessing on the training data only and applies it the same way to every later sample
- Swift 6 language mode with strict concurrency checking; DocC catalog with four articles and the ModelLab example app for iOS and macOS
Quickstart
1import SwiftUI2import SwiftAI3 4struct ContentView: View {5 var body: some View {6 Text(irisSummary())7 .padding()8 }9 10 func irisSummary() -> String {11 do {12 // 1. Split the built-in iris flowers into training and test samples.13 let (training, test) = try Dataset.iris.stratifiedSplit(testFraction: 0.2, seed: 42)14 // 2. Train a random forest on the training samples.15 let model = try RandomForestClassifier.fit(training, seed: 42)16 // 3. Measure it on the test samples and classify a new flower.17 let accuracy = try model.accuracy(on: test)18 let species = try model.predict([6.3, 2.8, 5.1, 1.5])19 return "Test accuracy: \(accuracy.formatted(.percent))\nNew flower: \(species)"20 } catch {21 return error.localizedDescription22 }23 }24}Installation
Xcode (File › Add Package Dependencies…)
1https://github.com/muhittincamdali/SwiftAI.gitSwift Package Manager
1dependencies: [2 .package(url: "https://github.com/muhittincamdali/SwiftAI.git", from: "2.0.0")3],4targets: [5 .target(name: "YourApp", dependencies: [6 .product(name: "SwiftAI", package: "SwiftAI")7 ])8]Screenshots


Timeline
Repository opened as a broad AI framework with app-architecture layers and Vision and NLP code
Version 1.0.0 released; three days later the app-architecture modules were removed and a smaller machine learning core was written
Trimmed down: the tests shrank to a single placeholder file and small stub layers were added
Rebuilt from scratch as a dependency-free classical machine learning package and released as 2.0.0; no 1.0.0 API remains