libSvm

package module
Version: v0.0.0-...-ec84452 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2014 License: Apache-2.0 Imports: 12 Imported by: 13

README

libsvm-go: Support Vector Machine

This is a full port of LIBSVM in the Go programming language. LIBSVM is a suite of tools and an API library for support vector classification, regression, and distribution estimation. This port implements the libsvm library in the form of a Go package called libSvm. It also implements the svm-train and svm-predict command line tools.

This port has no external package dependencies, and uses only the native standard library.

Installation

go get github.com/ewalker544/libsvm-go
make

Compatibility Notes

I have tried to make the Go implementation of svm-train and svm-predict plug-in compatibile with the original LIBSVM 3.18 distribution. This is to allow you to use the other tools available in the original distribution, like easy.py and grid.py.

svm-predict should be 100% plug-in compatibile. However, svm-train is plug-in compatible with one exception. The exception is the parameter weight flag used in the command. In this implementation, the flag is

-w i,weight : set the parameter C of class i to weight*C, for C-SVC (default 1)

For full documentation of the svm-train and svm-predict commands, please refer to the original LIBSVM web site.

API Example

Training
import "github.com/ewalker544/libsvm-go"
    
param := libSvm.NewParameter()      // Create a parameter object with default values
param.KernelType = libSvm.POLY      // Use the polynomial kernel
    
model := libSvm.NewModel(param)     // Create a model object from the parameter attributes
    
// Create a problem specification from the training data and parameter attributes
problem, err := libSvm.NewProblem("a9a.train", param) 
    
model.Train(problem)                // Train the model from the problem specification
    
model.Dump("a9a.model")             // Dump the model into a user-specified file
Predicting
import "github.com/ewalker544/libsvm-go"
    
// Create a model object from the model file generated from training
model := libSvm.NewModelFromFile("a9a.model")  
    
x := make(map[int]float64)
// Populate x with the test vector
    
predictLabel := model.Predict(x)    // Predicts a float64 label given the test vector 

Documentation

Overview

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Caches Q matrix rows. The cache implements a LRU (Last Recently Used) eviction policy. ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Implements the linear, radial-basis function, sigmoid, and polynomial kernels ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Model describes the properties of the Support Vector Machine after training. ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Input/output routines for the Support Vector Machine model ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Useful types/methods for running loops in parallel. ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Describes the parameters of the Supper Vector Machine solver ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Prediciton related APIs ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Probability estimation APIs ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Describes problem, i.e. label/vector set ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Q matrix for Support Vector Classification (svcQ), Support Vector Regression (svrQ), ** and One-Class Support Vector Machines (oneClassQ) ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Sequential Minimal Optimization (SMO) solver ** Ref: C.-C. Chang, C.-J. Lin. "LIBSVM: A library for support vector machines". ACM Transactions on Intelligent Systems and Technology 2 (2011) ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Functions for calling the solver for different problem scenerios, i.e. SVC, SVR, or One-Class ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Useful functions used in various parts of the library ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Working-set selection ** Ref: R.-E. Fan, P.-H. Chen, and C.-J. Lin. "Working set selection using second order information for training SVM". Journal of Machine Learning Research 6 (2005) ** @author: Ed Walker

** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http ://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ** ** Description: Cross validation API ** @author: Ed Walker

Index

Constants

View Source
const (
	C_SVC       = iota
	NU_SVC      = iota
	ONE_CLASS   = iota
	EPSILON_SVR = iota
	NU_SVR      = iota
)
View Source
const (
	LINEAR      = iota
	POLY        = iota
	RBF         = iota
	SIGMOID     = iota
	PRECOMPUTED = iota
)
View Source
const (
	LOWER_BOUND = iota
	UPPER_BOUND = iota
	FREE        = iota
)
View Source
const LibSvmGoVersion = 0.318
View Source
const TAU float64 = 1e-12

Variables

This section is empty.

Functions

func CrossValidation

func CrossValidation(prob *Problem, param *Parameter, nrFold int) (target []float64)

* * This function conducts cross validation. Data are separated to

nrFold folds. Under given parameters, sequentially each fold is
validated using the model from training the remaining. Predicted
labels (of all prob's instances) in the validation process are
stored in the slice called target.

func MapToSnode

func MapToSnode(m map[int]float64) []snode

func SnodeToMap

func SnodeToMap(x []snode) map[int]float64

Types

type Model

type Model struct {
	// contains filtered or unexported fields
}

func NewModel

func NewModel(param *Parameter) *Model

func NewModelFromFile

func NewModelFromFile(file string) *Model

func (*Model) Dump

func (model *Model) Dump(file string) error

func (Model) NrClass

func (model Model) NrClass() int

func (Model) Predict

func (model Model) Predict(x map[int]float64) float64

* * This function does classification or regression on a test vector x

given a model.

For a classification model, the predicted class for x is returned.
For a regression model, the function value of x calculated using
the model is returned. For an one-class model, +1 or -1 is
returned.

func (Model) PredictProbability

func (model Model) PredictProbability(x map[int]float64) (returnValue float64, probabilityEstimate []float64)

* * This function does classification or regression on a test vector x

given a model with probability information.

For a classification model with probability information, this
function gives nrClass probability estimates in the slice
probabilityEstimate. The class with the highest probability is returned
in returnValue. For regression/one-class SVM, probabilityEsstimate is nil,
and returnValue is the same as that of Predict.

func (Model) PredictValues

func (model Model) PredictValues(x map[int]float64) (returnValue float64, decisionValues []float64)

* * This function gives decision values on a test vector x given a

model, and return the predicted label (classification) or
the function value (regression).

For a classification model with nrClass classes, this function
gives nrClass*(nrClass-1)/2 decision values in the slice
decisionValues.  The order is label[0] vs. label[1], ...,
label[0] vs. label[nr_class-1], label[1] vs. label[2], ...,
label[nrClass-2] vs. label[nrClass-1]. The returned returnValue is
the predicted class for x. Note that when nrClass = 1, this
function does not give any decision value.

For a regression model, decisionValues[0] and the returned returnValue are
both the function value of x calculated using the model. For a
one-class model, decisionValues[0] is the decision value of x, while
the returned returnValue is +1/-1.

func (*Model) ReadModel

func (model *Model) ReadModel(file string) error

func (*Model) Train

func (model *Model) Train(prob *Problem) error

type Parameter

type Parameter struct {
	SvmType    int     // Support vector type
	KernelType int     // Kernel type
	Degree     int     // Degree used in polynomial kernel
	Gamma      float64 // Gamma used in rbf, polynomial, and sigmoid kernel
	Coef0      float64 // Coef0 used in polynomial and sigmoid kernel

	Eps         float64 // stopping criteria
	C           float64 // penality
	NrWeight    int
	WeightLabel []int
	Weight      []float64
	Nu          float64
	P           float64
	Probability bool // Should probability estimation be performed?
	CacheSize   int  // Size of Q matrix cache
	QuietMode   bool // quiet mode
	NumCPU      int  // Number of CPUs to use
}

func NewParameter

func NewParameter() *Parameter

type Problem

type Problem struct {
	// contains filtered or unexported fields
}

func NewProblem

func NewProblem(file string, param *Parameter) (*Problem, error)

func (*Problem) Begin

func (problem *Problem) Begin()

*

* Initialize the start of iterating through the labels and vectors in the problem set

func (*Problem) Done

func (problem *Problem) Done() bool

*

* Finished iterating through all the labels and vectors in the problem set

func (*Problem) GetLine

func (problem *Problem) GetLine() (y float64, x map[int]float64)

*

* Return one label and vector from the problem set
* @return y label
* @return x vector (map of dimension/value)

func (*Problem) Next

func (problem *Problem) Next()

*

* Move to the next label and vector in the problem set

func (*Problem) ProblemSize

func (problem *Problem) ProblemSize() int

*

* Returns number of label and vectors in the problem set
* @return problem set size

func (*Problem) Read

func (problem *Problem) Read(file string, param *Parameter) error

type SquareErrorComputer

type SquareErrorComputer struct {
	// contains filtered or unexported fields
}

func NewSquareErrorComputer

func NewSquareErrorComputer() SquareErrorComputer

func (*SquareErrorComputer) MeanSquareError

func (s *SquareErrorComputer) MeanSquareError() (err float64)

func (*SquareErrorComputer) SquareCorrelationCoeff

func (s *SquareErrorComputer) SquareCorrelationCoeff() (coeff float64)

func (*SquareErrorComputer) Sum

func (s *SquareErrorComputer) Sum(predict, target float64)

Directories

Path Synopsis
cmds
svm-predict
** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License.
** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License.
svm-train
** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License.
** Copyright 2014 Edward Walker ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL