📄 lbr.java
字号:
} /** * * Returns the boolean value at the specified index in the Attribute Indexes array * * @param index the index of the Instance * @return the boolean value */ public boolean getAttIndex(int index) { if(index < 0 || index >= m_NumAtts) throw new IllegalArgumentException("Invalid index value"); return m_AttIndexes[(int)index]; } /** * * Returns the boolean value at the specified index in the Sequential Attribute Indexes array * * @param index the index of the Attribute * @return the requested value */ public int getSequentialAttIndex(int index) { if(index < 0 || index >= m_NumAtts) throw new IllegalArgumentException("Invalid index value"); return m_SequentialAttIndexes[(int)index]; } /** * * Returns the number of instances "in use" * * @return the number of instances "in use" */ public int getNumInstancesSet() { return m_NumInstsSet; } /** * * Returns the number of instances in the dataset * * @return the number of instances in the dataset */ public int getNumInstances() { return m_NumInstances; } /** * * Returns the number of instances in the Sequential array * * @return the number of instances in the sequential array */ public int getSequentialNumInstances() { // will always be the number set as the sequential array is for referencing only return m_NumSeqInstsSet; } /** * * Returns the number of attributes in the dataset * * @return the number of attributes */ public int getNumAttributes() { return m_NumAtts; } /** * * Returns the number of attributes "in use" * * @return the number of attributes "in use" */ public int getNumAttributesSet() { return m_NumAttsSet; } /** * * Returns the number of attributes in the Sequential array * * @return the number of attributes in the sequentual array */ public int getSequentialNumAttributes() { // will always be the number set as the sequential array is for referencing only return m_NumSeqAttsSet; } /** * * Returns whether or not the Sequential Instance Index requires rebuilding due to a change * * @return true if the sequential instance index needs rebuilding */ public boolean isSequentialInstanceIndexValid() { return m_SequentialInstanceIndex_valid; } /** * * Returns whether or not the Sequential Attribute Index requires rebuilding due to a change * * @return true if the sequential attribute index needs rebuilding */ public boolean isSequentialAttIndexValid() { return m_SequentialAttIndex_valid; } /** * * Sets both the Instance and Attribute indexes to a specified value * * @param value the value for the Instance and Attribute indices */ public void setSequentialDataset(boolean value) { setSequentialInstanceIndex(value); setSequentialAttIndex(value); } /** * * A Sequential Instance index is all those Instances that are set to the specified value placed in a sequential array. * Each value in the sequential array contains the Instance index within the Indexes. * * @param value the sequential instance index */ public void setSequentialInstanceIndex(boolean value) { if(m_SequentialInstanceIndex_valid == true) return; /* needs to be recalculated */ int size; size = m_NumInstsSet; m_SequentialInstIndexes = new int [(int)size]; int j = 0; for(int i = 0; i < m_NumInstances; i++) { if(m_InstIndexes[i] == value) { m_SequentialInstIndexes[j] = i; j++; } } m_SequentialInstanceIndex_valid = true; m_NumSeqInstsSet = j; } /** * * A Sequential Attribute index is all those Attributes that are set to the specified value placed in a sequential array. * Each value in the sequential array contains the Attribute index within the Indexes * * @param value the sequential attribute index */ public void setSequentialAttIndex(boolean value) { if(m_SequentialAttIndex_valid == true) return; /* needs to be recalculated */ int size; size = m_NumAttsSet; m_SequentialAttIndexes = new int [(int)size]; int j = 0; for(int i = 0; i < m_NumAtts; i++) { if(m_AttIndexes[i] == value) { m_SequentialAttIndexes[j] = i; j++; } } m_SequentialAttIndex_valid = true; m_NumSeqAttsSet = j; } } /* end of Indexes inner-class */ /** All the counts for nominal attributes. */ protected int [][][] m_Counts; /** All the counts for nominal attributes. */ protected int [][][] m_tCounts; /** The prior probabilities of the classes. */ protected int [] m_Priors; /** The prior probabilities of the classes. */ protected int [] m_tPriors; /** number of attributes for the dataset ***/ protected int m_numAtts; /** number of classes for dataset ***/ protected int m_numClasses; /** number of instances in dataset ***/ protected int m_numInsts; /** The set of instances used for current training. */ protected Instances m_Instances = null; /** leave-one-out errors on the training dataset. */ protected int m_Errors; /** leave-one-out error flags on the training dataaet. */ protected boolean [] m_ErrorFlags; /** best attribute's index list. maybe as output result */ protected ArrayList leftHand = new ArrayList(); /** significantly lower */ protected static final double SIGNLOWER = 0.05; /** following is defined by wangzh, * the number of instances to be classified incorrectly * on the subset. */ protected boolean [] m_subOldErrorFlags; /** the number of instances to be classified incorrectly * besides the subset. */ protected int m_RemainderErrors = 0; /** the number of instance to be processed */ protected int m_Number = 0; /** the Number of Instances to be used in building a classifiers */ protected int m_NumberOfInstances = 0; /** for printing in n-fold cross validation */ protected boolean m_NCV = false; /** index of instances and attributes for the given dataset */ protected Indexes m_subInstances; /** index of instances and attributes for the given dataset */ protected Indexes tempSubInstances; /** probability values array */ protected double [] posteriorsArray; protected int bestCnt; protected int tempCnt; protected int forCnt; protected int whileCnt; /** * @return a description of the classifier suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "Lazy Bayesian Rules Classifier. 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.\n\n" + "For more information, see:\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.ARTICLE); result.setValue(Field.AUTHOR, "Zijian Zheng and G. Webb"); result.setValue(Field.YEAR, "2000"); result.setValue(Field.TITLE, "Lazy Learning of Bayesian Rules"); result.setValue(Field.JOURNAL, "Machine Learning"); result.setValue(Field.VOLUME, "4"); result.setValue(Field.NUMBER, "1"); result.setValue(Field.PAGES, "53-84"); return result; } /** * Returns default capabilities of the classifier. * * @return the capabilities of this classifier */ public Capabilities getCapabilities() { Capabilities result = super.getCapabilities(); // attributes result.enable(Capability.NOMINAL_ATTRIBUTES); result.enable(Capability.MISSING_VALUES); // class result.enable(Capability.NOMINAL_CLASS); result.enable(Capability.MISSING_CLASS_VALUES); // instances result.setMinimumNumberInstances(0); return result; } /** * For lazy learning, building classifier is only to prepare their inputs * until classification time. * * @param instances set of instances serving as training data * @throws Exception if the preparation has not been generated. */ public void buildClassifier(Instances instances) throws Exception { int attIndex, i, j; bestCnt = 0; tempCnt = 0; forCnt = 0; whileCnt = 0; // can classifier handle the data? getCapabilities().testWithFail(instances); // remove instances with missing class instances = new Instances(instances); instances.deleteWithMissingClass(); m_numAtts = instances.numAttributes(); m_numClasses = instances.numClasses(); m_numInsts = instances.numInstances(); // Reserve space m_Counts = new int[m_numClasses][m_numAtts][0]; m_Priors = new int[m_numClasses]; m_tCounts = new int[m_numClasses][m_numAtts][0]; m_tPriors = new int[m_numClasses]; m_subOldErrorFlags = new boolean[m_numInsts+1]; m_Instances = instances; m_subInstances = new Indexes(m_numInsts, m_numAtts, true, m_Instances.classIndex()); tempSubInstances = new Indexes(m_numInsts, m_numAtts, true, m_Instances.classIndex()); posteriorsArray = new double[m_numClasses]; // prepare arrays for (attIndex = 0; attIndex < m_numAtts; attIndex++) { Attribute attribute = (Attribute) instances.attribute(attIndex); for (j = 0; j < m_numClasses; j++) { m_Counts[j][attIndex] = new int[attribute.numValues()]; m_tCounts[j][attIndex] = new int[attribute.numValues()]; } } // Compute counts and priors for(i = 0; i < m_numInsts; i++) { Instance instance = (Instance) instances.instance(i); int classValue = (int)instance.classValue(); // pointer for more efficient access to counts matrix in loop int [][] countsPointer = m_tCounts[classValue]; for(attIndex = 0; attIndex < m_numAtts; attIndex++) { countsPointer[attIndex][(int)instance.value(attIndex)]++; } m_tPriors[classValue]++; } // Step 2: Leave-one-out on the training data set. // get m_Errors and its flags array using leave-one-out. m_ErrorFlags = new boolean[m_numInsts]; m_Errors = leaveOneOut(m_subInstances, m_tCounts, m_tPriors, m_ErrorFlags); if (m_Number == 0) { m_NumberOfInstances = m_Instances.numInstances(); } else { System.out.println(" "); System.out.println("N-Fold Cross Validation: "); m_NCV = true; } } /** * Calculates the class membership probabilities * for the given test instance. * This is the most important method for Lazy Bayesian Rule algorithm. * * @param testInstance the instance to be classified * @return predicted class probability distribution * @throws Exception if distribution can't be computed */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -