Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
examples/undocumented/lua_modular/classifier_libsvm_minimal_modular.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'shogun' | ||
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=RealFeatures(traindata_real) | ||
feats_test=RealFeatures(testdata_real) | ||
kernel=GaussianKernel(feats_train, feats_train, width) | ||
|
||
labels=Labels(trainlab) | ||
svm=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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters