import weka.classifiers.bayes.NaiveBayes; //导入方法依赖的package包/类
/**
* Generates the classifier.
*
* @param data set of instances serving as training data
* @throws Exception if the classifier has not been generated successfully
*/
public void buildClassifier(Instances data) throws Exception {
m_saveMemory = false;
if (data.classAttribute().isNumeric()) {
throw new Exception("Can only handle nominal class!");
}
if (m_backwardWithDelete == null) {
setUpSearch();
m_search = m_backwardWithDelete;
}
/* if (m_search != m_backwardWithDelete) {
m_search = m_backwardWithDelete;
} */
super.buildClassifier(data);
// new NB stuff
// delete the features used by the decision table (not the class!!)
for (int i = 0; i < m_theInstances.numAttributes(); i++) {
m_theInstances.attribute(i).setWeight(1.0); // reset all weights
}
// m_nbFeatures = new int [m_decisionFeatures.length - 1];
int count = 0;
for (int i = 0; i < m_decisionFeatures.length; i++) {
if (m_decisionFeatures[i] != m_theInstances.classIndex()) {
count++;
//m_nbFeatures[count++] = m_decisionFeatures[i];
m_theInstances.attribute(m_decisionFeatures[i]).setWeight(0.0); // No influence for NB
}
}
double numDeleted = 0;
// remove any attributes that have been deleted completely from the DTNB
BitSet deleted = ((EvalWithDelete)m_evaluator).getDeletedList();
for (int i = 0; i < m_theInstances.numAttributes(); i++) {
if (deleted.get(i)) {
m_theInstances.attribute(i).setWeight(0.0);
// count--;
numDeleted++;
// System.err.println("Attribute "+i+" was eliminated completely");
}
}
m_percentUsedByDT = (double)count / (m_theInstances.numAttributes() - 1);
m_percentDeleted = numDeleted / (m_theInstances.numAttributes() -1);
m_NB = new NaiveBayes();
m_NB.buildClassifier(m_theInstances);
m_dtInstances = new Instances(m_dtInstances, 0);
m_theInstances = new Instances(m_theInstances, 0);
}