LasseRegin / SVM-w-SMO

Simple implementation of a Support Vector Machine using the Sequential Minimal Optimization (SMO) algorithm for training.
MIT License
117 stars 55 forks source link

I think I might have found a bug. #5

Open daidai21 opened 5 years ago

daidai21 commented 5 years ago

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()
ddbourgin commented 5 years ago

Public service announcement: @daidai21 just tried to pass your code off as their own in my repo (https://github.com/ddbourgin/numpy-ml/issues/31 and https://github.com/ddbourgin/numpy-ml/pull/37).

LasseRegin commented 5 years ago

Thank you very much for letting me know @ddbourgin and for the reference in your repo.

@daidai21 feel free to use my code, but please don't refer to it as your own. Thank you for posting the issue. I will look into it.