A simple Support Vector Machine for Binary Classification.
Project description
simplesvm
A simple Support Vector Machine for binary classification with scikit-learn like API.
- Repository: https://github.com/obizip/simplesvm
- About this SVM (ja)
Installation
pip install simplesvm
How to use
LienearSVM
from simplesvm import LinearSVM
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_blobs
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
np.random.seed(42)
X, y = make_blobs(random_state=8,
n_samples=500,
n_features=2,
cluster_std=3,
centers=2)
X_train, X_test, y_train, y_test = train_test_split(X, y)
model = LinearSVM()
model.fit(X_train, y_train)
preds = model.predict(X_test)
print(f"ACC: {accuracy_score(y_test, preds)}")
# ACC: 0.936
plt.figure(figsize=(8, 7))
plt.scatter(X[:, 0], X[:, 1], marker='o', c=y, s=25, edgecolor='k')
w = model._w
# w0*x + w1*y + w2 = 0
# y = - (w0*x + w2) / w1
plt.plot(X[:, 0], - (w[0] * X[:, 0] + w[2]) / w[1])
KernelSVM
from simplesvm import KernelSVM
import numpy as np
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
np.random.seed(42)
X, y = make_moons(n_samples=500, noise=0.1, random_state=1)
X_train, X_test, y_train, y_test = train_test_split(X, y)
model = KernelSVM()
model.fit(X_train, y_train)
preds = model.predict(X_test)
print(f"ACC: {accuracy_score(y_test, preds)}")
#> ACC: 0.992
# Plot a decision boundary
x_min=X[:, 0].min() - 0.5
x_max=X[:, 0].max() + 0.5
y_min=X[:, 1].min() - 0.5
y_max=X[:, 1].max() + 0.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01), np.arange(y_min, y_max, 0.01))
XY = np.array([xx.ravel(), yy.ravel()]).T
z = model.predict(XY)
plt.contourf(xx, yy, z.reshape(xx.shape), alpha=0.2, cmap=plt.cm.coolwarm)
plt.scatter(X[:, 0], X[:, 1], c=y, s=10, cmap=plt.cm.coolwarm)
plt.show()
License
simplesvm
is distributed under the terms of the MIT license.
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
simplesvm-0.0.6.tar.gz
(173.1 kB
view details)
Built Distribution
File details
Details for the file simplesvm-0.0.6.tar.gz
.
File metadata
- Download URL: simplesvm-0.0.6.tar.gz
- Upload date:
- Size: 173.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
84d4566c445bf3806fb1742ff6e21f61716dda2e3043bc5dd59f591ded396d46
|
|
MD5 |
1e0b6967c0e028b89ef7a74b72e74da7
|
|
BLAKE2b-256 |
453c62a9a112ae5b983c5f66de97b83476facdfc6da6ef980406debf999bb2d6
|
File details
Details for the file simplesvm-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: simplesvm-0.0.6-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
663db2c858b3cd5de2b55e3c9f1cdcaf9de491b0ee92aa9f3c741d5484e91008
|
|
MD5 |
6305ff5287dabb1452fe716078323e47
|
|
BLAKE2b-256 |
fad83688f6584a0ccc90ac51de106af8a02bd5fbe5937c481846f145aec08ecd
|