liblinear

package module
v0.0.0-...-7496d41 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2015 License: MIT Imports: 8 Imported by: 0

README

LIBLINEAR for Go

Build Status

This is a Golang wrapper for LIBLINEAR (C.-J. Lin et al.) (GitHub). Note that the interface of this package might be slightly different from liblinear C interface because of Go convention. Yet, I'll try to align the function name and functionality to liblinear C library.

GoDoc: Document.

Introduction to LIBLINEAR

LIBLINEAR is a linear classifier for data with millions of instances and features. It supports

  • L2-regularized classifiers
  • L2-loss linear SVM, L1-loss linear SVM, and logistic regression (LR)
  • L1-regularized classifiers (after version 1.4)
  • L2-loss linear SVM and logistic regression (LR)
  • L2-regularized support vector regression (after version 1.9)
  • L2-loss linear SVR and L1-loss linear SVR.

Install

This package depends on LIBLINEAR 2.01+. Please install it first via Homebrew or other package managers on your OS:

brew update
brew info liblinear # make sure your formula will install version higher than 2.01
brew install liblinear

After liblinear installation, just go get this package

go get github.com/lazywei/liblinear

Usage

The package is based on mat64.

import linear "github.com/lazywei/liblinear"

// ReadLibsvm(filepath string, oneBased bool) (X, y *mat64.Dense)
X, y := linear.ReadLibsvm("heart_scale", true)

// Train(X, y *mat64.Dense, bias float64, solverType int,
// 	eps, C_, p float64,
// 	classWeights map[int]float64) (*Model)
// Please checkout liblinear's doc for the explanation for these parameters.
model := Train(X, y, -1, 0, 0.01, 1.0, 0.1, map[int]float64{1: 1, -1: 1})
y_pred:= linear.Predict(model, X)

fmt.Println(linear.Accuracy(y, y_pred))

Self-Promotion

This package is mainly built because of mockingbird, which is a programming language classifier in Go. Mockingbird is my Google Summer of Code 2015 Project with GitHub and linguist. If you like it, please feel free to follow linguist, mockingbird, and this library.

Roadmap

  • Wrap core functions
    • Train
    • Predict
    • LIBSVM format reader
    • Predict Probability
    • Cross Validation
    • Save Model / Load Model
  • Better Wrapper for liblinear's C struct
    • Model (wrap struct model)
    • FeatureNode (wrap struct feature_node)
    • Parameter (wrap struct parameter)
    • Problem (wrap struct problem)
  • Abstraction for classifier: encapsulate model

Documentation

Index

Constants

View Source
const (
	// Solver Type
	L2R_LR              = int(C.L2R_LR)
	L2R_L2LOSS_SVC_DUAL = int(C.L2R_L2LOSS_SVC_DUAL)
	L2R_L2LOSS_SVC      = int(C.L2R_L2LOSS_SVC)
	L2R_L1LOSS_SVC_DUAL = int(C.L2R_L1LOSS_SVC_DUAL)
	MCSVM_CS            = int(C.MCSVM_CS)
	L1R_L2LOSS_SVC      = int(C.L1R_L2LOSS_SVC)
	L1R_LR              = int(C.L1R_LR)
	L2R_LR_DUAL         = int(C.L2R_LR_DUAL)
	L2R_L2LOSS_SVR      = int(C.L2R_L2LOSS_SVR)
	L2R_L2LOSS_SVR_DUAL = int(C.L2R_L2LOSS_SVR_DUAL)
	L2R_L1LOSS_SVR_DUAL = int(C.L2R_L1LOSS_SVR_DUAL)
)

Variables

This section is empty.

Functions

func Accuracy

func Accuracy(y_true, y_pred *mat64.Dense) float64

func Predict

func Predict(model *Model, X *mat64.Dense) *mat64.Dense

double predict(const struct model *model_, const struct feature_node *x);

func PredictProba

func PredictProba(model *Model, X *mat64.Dense) *mat64.Dense

double predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates);

func ReadLibsvm

func ReadLibsvm(filepath string, oneBased bool) (X, y *mat64.Dense)

ReadLibsvm reads libsvm format data from `filepath`. `oneBased` denotes the index of data stored in the file starts from 1 (`oneBased=true`) or 0 (`oneBased=false`). Returned X, y is of dimension (nSamples, nFeatures) and (nSamples, 1) respectively.

func SaveModel

func SaveModel(model *Model, filename string)

Types

type FeatureNode

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

func NewFeatureNode

func NewFeatureNode(index int, value float64) *FeatureNode

func (*FeatureNode) GetData

func (fn *FeatureNode) GetData() (int, float64)

func (*FeatureNode) GetPtr

func (fn *FeatureNode) GetPtr() *C.struct_feature_node

type Model

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

func LoadModel

func LoadModel(filename string) *Model

func Train

func Train(X, y *mat64.Dense, bias float64, pm *Parameter) *Model

Wrapper for the `train` function in liblinear.

`model* train(const struct problem *prob, const struct parameter *param);`

The explanation of parameters are:

solverType:

for multi-class classification
       0 -- L2-regularized logistic regression (primal)
       1 -- L2-regularized L2-loss support vector classification (dual)
       2 -- L2-regularized L2-loss support vector classification (primal)
       3 -- L2-regularized L1-loss support vector classification (dual)
       4 -- support vector classification by Crammer and Singer
       5 -- L1-regularized L2-loss support vector classification
       6 -- L1-regularized logistic regression
       7 -- L2-regularized logistic regression (dual)
for regression
      11 -- L2-regularized L2-loss support vector regression (primal)
      12 -- L2-regularized L2-loss support vector regression (dual)
      13 -- L2-regularized L1-loss support vector regression (dual)

eps is the stopping criterion.

C_ is the cost of constraints violation.

p is the sensitiveness of loss of support vector regression.

classWeights is a map from int to float64, with the key be the class and the value be the weight. For example, {1: 10, -1: 0.5} means giving weight=10 for class=1 while weight=0.5 for class=-1

If you do not want to change penalty for any of the classes, just set classWeights to nil.

type Parameter

type Parameter struct {

	// 	/* these are for training only */
	// 	double eps;	        /* stopping criteria */
	// 	double C;
	// 	int nr_weight;
	// 	int *weight_label;
	// 	double* weight;
	// 	double p;
	// 	double *init_sol;
	// };
	SolverType   int
	Eps, C, P    float64
	ClassWeights map[int]float64
}

func (*Parameter) GetPtr

func (pm *Parameter) GetPtr() *C.struct_parameter

Jump to

Keyboard shortcuts

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