项目作者: JuliaML

项目描述 :
LIBSVM bindings for Julia
高级语言: Julia
项目地址: git://github.com/JuliaML/LIBSVM.jl.git
创建时间: 2013-04-15T01:50:21Z
项目社区:https://github.com/JuliaML/LIBSVM.jl

开源协议:Other

下载


LIBSVM.jl

Build Status
codecov

This is a Julia interface for
LIBSVM and for the linear
SVM model provided by
LIBLINEAR.

Features:

  • Supports all LIBSVM models: classification C-SVC, nu-SVC, regression: epsilon-SVR, nu-SVR
    and distribution estimation: one-class SVM
  • Model objects are represented by Julia type SVM which gives you easy
    access to model features and can be saved e.g. as JLD file
  • Supports ScikitLearn.jl API

Usage

LIBSVM API

This provides a lower level API similar to LIBSVM C-interface. See ?svmtrain
for options.

  1. using LIBSVM
  2. using RDatasets
  3. using Printf
  4. using Statistics
  5. # Load Fisher's classic iris data
  6. iris = dataset("datasets", "iris")
  7. # First four dimension of input data is features
  8. X = Matrix(iris[:, 1:4])'
  9. # LIBSVM handles multi-class data automatically using a one-against-one strategy
  10. y = iris.Species
  11. # Split the dataset into training set and testing set
  12. Xtrain = X[:, 1:2:end]
  13. Xtest = X[:, 2:2:end]
  14. ytrain = y[1:2:end]
  15. ytest = y[2:2:end]
  16. # Train SVM on half of the data using default parameters. See documentation
  17. # of svmtrain for options
  18. model = svmtrain(Xtrain, ytrain)
  19. # Test model on the other half of the data.
  20. ŷ, decision_values = svmpredict(model, Xtest);
  21. # Compute accuracy
  22. @printf "Accuracy: %.2f%%\n" mean(ŷ .== ytest) * 100

Precomputed kernel

It is possible to use different kernels than those that are provided. In such a
case, it is required to provide a matrix filled with precomputed kernel values.

For training, a symmetric matrix is expected:

  1. K = [k(x_1, x_1) k(x_1, x_2) ... k(x_1, x_l);
  2. k(x_2, x_1)
  3. ... ...
  4. k(x_l, x_1) ... k(x_l, x_l)]

where x_i is i-th training instance and l is the number of training
instances.

To predict n instances, a matrix of shape (l, n) is expected:

  1. KK = [k(x_1, t_1) k(x_1, t_2) ... k(x_1, t_n);
  2. k(x_2, t_1)
  3. ... ...
  4. k(x_l, t_1) ... k(x_l, t_n)]

where t_i is i-th instance to be predicted.

Example

  1. # Training data
  2. X = [-2 -1 -1 1 1 2;
  3. -1 -1 -2 1 2 1]
  4. y = [1, 1, 1, 2, 2, 2]
  5. # Testing data
  6. T = [-1 2 3;
  7. -1 2 2]
  8. # Precomputed matrix for training (corresponds to linear kernel)
  9. K = X' * X
  10. model = svmtrain(K, y, kernel=Kernel.Precomputed)
  11. # Precomputed matrix for prediction
  12. KK = X' * T
  13. ỹ, _ = svmpredict(model, KK)

ScikitLearn API

You can alternatively use ScikitLearn.jl API with same options as svmtrain:

  1. using LIBSVM
  2. using RDatasets
  3. # Classification C-SVM
  4. iris = dataset("datasets", "iris")
  5. X = Matrix(iris[:, 1:4])
  6. y = iris.Species
  7. Xtrain = X[1:2:end, :]
  8. Xtest = X[2:2:end, :]
  9. ytrain = y[1:2:end]
  10. ytest = y[2:2:end]
  11. model = fit!(SVC(), Xtrain, ytrain)
  12. ŷ = predict(model, Xtest)
  1. # Epsilon-Regression
  2. whiteside = RDatasets.dataset("MASS", "whiteside")
  3. X = Matrix(whiteside[:, 3:3]) # the `Gas` column
  4. y = whiteside.Temp
  5. model = fit!(EpsilonSVR(cost = 10., gamma = 1.), X, y)
  6. ŷ = predict(model, X)

MLJ API

The MLJ interface to LIBSVM.jl consists of the following models:

  • classification: LinearSVC, SVC, NuSVC
  • regression: EpsilonSVR, NuSVR
  • outlier detection: OneClassSVM

Each model has a detailed document string, which includes examples of
usage. Document strings can be accessed from MLJ without loading
LIBSVM.jl (or its MLJ interface) as shown in the following example:

  1. using MLJ # or MLJModels
  2. doc("NuSVC", pkg="LIBSVM")

This assumes the version of MLJModels loaded is 0.15.5 or higher.

Credits

The LIBSVM.jl library is currently developed and maintained by Matti
Pastell. It was originally developed by Simon Kornblith.

LIBSVM by Chih-Chung Chang and Chih-Jen Lin