Support Vector Machines (SVMs)

class mlpy.Svm(kernel='linear', kp=0.10000000000000001, C=1.0, tol=0.001, eps=0.001, maxloops=1000, cost=0.0, alpha_tversky=1.0, beta_tversky=1.0, opt_offset=True)

Support Vector Machines (SVM).

Example:
>>> import numpy as np
>>> import mlpy
>>> xtr = np.array([[1.0, 2.0, 3.0, 1.0],  # first sample
...                 [1.0, 2.0, 3.0, 2.0],  # second sample
...                 [1.0, 2.0, 3.0, 1.0]]) # third sample
>>> ytr = np.array([1, -1, 1])             # classes
>>> mysvm = mlpy.Svm()                     # initialize Svm class
>>> mysvm.compute(xtr, ytr)                # compute SVM
1
>>> mysvm.predict(xtr)                     # predict SVM model on training data
array([ 1, -1,  1])
>>> xts = np.array([4.0, 5.0, 6.0, 7.0])   # test point
>>> mysvm.predict(xts)                     # predict SVM model on test point
-1
>>> mysvm.realpred                         # real-valued prediction
-5.5
>>> mysvm.weights(xtr, ytr)                # compute weights on training data
array([ 0.,  0.,  0.,  1.])

Initialize the Svm class

Parameters:
kernel : string [‘linear’, ‘gaussian’, ‘polynomial’, ‘tr’, ‘tversky’]

kernel

kp : float

kernel parameter (two sigma squared) for gaussian and polynomial kernel

C : float

regularization parameter

tol : float

tolerance for testing KKT conditions

eps : float

convergence parameter

maxloops : integer

maximum number of optimization loops

cost : float [-1.0, ..., 1.0]

for cost-sensitive classification

alpha_tversky : float

positive multiplicative parameter for the norm of the first vector

beta_tversky : float

positive multiplicative parameter for the norm of the second vector

opt_offset : bool

compute the optimal offset

compute(x, y)

Compute SVM model

Parameters:
x : 2d ndarray float (samples x feats)

training data

y : 1d ndarray integer (-1 or 1)

classes

Returns:
conv : integer

svm convergence (0: false, 1: true)

predict(p)

Predict svm model on a test point(s)

Parameters:
p : 1d or 2d ndarray float (samples x feats)

test point(s)training dataInput

Returns:
cl : integer or 1d ndarray integer

class(es) predicted

Attributes:
Svm.realpred : float or 1d ndarray float

real valued prediction

weights(x, y)

Return feature weights

Parameters:
x : 2d ndarray float (samples x feats)

training data

y : 1d ndarray integer (-1 or 1)

classes

Returns:
fw : 1d ndarray float

feature weights

Note

For tr kernel (Terminated Ramp Kernel) see [Merler06].

[Vapnik95]V Vapnik. The Nature of Statistical Learning Theory. Springer-Verlag, 1995.
[Cristianini]N Cristianini and J Shawe-Taylor. An introduction to support vector machines. Cambridge University Press.
[Merler06]S Merler and G Jurman. Terminated Ramp - Support Vector Machine: a nonparametric data dependent kernel. Neural Network, 19:1597-1611, 2006.
[Nasr09]
  1. Nasr, S. Swamidass, and P. Baldi. Large scale study of multiplemolecule queries. Journal of Cheminformatics, vol. 1, no. 1, p. 7, 2009.

Previous topic

Classification

Next topic

K Nearest Neighbor (KNN)

This Page