- In this example a two-class support vector machine classifier is trained on a
- 2-dimensional randomly generated data set and the trained classifier is used to
- predict labels of test examples. As training algorithm the LIBSVM solver is used
- with SVM regularization parameter C=1 and a Gaussian kernel of width 2.1.
-
- For more details on LIBSVM solver see http://www.csie.ntu.edu.tw/~cjlin/libsvm/
require 'modshogun'
require 'load'
function concatenate(...)
local result = ...
for _,t in ipairs{select(2, ...)} do
for row,rowdata in ipairs(t) do
for col,coldata in ipairs(rowdata) do
table.insert(result[row], coldata)
end
end
end
return result
end
function rand_matrix(rows, cols, dist)
local matrix = {}
for i = 1, rows do
matrix[i] = {}
for j = 1, cols do
matrix[i][j] = math.random() + dist
end
end
return matrix
end
function ones(num)
r={}
for i=1,num do
r[i]=1
end
return r
end
num=1000
dist=1
width=2.1
C=1
traindata_real=concatenate(rand_matrix(2,num, -dist),rand_matrix(2,num,dist))
testdata_real=concatenate(rand_matrix(2,num,-dist), rand_matrix(2,num, dist))
trainlab={}
for i = 1, num do
trainlab[i] = -1
trainlab[i + num] = 1
end
testlab={}
for i = 1, num do
testlab[i] = -1
testlab[i + num] = 1
end
feats_train=modshogun.RealFeatures(traindata_real)
feats_test=modshogun.RealFeatures(testdata_real)
kernel=modshogun.GaussianKernel(feats_train, feats_train, width)
labels=modshogun.Labels(trainlab)
svm=modshogun.LibSVM(C, kernel, labels)
svm:train()
kernel:init(feats_train, feats_test)
out=svm:apply():get_labels()
err_num = 0
for i = 1, num do
if out[i] > 0 then
err_num = err_num+1
end
if out[i+num] < 0 then
err_num = err_num+1
end
end
testerr=err_num/(2*num)
print(testerr)

Distance

../examples/documented/lua_modular/distance_braycurtis_modular.lua

- An approach as applied below, which shows the processing of input data
- from a file becomes a crucial factor for writing your own sample applications.
- This approach is just one example of what can be done using the distance
- functions provided by shogun.
-
- First, you need to determine what type your data will be, because this
- will determine the distance function you can use.
-
- This example loads two stored matrices of real values from different
- files and initializes the matrices to 'RealFeatures'.
- Each column of the matrices corresponds to one data point.
-
- The distance initialized by two data sets (the same data set as shown in the
- first call) controls the processing of the given data points, where a pairwise
- distance matrix is computed by 'get_distance_matrix'.
-
- The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
-
- The method call 'init'* binds the given data sets, where a pairwise distance
- matrix between these two data sets is computed by 'get_distance_matrix'.
-
- The resulting distance matrix can be reaccessed by 'get_distance_matrix'.
-
- *Note that the previous computed distance matrix can no longer be
- reaccessed by 'get_distance_matrix'.
-
- For more details see doc/classshogun_1_1CBrayCurtisDistance.html.
-
- Obviously, using the Bray Curtis distance is not limited to this showcase
- example.
require 'modshogun'
require 'load'
traindat = load_numbers('../data/fm_train_real.dat')
testdat = load_numbers('../data/fm_test_real.dat')
parameter_list = {{traindat,testdat},{traindat,testdat}}
function distance_braycurtis_modular (fm_train_real,fm_test_real)
feats_train=modshogun.RealFeatures(fm_train_real)
feats_test=modshogun.RealFeatures(fm_test_real)
distance=modshogun.BrayCurtisDistance(feats_train, feats_train)
dm_train=distance:get_distance_matrix()
distance:init(feats_train, feats_test)
dm_test=distance:get_distance_matrix()
return distance,dm_train,dm_test
end
if debug.getinfo(3) == nill then
print 'BrayCurtisDistance'
distance_braycurtis_modular(unpack(parameter_list[1]))
end