GithubHelp home page GithubHelp logo

lasseregin / svm-w-smo Goto Github PK

View Code? Open in Web Editor NEW
118.0 5.0 55.0 10 KB

Simple implementation of a Support Vector Machine using the Sequential Minimal Optimization (SMO) algorithm for training.

License: MIT License

Python 100.00%

svm-w-smo's Introduction

SVM

Simple implementation of a Support Vector Machine using the Sequential Minimal Optimization (SMO) algorithm for training.

Supported python versions:

  • Python 2.7
  • Python 3.4

Python package dependencies

Documentation

Setup model (following parameters are default)

from SVM import SVM
model = SVM(max_iter=10000, kernel_type='linear', C=1.0, epsilon=0.001)

Train model

model.fit(X, y)

Predict new observations

y_hat = model.predict(X_test)

svm-w-smo's People

Contributors

av4l4nc14 avatar lasseregin avatar oooook0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

svm-w-smo's Issues

I think I might have found a bug.

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()

calc_w function issue

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

calc_w function

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.