weka.classifiers.bayes.HNB Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weka-stable Show documentation
Show all versions of weka-stable Show documentation
The Waikato Environment for Knowledge Analysis (WEKA), a machine
learning workbench. This is the stable version. Apart from bugfixes, this version
does not receive any other updates.
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* HNB.java
* Copyright (C) 2004 Liangxiao Jiang
*/
package weka.classifiers.bayes;
import weka.classifiers.Classifier;
import weka.core.Capabilities;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.RevisionUtils;
import weka.core.TechnicalInformation;
import weka.core.TechnicalInformationHandler;
import weka.core.Utils;
import weka.core.Capabilities.Capability;
import weka.core.TechnicalInformation.Field;
import weka.core.TechnicalInformation.Type;
/**
* Contructs Hidden Naive Bayes classification model with high classification accuracy and AUC.
*
* For more information refer to:
*
* H. Zhang, L. Jiang, J. Su: Hidden Naive Bayes. In: Twentieth National Conference on Artificial Intelligence, 919-924, 2005.
*
*
* BibTeX:
*
* @inproceedings{Zhang2005,
* author = {H. Zhang and L. Jiang and J. Su},
* booktitle = {Twentieth National Conference on Artificial Intelligence},
* pages = {919-924},
* publisher = {AAAI Press},
* title = {Hidden Naive Bayes},
* year = {2005}
* }
*
*
*
* Valid options are:
*
* -D
* If set, classifier is run in debug mode and
* may output additional info to the console
*
*
* @author H. Zhang ([email protected])
* @author Liangxiao Jiang ([email protected])
* @version $Revision: 5516 $
*/
public class HNB
extends Classifier
implements TechnicalInformationHandler {
/** for serialization */
static final long serialVersionUID = -4503874444306113214L;
/** The number of each class value occurs in the dataset */
private double [] m_ClassCounts;
/** The number of class and two attributes values occurs in the dataset */
private double [][][] m_ClassAttAttCounts;
/** The number of values for each attribute in the dataset */
private int [] m_NumAttValues;
/** The number of values for all attributes in the dataset */
private int m_TotalAttValues;
/** The number of classes in the dataset */
private int m_NumClasses;
/** The number of attributes including class in the dataset */
private int m_NumAttributes;
/** The number of instances in the dataset */
private int m_NumInstances;
/** The index of the class attribute in the dataset */
private int m_ClassIndex;
/** The starting index of each attribute in the dataset */
private int[] m_StartAttIndex;
/** The 2D array of conditional mutual information of each pair attributes */
private double[][] m_condiMutualInfo;
/**
* Returns a string describing this classifier.
*
* @return a description of the data generator suitable for
* displaying in the explorer/experimenter gui
*/
public String globalInfo() {
return
"Contructs Hidden Naive Bayes classification model with high "
+ "classification accuracy and AUC.\n\n"
+ "For more information refer to:\n\n"
+ getTechnicalInformation().toString();
}
/**
* Returns an instance of a TechnicalInformation object, containing
* detailed information about the technical background of this class,
* e.g., paper reference or book this class is based on.
*
* @return the technical information about this class
*/
public TechnicalInformation getTechnicalInformation() {
TechnicalInformation result;
result = new TechnicalInformation(Type.INPROCEEDINGS);
result.setValue(Field.AUTHOR, "H. Zhang and L. Jiang and J. Su");
result.setValue(Field.TITLE, "Hidden Naive Bayes");
result.setValue(Field.BOOKTITLE, "Twentieth National Conference on Artificial Intelligence");
result.setValue(Field.YEAR, "2005");
result.setValue(Field.PAGES, "919-924");
result.setValue(Field.PUBLISHER, "AAAI Press");
return result;
}
/**
* Returns default capabilities of the classifier.
*
* @return the capabilities of this classifier
*/
public Capabilities getCapabilities() {
Capabilities result = super.getCapabilities();
result.disableAll();
// attributes
result.enable(Capability.NOMINAL_ATTRIBUTES);
// class
result.enable(Capability.NOMINAL_CLASS);
result.enable(Capability.MISSING_CLASS_VALUES);
return result;
}
/**
* Generates the classifier.
*
* @param instances set of instances serving as training data
* @exception Exception if the classifier has not been generated successfully
*/
public void buildClassifier(Instances instances) throws Exception {
// can classifier handle the data?
getCapabilities().testWithFail(instances);
// remove instances with missing class
instances = new Instances(instances);
instances.deleteWithMissingClass();
// reset variable
m_NumClasses = instances.numClasses();
m_ClassIndex = instances.classIndex();
m_NumAttributes = instances.numAttributes();
m_NumInstances = instances.numInstances();
m_TotalAttValues = 0;
// allocate space for attribute reference arrays
m_StartAttIndex = new int[m_NumAttributes];
m_NumAttValues = new int[m_NumAttributes];
// set the starting index of each attribute and the number of values for
// each attribute and the total number of values for all attributes (not including class).
for(int i = 0; i < m_NumAttributes; i++) {
if(i != m_ClassIndex) {
m_StartAttIndex[i] = m_TotalAttValues;
m_NumAttValues[i] = instances.attribute(i).numValues();
m_TotalAttValues += m_NumAttValues[i];
}
else {
m_StartAttIndex[i] = -1;
m_NumAttValues[i] = m_NumClasses;
}
}
// allocate space for counts and frequencies
m_ClassCounts = new double[m_NumClasses];
m_ClassAttAttCounts = new double[m_NumClasses][m_TotalAttValues][m_TotalAttValues];
// Calculate the counts
for(int k = 0; k < m_NumInstances; k++) {
int classVal=(int)instances.instance(k).classValue();
m_ClassCounts[classVal] ++;
int[] attIndex = new int[m_NumAttributes];
for(int i = 0; i < m_NumAttributes; i++) {
if(i == m_ClassIndex)
attIndex[i] = -1;
else
attIndex[i] = m_StartAttIndex[i] + (int)instances.instance(k).value(i);
}
for(int Att1 = 0; Att1 < m_NumAttributes; Att1++) {
if(attIndex[Att1] == -1) continue;
for(int Att2 = 0; Att2 < m_NumAttributes; Att2++) {
if((attIndex[Att2] != -1)) {
m_ClassAttAttCounts[classVal][attIndex[Att1]][attIndex[Att2]] ++;
}
}
}
}
//compute conditional mutual information of each pair attributes (not including class)
m_condiMutualInfo=new double[m_NumAttributes][m_NumAttributes];
for(int son=0;son0){
prob=prob/condiMutualInfoSum;
probs[classVal] *= prob;
}
else{
prob=(m_ClassAttAttCounts[classVal][sIndex][sIndex]+1.0/m_NumAttValues[son])/(m_ClassCounts[classVal]+1.0);
probs[classVal]*= prob;
}
attIndex[son] = sIndex;
}
}
Utils.normalize(probs);
return probs;
}
/**
* returns a string representation of the classifier
*
* @return a representation of the classifier
*/
public String toString() {
return "HNB (Hidden Naive Bayes)";
}
/**
* Returns the revision string.
*
* @return the revision
*/
public String getRevision() {
return RevisionUtils.extract("$Revision: 5516 $");
}
/**
* Main method for testing this class.
*
* @param args the options
*/
public static void main(String[] args) {
runClassifier(new HNB(), args);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy