All open-source packages
AISwift

SwiftAI

Machine learning in pure Swift: train, evaluate and save models on device.

9StarsLatest Release: v2.0.0Updated: October 15, 2026
Platforms:iOS 15macOS 12tvOS 15watchOS 8visionOS 1

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

swift
1import SwiftUI
2import SwiftAI
3 
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.localizedDescription
22 }
23 }
24}

Installation

Xcode (File › Add Package Dependencies…)

text
1https://github.com/muhittincamdali/SwiftAI.git

Swift Package Manager

swift
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

Four classifiers trained on the iris petal measurements, with the species each predicts across the plane and their cross-validated accuracy (k-nearest neighbours 96.0%, logistic regression 95.3%, decision tree 94.0%, neural network 96.0%)
Dark version of the decision-boundary image: four classifiers trained on the iris petal measurements and their cross-validated accuracy

Timeline

  1. Repository opened as a broad AI framework with app-architecture layers and Vision and NLP code

  2. Version 1.0.0 released; three days later the app-architecture modules were removed and a smaller machine learning core was written

  3. Trimmed down: the tests shrank to a single placeholder file and small stub layers were added

  4. Rebuilt from scratch as a dependency-free classical machine learning package and released as 2.0.0; no 1.0.0 API remains

Links

Related Article

Read the article