ml_svm 0.1.0 copy "ml_svm: ^0.1.0" to clipboard
ml_svm: ^0.1.0 copied to clipboard

Native Dart implementation of Support Vector Machine (SVM) with linear classification, hinge loss, and multi-class OvR support.

🧠 ml_svm – Native Dart Support Vector Machine (SVM) #

A fully Dart-native implementation of the Support Vector Machine (SVM) algorithm.
This library supports binary and multi-class classification using hinge loss + L2 regularization, optimized with Float64List vectors and zero dependencies.

⚑ No Python, no bindings β€” just pure Dart for real machine learning on Flutter, CLI, or embedded apps.


πŸ“˜ What is SVM? #

Support Vector Machine (SVM) is a supervised learning algorithm used for classification and regression.
It works by finding the hyperplane that best separates different classes in a high-dimensional space.

This package implements:

  • Linear SVM
  • Hinge Loss
  • L2 Regularization
  • Gradient Descent Optimization
  • One-vs-Rest (OvR) for multi-class classification

πŸš€ Features #

βœ… Linear classification
βœ… Binary and multi-class (OvR) support
βœ… Optimized for Dart's Float64List
βœ… Gradient descent training
βœ… Intercept/bias term
βœ… Utility functions: normalization, shuffling
βœ… MIT License – Free for commercial or academic use


πŸ“¦ Installation #

dart pub add ml_svm

⚑ Quick Example #

import 'dart:typed_data';
import 'package:ml_svm/ml_svm.dart';

void main() {
  // Training dataset: [height, weight]
  final features = <Float64List>[
    Float64List.fromList([160, 50]),
    Float64List.fromList([170, 65]),
    Float64List.fromList([180, 80]),
    Float64List.fromList([155, 48]),
  ];

  final labels = ['underweight', 'normal', 'overweight', 'underweight'];

  // Normalize input features
  final normalized = normalize(features);

  // Train the SVM model
  final model = SVMModel(
    learningRate: 0.01,
    regularizationC: 1.0,
    maxIterations: 500,
  );

  model.fit(normalized, labels);

  // Predict a new sample
  final testSample = normalize([
    Float64List.fromList([172, 70])
  ]).first;

  print('Prediction: ${model.predict(testSample)}');
  print('Scores: ${model.decisionFunction(testSample)}');
}

πŸ“Œ Expected Output:

Prediction: normal
Scores: {underweight: -0.12, normal: 0.53, overweight: 0.42}

πŸ“š API Documentation #

SVMModel Constructor #

SVMModel({
  double learningRate = 0.01,
  double regularizationC = 1.0,
  int maxIterations = 1000,
  double tolerance = 1e-4,
});

Main Methods #

Method Description
fit(List<Float64List> X, List<String> y) Train the model using labeled dataset
predict(Float64List x) Predict the label for a new input sample
decisionFunction(Float64List x) Return class scores (margin values)
classes Returns list of learned class labels
weights Access learned weights
biases Access learned intercepts

🧰 Utility Functions #

  • normalize(List<Float64List> X) β†’ Min-max scaling to [0, 1]
  • shuffleInPlace(List data, List labels, {int? seed}) β†’ Random shuffle with labels synced

πŸ”¬ Performance Notes #

  • Designed for small to medium-sized datasets
  • Optimized for embedded & offline ML
  • Uses CPU-friendly gradient descent (not SMO or QP)
  • Not suitable for kernel methods or large-scale vector spaces

πŸ“Œ Use Cases #

  • πŸŽ“ Teaching & academic learning in Dart
  • πŸ“± Lightweight ML inside Flutter apps
  • πŸ” Edge/IoT classification without TensorFlow
  • βš™οΈ Dart-native AI pipelines and decision systems

πŸ§ͺ Testing #

Run all tests:

dart test

Test coverage includes:

  • Model training and prediction
  • Class scores with decisionFunction
  • Label consistency & structure

πŸ”— Author & Contributions #

Created with ❀️ by Mehmet Γ‡elik
If you found this useful, consider ⭐ starring the project and following for more!

Contributions are welcome! Open issues, suggest features, or submit PRs.


πŸ“„ License #

This project is licensed under the MIT License.
See the LICENSE file for details.

0
likes
150
points
23
downloads

Publisher

unverified uploader

Weekly Downloads

Native Dart implementation of Support Vector Machine (SVM) with linear classification, hinge loss, and multi-class OvR support.

Repository (GitHub)
View/report issues

Topics

#svm #classifier #ml #offline-ml #native

Documentation

API reference

License

MIT (license)

More

Packages that depend on ml_svm