trainset = new InstanceSet(factory);
for(int i=0;i<data.length;i++){
ISparseVector sv = new HashSparseVector(data[i],true);
int l = lf.lookupIndex(target[i]);
Instance inst = new Instance(sv,l);
trainset.add(inst);
}
lf.setStopIncrement(true);
//构建测试集
System.out.println("Train Number: " + trainset.size());
System.out.println("Class Number: " + lf.size());
float c = 1.0f;
int round = 10;
BaseGenerator featureGen = new BaseGenerator();
ZeroOneLoss loss = new ZeroOneLoss();
Inferencer msolver = new MultiLinearMax(featureGen, lf, null,2);
PATrainer trainer = new PATrainer(msolver, featureGen, loss, round,c, null);
Linear pclassifier = trainer.train(trainset, null);
String modelFile = "./tmp/m.gz";
pclassifier.saveTo(modelFile);
pclassifier = null;
System.out.println("分类器测试");
pclassifier = Linear.loadFrom(modelFile);
float[] tdata = new float[]{1,0,1};
ISparseVector sv = new HashSparseVector(tdata,true);
Instance inst = new Instance(sv);
String lab = pclassifier.getStringLabel(inst);
System.out.println("分类结果:\t"+lab);
long end = System.currentTimeMillis();