ml_svm 0.1.0
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.