lasseregin / svm-w-smo Goto Github PK
View Code? Open in Web Editor NEWSimple implementation of a Support Vector Machine using the Sequential Minimal Optimization (SMO) algorithm for training.
License: MIT License
Simple implementation of a Support Vector Machine using the Sequential Minimal Optimization (SMO) algorithm for training.
License: MIT License
Hello! Excuse me.
I think this kernel method quadratic
have error.
This is my test code.
import warnings
warnings.filterwarnings('ignore')
import numpy as np
import random
# load yourself model
from SVM import SVM
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.datasets.samples_generator import make_blobs
from sklearn.model_selection import train_test_split
def test_SVM():
i = 1
np.random.seed(12345)
while True:
# generate dataset
X, Y = make_blobs(
n_samples=np.random.randint(2, 100),
n_features=np.random.randint(2, 100),
centers=2, random_state=i,
)
X, X_test, Y, Y_test = train_test_split(X, Y, test_size=0.3, random_state=i)
# ignore split error(train/test data only 1 class)
if 0 not in Y or 1 not in Y:
continue
# generate param
C = random.uniform(0.1, 0.9)
max_iter = random.uniform(50, 500)
kernel = "quadratic"
tol = random.uniform(0.000001, 0.1)
# fit and predict
clf = SVM(C=C, max_iter=max_iter, kernel=kernel, tol=tol)
clf.fit(X, Y)
pred = clf.predict(X_test)
print(accuracy_score(Y_test, pred))
if __name__ == "__main__":
test_SVM()
Hey Lasse,
When I tried your SVM, it seems calc_w was not working properly as alpha*y
calculated the dot product of these two vectors so that your function doesn't really return the right w
. I replace your function with mine below, which works. Let me know what you think, I can do a pull request to correct it.
def calc_w(self, alpha, y, X):
new = np.multiply(alpha,y)
return np.dot(X.T, new)
Yitao
Hey Lasse,
The calc_w function in your SVM function seems wrong, it should return a vector but it ends up with a matrix. I rewrote the calc_w as follow, it worked, let me know what you think.
def calc_w(self, alpha, y, X):
X_t = X.transpose()
X_ta = np.matmul(X_t, X)
X_tai = np.linalg.inv(X_ta)
ols = np.matmul(X_tai, X_t)
ols = np.matmul(ols, y)
return ols
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.