📄 lbr.java
字号:
/* * 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 * aint with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * LBR.java * The naive Bayesian classifier provides a simple and effective approach to * classifier learning, but its attribute independence assumption is often * violated in the real world. Lazy Bayesian Rules selectively relaxes the * independence assumption, achieving lower error rates over a range of * learning tasks. LBR defers processing to classification time, making it * a highly efficient and accurate classification algorithm when small * numbers of objects are to be classified. * * For more information, see<p> * Zijian Zheng & G. Webb, (2000). <i>Lazy Learning of Bayesian Rules.</i> Machine Learning, 41(1): 53-84. * * http://www.cm.deakin.edu.au/webb * * Copyright (C) 2001 Deakin University * School of Computing and Mathematics * Deakin University * Geelong, Vic, 3217, Australia * * Email: zhw@deakin.edu.au * */package weka.classifiers.lazy;import weka.classifiers.DistributionClassifier;import weka.classifiers.Classifier;import weka.classifiers.Evaluation;import java.io.*;import java.util.*;import weka.core.*;/** * Class for building and using a Lazy Bayesian classifier.<BR> * The naive Bayesian classifier provides a simple and effective approach to * classifier learning, but its attribute independence assumption is often violated * in the real world. A number of approaches have sought to alleviate this problem. * A Bayesian tree learning algorithm builds a decision tree, and generates a local * naive Bayesian classifier at each leaf. The tests leading to a leaf can alleviate * attribute inter璬ependencies for the local naive Bayesian classifier. However, * Bayesian tree learning still suffers from the small disjunct problem of tree learning. * While inferred Bayesian trees demonstrate low average prediction error rates, there is * reason to believe that error rates will be higher for those leaves with few training examples. <BR> * * For more information, see<p> * Zijian Zheng & G. Webb, (2000). <i>Lazy Learning of Bayesian Rules.</i> Machine Learning, 41(1): 53-84.<BR> * @author Zhihai Wang (Email: zhw@deakin.edu.au) : July 2001 implemented the algorithm * @author Jason Wells (Email: wells@deakin.edu.au) : November 2001 added instance referencing via indexes * */public class LBR extends DistributionClassifier { /** * Class for handling instances and the associated attributes. <p> * Enables a set of indexes to a given dataset to be created and used * with an algorithm. This reduces the memory overheads and time required * when manipulating and referencing Instances and their Attributes. */ public class Indexes implements Serializable { /** the array instance indexes **/ public boolean [] m_InstIndexes; /** the array attribute indexes **/ public boolean [] m_AttIndexes; /** the number of instances indexed **/ private int m_NumInstances; /** the number of attributes indexed **/ private int m_NumAtts; /** the array of instance indexes that are set to a either true or false **/ public int [] m_SequentialInstIndexes; /** an array of attribute indexes that are set to either true or false **/ public int [] m_SequentialAttIndexes; /** flag to check if sequential array must be rebuilt due to changes to the instance index*/ private boolean m_SequentialInstanceIndex_valid = false; /** flag to check if sequential array must be rebuilt due to changes to the attribute index */ private boolean m_SequentialAttIndex_valid = false; /** the number of instances "in use" or set to a the original value (true or false) **/ public int m_NumInstsSet; /** the number of attributes "in use" or set to a the original value (true or false) **/ public int m_NumAttsSet; /** the number of sequential instances "in use" or set to a the original value (true or false) **/ public int m_NumSeqInstsSet; /** the number of sequential attributes "in use" or set to a the original value (true or false) **/ public int m_NumSeqAttsSet; /** the Class Index for the data set **/ public int m_ClassIndex; /** * constructor * @param numInstances the number of instances in dataset * @param numAtts the number of attributes in dataset * @param value either true or false * @param classIndex. Set to -1 if you want class attribute switched on or the value of the instances * class index will be switched of and the class attibute will not be considered. */ public Indexes(int numInstances, int numAtts, boolean value, int classIndex) { /* to create an empty DATASET with all attributes indexed use FALSE * to create a index of all instances and attributes use TRUE */ // initialise counts m_NumInstsSet = m_NumInstances = numInstances; m_NumAttsSet = m_NumAtts = numAtts; m_InstIndexes = new boolean [(int)numInstances]; /* set all indexes to value */ int i = 0; while(i < numInstances) { m_InstIndexes[i] = value; i++; } m_AttIndexes = new boolean [(int)numAtts]; /* set all indexes to true */ i = 0; while(i < numAtts) { m_AttIndexes[i] = true; i++; } // if the value is false the dataset has no instances therefore no instances are set if(value == false) m_NumInstsSet = 0; // no sequential array has been created m_SequentialInstanceIndex_valid = false; m_SequentialAttIndex_valid = false; // switch class attr to false as the class is not used in the dataset. Set to -1 if you want the class attr included if(classIndex != -1) setAttIndex(classIndex, false); m_ClassIndex = classIndex; } /** * constructor * @param FromIndexes the object you want to copy */ public Indexes(Indexes FromIndexes) { // set counts to the FromIndexes counts m_NumInstances = FromIndexes.getNumInstances(); m_NumInstsSet = FromIndexes.m_NumInstsSet; m_NumAtts = FromIndexes.m_NumAtts; m_NumAttsSet = FromIndexes.m_NumAttsSet; m_InstIndexes = new boolean [m_NumInstances]; System.arraycopy(FromIndexes.m_InstIndexes, 0, m_InstIndexes, 0, m_NumInstances); m_AttIndexes = new boolean [(int)m_NumAtts]; System.arraycopy(FromIndexes.m_AttIndexes, 0, m_AttIndexes, 0, m_NumAtts); m_ClassIndex = FromIndexes.m_ClassIndex; m_SequentialInstanceIndex_valid = false; m_SequentialAttIndex_valid = false; } /** * * Changes the boolean value at the specified index in the InstIndexes array * * @param index the index of the instance * @param value the value to set at the specified index * */ public void setInstanceIndex(int index, boolean value) { if(index < 0 || index >= m_NumInstances) throw new IllegalArgumentException("Invalid Instance Index value"); // checks that the index isn't alreading set to value if(m_InstIndexes[(int)index] != value) { // set the value m_InstIndexes[(int)index] = value; // a change has been made, so sequential array is invalid m_SequentialInstanceIndex_valid = false; // change the number of values "in use" to appropriate value if(value == false) m_NumInstsSet--; else m_NumInstsSet++; } } /** * * Changes the boolean value at the specified index in the InstIndexes array * * @param index the index of the instance * @param value the value to set at the specified index * */ public void setAtts(int [] Attributes, boolean value) { for(int i = 0; i < m_NumAtts; i++) { m_AttIndexes[i] = !value; } for (int i = 0; i < Attributes.length; i++) { m_AttIndexes[Attributes[i]] = value; } m_NumAttsSet = Attributes.length; m_SequentialAttIndex_valid = false; } /** * * Changes the boolean value at the specified index in the InstIndexes array * * @param index the index of the instance * @param value the value to set at the specified index * */ public void setInsts(int [] Instances, boolean value) { resetInstanceIndex(!value); for (int i = 0; i < Instances.length; i++) { m_InstIndexes[Instances[i]] = value; } m_NumInstsSet = Instances.length; m_SequentialInstanceIndex_valid = false; } /** * * Changes the boolean value at the specified index in the AttIndexes array * * @param index the index of the instance * @param value the value to set at the specified index * */ public void setAttIndex(int index, boolean value) { if(index < 0 || index >= m_NumAtts) throw new IllegalArgumentException("Invalid Attribute Index value"); // checks that the index isn't alreading set to value if(m_AttIndexes[(int)index] != value) { // set the value m_AttIndexes[(int)index] = value; // a change has been made, so sparse array is invalid m_SequentialAttIndex_valid = false; // change the number of values "in use" to appropriate value if(value == false) m_NumAttsSet--; else m_NumAttsSet++; } } /** * * Returns the boolean value at the specified index in the Instance Index array * * @param index the index of the instance * */ public boolean getInstanceIndex(int index) { if(index < 0 || index >= m_NumInstances) throw new IllegalArgumentException("Invalid index value"); return m_InstIndexes[(int)index]; } /** * * Returns the boolean value at the specified index in the Sequential Instance Indexes array * * @param index the index of the instance * */ public int getSequentialInstanceIndex(int index) { if(index < 0 || index >= m_NumInstances) throw new IllegalArgumentException("Invalid index value"); return m_SequentialInstIndexes[(int)index]; } /** * * Resets the boolean value in the Instance Indexes array to a specified value * * @param value the value to set all indexes * */ public void resetInstanceIndex(boolean value) { m_NumInstsSet = m_NumInstances; for(int i = 0; i < m_NumInstances; i++) { m_InstIndexes[i] = value; } if(value == false) m_NumInstsSet = 0; m_SequentialInstanceIndex_valid = false; } /** * * Resets the boolean values in Attribute and Instance array to reflect an empty dataset withthe same attributes set as in the incoming Indexes Object * * @param FromIndexes the Indexes to be copied * */ public void resetDatasetBasedOn(Indexes FromIndexes) { resetInstanceIndex(false); resetAttIndexTo(FromIndexes); } /** * * Resets the boolean value in AttIndexes array * * @param value the value to set the attributes to * */ public void resetAttIndex(boolean value) { m_NumAttsSet = m_NumAtts; for(int i = 0; i < m_NumAtts; i++) { m_AttIndexes[i] = value; } if(m_ClassIndex != -1) setAttIndex(m_ClassIndex, false); if(value == false) m_NumAttsSet = 0; m_SequentialAttIndex_valid = false; } /** * * Resets the boolean value in AttIndexes array based on another set of Indexes * * @param FromIndexes the Indexes to be copied * */ public void resetAttIndexTo(Indexes FromIndexes) { System.arraycopy(FromIndexes.m_AttIndexes, 0, m_AttIndexes, 0, m_NumAtts); m_NumAttsSet = FromIndexes.getNumAttributesSet(); m_ClassIndex = FromIndexes.m_ClassIndex; m_SequentialAttIndex_valid = false; } /** * * Returns the boolean value at the specified index in the Attribute Indexes array
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -