lineargo

package module
v0.0.0-...-48eb5e8 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2019 License: MIT Imports: 9 Imported by: 0

README

LinearGo: LIBLINEAR for Go

Build Status Go Report Card

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.1+ and Go 1.6+. Please install them 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.1
brew install liblinear

brew info go # make sure version 1.6+
brew install go

After liblinear installation, just go get this package

go get github.com/lazywei/lineargo

Usage

The package is based on mat64.

import linear "github.com/lazywei/lineargo"

// 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,
// 	C_, p, eps float64,
// 	classWeights map[int]float64) (*Model)
// Please checkout liblinear's doc for the explanation for these parameters.
model := linear.Train(X, y, -1, linear.L2R_LR, 1.0, 0.1, 0.01, map[int]float64{1: 1, -1: 1})
y_pred:= linear.Predict(model, X)

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

Predict probability with model's labels

import linear "github.com/lazywei/lineargo"

// 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,
// 	C_, p, eps float64,
// 	classWeights map[int]float64) (*Model)
// Please checkout liblinear's doc for the explanation for these parameters.
model := linear.Train(X, y, -1, linear.L2R_LR, 1.0, 0.1, 0.01, map[int]float64{1: 1, -1: 1})

predictProba:= linear.PredictProba(model, X)

// After you have predicted result 
// you can resolve accuracy with existing labels from saved model
labels := linear.Labels(model) // Return []float64 slice of the labels

// You could view percent probability prediction
for idx, pValues := range predictProba {
    // You can format number for probability prediction
    fmt.Printf("Predicted values by label: %d, %f\n", int(labels[idx]), pValues*100)
}

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.

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 Labels

func Labels(model *Model) []float64

func LabelsDense

func LabelsDense(model *Model) []*mat64.Dense

func NrClasses

func NrClasses(model *Model) int

func NrFutures

func NrFutures(model *Model) int

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 PredictValues

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

void call_predict_values(const struct model *model_, double* x, int n_rows, int n_cols, int n_classes) result = (double *)malloc(n_rows * n_classes * sizeof(double)); proba = (double *)malloc(n_classes * sizeof(double));

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 Model

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

Model contains a pointer to C's struct model (i.e., `*C.struct_model`). It is returned after training and used for predicting.

func LoadModel

func LoadModel(filename string) *Model

func Train

func Train(X, y *mat64.Dense, bias float64, solverType int, c_, p, eps float64, classWeights map[int]float64) *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.

Jump to

Keyboard shortcuts

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