Skip to main content

TinySVM 🚀

A lightweight, dependency-free (NumPy only), and educational implementation of Support Vector Machines (SVM) in pure Python.

TinySVM implements both Classification (SVC) and Regression (SVR) with a simplified SMO (Sequential Minimal Optimization) and Coordinate Descent algorithm. It serves as a great learning resource or a drop-in replacement for heavy libraries when you only need basic SVM functionality.

✨ Features

  • Zero Dependencies: Only requires numpy. No scikit-learn or scipy needed.
  • Full-featured:
    • Binary & Multi-class Classification (One-vs-Rest).
    • Regression (Single & Multi-output).
    • Kernel Support: Linear & RBF (Gaussian).
  • Production Ready-ish:
    • Built-in Auto-Scaling (StandardScaler).
    • Probability estimates (predict_proba).
    • Scikit-learn compatible API (fit, predict, score).
  • Tiny: Less than 300 lines of code.

📦 Installation

Just copy tinysvm.py to your project folder. Yes, it's that simple!

Dependencies:

pip install numpy

⚡ Quick Start

Classification (XOR Problem)

from tinysvm import TinySVM
import numpy as np

# XOR data (Linear Separable? No.)
X = [[0, 0], [1, 1], [1, 0], [0, 1]]
y = [0, 0, 1, 1]

# Initialize with RBF kernel and Auto-Scaling
clf = TinySVM(mode='classification', kernel='rbf', gamma=2.0, C=10.0, scaling=True)
clf.fit(X, y)

print(f"Prediction: {clf.predict([[0, 1]])}") # Output: [1]
print(f"Accuracy:   {clf.score(X, y)}")       # Output: 1.0

Regression

# Simple Linear Regression
X = [[1], [2], [3], [4], [5]]
y = [3, 5, 7, 9, 11] # y = 2x + 1

reg = TinySVM(mode='regression', kernel='linear', C=50.0)
reg.fit(X, y)

print(f"Prediction for x=6: {reg.predict([[6]])}") # Should be close to 13

⚙️ API Reference

TinySVM(mode, C, kernel, gamma, scaling, ...)

  • mode: 'classification' or 'regression'.
  • C: Regularization parameter (default 1.0).
  • kernel: 'rbf' (default) or 'linear'.
  • gamma: Kernel coefficient for RBF.
  • scaling: Boolean. If True (default), automatically scales data using Z-score.

Methods

  • fit(X, y): Train the model.
  • predict(X): Predict class or value.
  • predict_proba(X): (Classification only) Estimate class probabilities.
  • score(X, y): Returns Accuracy (Classification) or R² (Regression).
  • save(path) / load(path): Save/Load model state.

📜 License

MIT License. Feel free to use it in your own projects!

Metadata

Release files for tinysvm 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tinysvm 0.1.0
File Size Uploaded
tinysvm-0.1.0.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tinysvm 0.1.0
File Interpreter ABI Platform
tinysvm-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.3 kB

Release files / tinysvm-0.1.0.tar.gz

Download URL tinysvm-0.1.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
314b17ee5a6b93eeba4234d53b32e6115cc3dfc231043e87869c1fc252629897
BLAKE2b-256 checksum
How to use checksums
07b9d48804c8909abd93690ba5df8f1461ac8207c057793056bd49af8dd5828f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.0

Release files / tinysvm-0.1.0-py3-none-any.whl

Download URL tinysvm-0.1.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9db5c29db45f409e373bb83ad9426ce63e0715e4378a593429c31754db02eda5
BLAKE2b-256 checksum
How to use checksums
3a4ac91e4fe3e990c92d05d5361b96345e96c4bfc4628edc2413d5e7cf4d6bdc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page