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.