Skip to main content

A lightweight, dependency-free (NumPy only) SVM implementation.

Project description

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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tinysvm-0.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tinysvm-0.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file tinysvm-0.1.0.tar.gz.

File metadata

  • Download URL: tinysvm-0.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for tinysvm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 314b17ee5a6b93eeba4234d53b32e6115cc3dfc231043e87869c1fc252629897
MD5 2f9e2fab70fb24cca7be0e9bb5aecc0a
BLAKE2b-256 07b9d48804c8909abd93690ba5df8f1461ac8207c057793056bd49af8dd5828f

See more details on using hashes here.

File details

Details for the file tinysvm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tinysvm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for tinysvm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9db5c29db45f409e373bb83ad9426ce63e0715e4378a593429c31754db02eda5
MD5 bf1cb62d1ac22d4b1b7a8e518b88a37d
BLAKE2b-256 3a4ac91e4fe3e990c92d05d5361b96345e96c4bfc4628edc2413d5e7cf4d6bdc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page