* @param insts the training data.
* @throws Exception if a classifier can't be built
*/
public void buildClassifier(Instances insts) throws Exception {
Instances newInsts;
// can classifier handle the data?
getCapabilities().testWithFail(insts);
// remove instances with missing class
insts = new Instances(insts);
insts.deleteWithMissingClass();
if (m_Classifier == null) {
throw new Exception("No base classifier has been set!");
}
m_ZeroR = new ZeroR();
m_ZeroR.buildClassifier(insts);
m_TwoClassDataset = null;
int numClassifiers = insts.numClasses();
if (numClassifiers <= 2) {
m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, 1);
m_Classifiers[0].buildClassifier(insts);
m_ClassFilters = null;
} else if (m_Method == METHOD_1_AGAINST_1) {
// generate fastvector of pairs
FastVector pairs = new FastVector();
for (int i=0; i<insts.numClasses(); i++) {
for (int j=0; j<insts.numClasses(); j++) {
if (j<=i) continue;
int[] pair = new int[2];
pair[0] = i; pair[1] = j;
pairs.addElement(pair);
}
}
numClassifiers = pairs.size();
m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, numClassifiers);
m_ClassFilters = new Filter[numClassifiers];
m_SumOfWeights = new double[numClassifiers];
// generate the classifiers
for (int i=0; i<numClassifiers; i++) {
RemoveWithValues classFilter = new RemoveWithValues();
classFilter.setAttributeIndex("" + (insts.classIndex() + 1));
classFilter.setModifyHeader(true);
classFilter.setInvertSelection(true);
classFilter.setNominalIndicesArr((int[])pairs.elementAt(i));
Instances tempInstances = new Instances(insts, 0);
tempInstances.setClassIndex(-1);
classFilter.setInputFormat(tempInstances);
newInsts = Filter.useFilter(insts, classFilter);
if (newInsts.numInstances() > 0) {
newInsts.setClassIndex(insts.classIndex());
m_Classifiers[i].buildClassifier(newInsts);
m_ClassFilters[i] = classFilter;
m_SumOfWeights[i] = newInsts.sumOfWeights();
} else {
m_Classifiers[i] = null;
m_ClassFilters[i] = null;
}
}
// construct a two-class header version of the dataset
m_TwoClassDataset = new Instances(insts, 0);
int classIndex = m_TwoClassDataset.classIndex();
m_TwoClassDataset.setClassIndex(-1);
m_TwoClassDataset.deleteAttributeAt(classIndex);
FastVector classLabels = new FastVector();
classLabels.addElement("class0");