java - Liblinear usage format -


i using .net implementation of liblinear in c# code following nuget package: https://www.nuget.org/packages/liblinear/

but in readme file of liblinear, format x is:

struct problem describes problem:

    struct problem     {         int l, n;         int *y;         struct feature_node **x;         double bias;     };  `l` number of training data. if bias >= 0, assume 1 additional feature added end of each data instance. `n` number of feature (including bias feature if bias >= 0). `y` array containing target values. (integers in classification, real numbers in regression) , `x` array of pointers, each of points sparse representation (array of feature_node) of 1 training vector.  example, if have following training data:      label       attr1   attr2   attr3   attr4   attr5     -----       -----   -----   -----   -----   -----     1           0       0.1     0.2     0       0     2           0       0.1     0.3    -1.2     0     1           0.4     0       0       0       0     2           0       0.1     0       1.4     0.5     3          -0.1    -0.2     0.1     1.1     0.1  , bias = 1, components of problem are:      l = 5     n = 6      y -> 1 2 1 2 3      x -> [ ] -> (2,0.1) (3,0.2) (6,1) (-1,?)          [ ] -> (2,0.1) (3,0.3) (4,-1.2) (6,1) (-1,?)          [ ] -> (1,0.4) (6,1) (-1,?)          [ ] -> (2,0.1) (4,1.4) (5,0.5) (6,1) (-1,?)          [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (6,1) (-1,?) 

but, in example showing java implementation: https://gist.github.com/hodzanassredin/6682771

problem.x <- [|                         [|new featurenode(1,0.); new featurenode(2,1.)|]                         [|new featurenode(1,2.); new featurenode(2,0.)|]                     |]// feature nodes problem.y <- [|1.;2.|] // target values 

which means data set is:

1 0 1 2 2 0 

so, not storing nodes per sparse format of liblinear. does, know of correct format x liblinear implementation?

though doesn't address library mentioned, can offer alternative. accord.net framework has incorporated of liblinear's algorithms in machine learning namespaces. available through nuget.

in library, direct syntax create linear support vector machine in-memory data is

// create simple binary , // classification problem:  double[][] problem = {     //                b    + b     new double[] { 0,   0,     0    },     new double[] { 0,   1,     0    },     new double[] { 1,   0,     0    },     new double[] { 1,   1,     1    }, };  // 2 first columns problem // inputs , last column output  // input columns double[][] inputs = problem.getcolumns(0, 1);  // output column int[] outputs = problem.getcolumn(2).toint32();  // however, svms expect output value // either -1 or +1. such, have convert // vector contains { -1, -1, -1, +1 }: // outputs = outputs.apply(x => x == 0 ? -1 : 1); 

after problem created, 1 can learn linear svm using

// create new linear-svm 2 inputs (a , b) supportvectormachine svm = new supportvectormachine(inputs: 2);  // create l2-regularized l2-loss support vector classification var teacher = new lineardualcoordinatedescent(svm, inputs, outputs) {     loss = loss.l2,     complexity = 1000,     tolerance = 1e-5 };  // learn machine double error = teacher.run(computeerror: true);  // compute machine's answers learned inputs int[] answers = inputs.apply(x => math.sign(svm.compute(x))); 

this assumes, however, data in-memory. if wish load data disk, from file in libsvm sparse format, can use framework's sparsereader class. example on how use can found below:

// suppose going read sparse sample file containing //  samples have actual dimension of 4. since samples //  in sparse format, each entry in file //  have smaller number of elements. //  int samplesize = 4;  // create new sparse sample reader read given file, //  passing correct dense sample size in constructor //  sparsereader reader = new sparsereader(file, encoding.default, samplesize);  // declare vector obtain label //  of each of samples in file //  int[] labels = null;  // declare vector obtain description (or comments) //  each of samples in file, if present. //  string[] descriptions = null;  // read sparse samples , store them in dense vector array double[][] samples = reader.readtoend(out labels, out descriptions); 

afterwards, 1 can use samples , labels vectors inputs , outputs of problem, respectively.

i hope helps.

disclaimer: author of library. answering question in sincere hope can useful op, since not long ago faced same problems. if moderator thinks looks spam, feel free delete. however, posting because think might others. came across question mistake while searching existing c# implementations of libsvm, not liblinear.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -