6
$\begingroup$

Under the hood Mathematica uses libsvm, so I don't think this question is beyond the pale.

I need to export an svm I trained with Classify to the standard SVM Format (which is readable by libsvm).

The SVM format obeys the following Backus-Naur Form (see http://www.cs.cornell.edu/People/tj/svm_light/), it looks like this:

<line> ::= <target> " " (<feature> ":" <value>)+
<target> ::= <positive-integer>
<feature> ::= <positive-integer>
<value> ::= <float>

How can I transform the internal representation of Mathematica's Classifiers into this representation?

Notes:

  • MATLAB provides libsvmread to import svms.
  • In python sklearn.datasets.load_svmlight_file imports svms.
  • Libsvm uses the so called "sparse" format where zero values do not need to be stored.
$\endgroup$
1
$\begingroup$

The problem is not able, but efficiency in training sparse data.

There are several purposes to import a libsvm dataset: one is training or predicting one is feature visualizing, doing something statistics and one is check debug data Of cause, there are many other purposes.

It's easy to write some functions and training them like IRIS, agaricus, lightweight datasets.

importSVMSimple[file_,n_:10]:=Block[{lines,data,label,data0},
lines=ReadList[file,"Record",n];
data=StringSplit[#,{" ",":"}]&/@lines;
label=First/@data;
data0=GroupBy[Partition[Rest@#,2],First->Last,#[[1]]&]&/@data;
Thread[data0->label]
]

iris=ExampleData[{"MachineLearning","FisherIris"}, "Data"];
iris[[1;;10]]

{{5.1,3.5,1.4,0.2}->setosa,{4.9,3.,1.4,0.2}->setosa,{4.7,3.2,1.3,0.2}->setosa,{4.6,3.1,1.5,0.2}->setosa,{5.,3.6,1.4,0.2}->setosa,{5.4,3.9,1.7,0.4}->setosa,{4.6,3.4,1.4,0.3}->setosa,{5.,3.4,1.5,0.2}->setosa,{4.4,2.9,1.4,0.2}->setosa,{4.9,3.1,1.5,0.1}->setosa}

data2LibSVM[dataSets_]:=Block[{labels,labelID,data},
labels=dataSets[[All,2]]//Union;
labelID=Association[Thread[labels->Range[Length@labels]]];
    data=MapAt[labelID,Map[MapIndexed[StringRiffle[{First@#2,#1},":"]&],dataSets,{2}],{All,2}];
StringRiffle[Flatten@Reverse@#," "]&/@List@@@data
]

Export["iris.libsvm.csv",data2LibSVM@iris]

This structure is trainable.

dataSet=importSVMSimple["iris.libsvm.csv",10000];
dataSet//Length
150
training=RandomSample[dataSet,100];
testing=Complement[dataSet,training];
model=Classify[training];
ClassifierMeasurements[model,testing,"Accuracy"]
0.95
model@testing[[1,1]]
1

We can write many related functions.

$\endgroup$
  • $\begingroup$ I think you have it backwards, I had asked for a way of exporting a ClassifierFunction, not data. $\endgroup$ – M.R. Apr 24 '18 at 19:15
  • $\begingroup$ @M.R. OK, The problem is in your question, you say 'load_svmlight_file', I use it to load svm-format file like training data. So I add my answer related. $\endgroup$ – HyperGroups Jul 24 '18 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.