/** * for testing the class * * @param argv the commandline parameters */ static public void main(String[] argv) { try { BayesNet b = new BayesNet(); b.setSearchAlgorithm(new ICSSearchAlgorithm()); Instances instances = new Instances(new FileReader( "C:\\eclipse\\workspace\\weka\\data\\contact-lenses.arff")); instances.setClassIndex(instances.numAttributes() - 1); b.buildClassifier(instances); System.out.println(b.toString()); } catch (Exception e) { e.printStackTrace(); } } // main
getCapabilities().testWithFail(instances); instances = normalizeDataSet(instances); initStructure(); buildStructure(); estimateCPTs();
/** * accuracyIncrease determines how much the accuracy estimate should be * increased due to the contribution of a single given instance. * * @param instance : instance for which to calculate the accuracy increase. * @return increase in accuracy due to given instance. * @throws Exception passed on by distributionForInstance and classifyInstance */ double accuracyIncrease(Instance instance) throws Exception { if (m_bUseProb) { double[] fProb = m_BayesNet.distributionForInstance(instance); return fProb[(int) instance.classValue()] * instance.weight(); } else { if (m_BayesNet.classifyInstance(instance) == instance.classValue()) { return instance.weight(); } } return 0; } // accuracyIncrease
/** * Main method for testing this class. * * @param argv the options */ public static void main(String[] argv) { runClassifier(new BayesNet(), argv); } // main
/** * Calc Node Score for given parent set * * @param nNode node for which the score is calculate * @return log score */ public double calcNodeScore(int nNode) { if (m_BayesNet.getUseADTree() && m_BayesNet.getADTree() != null) { return calcNodeScoreADTree(nNode); } else { return calcNodeScorePlain(nNode); } }
if (bayesNet.getParentSet(iAttribute).getNrOfParents() > 1) { throw new Exception( "Cannot handle networks with nodes with more than 1 parent (yet)."); BayesNet EmptyNet = new BayesNet(); K2 oSearchAlgorithm = new K2(); oSearchAlgorithm.setInitAsNaiveBayes(false); oSearchAlgorithm.setMaxNrOfParents(0); EmptyNet.setSearchAlgorithm(oSearchAlgorithm); EmptyNet.buildClassifier(instances); BayesNet NBNet = new BayesNet(); oSearchAlgorithm.setInitAsNaiveBayes(true); oSearchAlgorithm.setMaxNrOfParents(1); NBNet.setSearchAlgorithm(oSearchAlgorithm); NBNet.buildClassifier(instances); for (int iParent = 0; iParent < bayesNet.getParentSet(iAttribute) .getCardinalityOfParents(); iParent++) { int nTotal = 0; int nParentValues = bayesNet.getParentSet(iAttribute) .getCardinalityOfParents(); for (int iParent = 0; iParent < nParentValues; iParent++) { for (int iParent = 0; iParent < bayesNet.getParentSet(iAttribute) .getCardinalityOfParents(); iParent++) { bayesNet.m_Distributions[iAttribute][iParent] = new DiscreteEstimatorFullBayes(
bestBayesNet = new BayesNet(); bestBayesNet.m_Instances = instances; bestBayesNet.initStructure(); copyParentSets(bestBayesNet, bayesNet);
bestBayesNet = new BayesNet(); bestBayesNet.m_Instances = instances; bestBayesNet.initStructure(); copyParentSets(bestBayesNet, bayesNet); if (bayesNet.getDebug()) { printTabuList();
BayesNet bestBayesNet = new BayesNet(); bestBayesNet.m_Instances = instances; bestBayesNet.initStructure(); copyParentSets(bestBayesNet, bayesNet); bRunSucces = true; bayesNet.getParentSet(iHeadNode).deleteParent(iTailNode, instances); double fScore = calcScore(bayesNet); fDeltaScore = fScore - fCurrentScore; } else { bayesNet.getParentSet(iHeadNode).addParent(iTailNode, instances); bayesNet.getParentSet(iHeadNode).addParent(iTailNode, instances); fCurrentScore = fScore;
/** * LeaveOneOutCV returns the accuracy calculated using Leave One Out cross * validation. The dataset used is m_Instances associated with the Bayes * Network. * * @param bayesNet : Bayes Network containing structure to evaluate * @return accuracy (in interval 0..1) measured using leave one out cv. * @throws Exception passed on by updateClassifier */ public double leaveOneOutCV(BayesNet bayesNet) throws Exception { m_BayesNet = bayesNet; double fAccuracy = 0.0; double fWeight = 0.0; Instances instances = bayesNet.m_Instances; bayesNet.estimateCPTs(); for (int iInstance = 0; iInstance < instances.numInstances(); iInstance++) { Instance instance = instances.instance(iInstance); instance.setWeight(-instance.weight()); bayesNet.updateClassifier(instance); fAccuracy += accuracyIncrease(instance); fWeight += instance.weight(); instance.setWeight(-instance.weight()); bayesNet.updateClassifier(instance); } return fAccuracy / fWeight; } // LeaveOneOutCV
/** Creates a default BayesNet */ public Classifier getClassifier() { return new BayesNet(); }
ParentSet oParentSet = m_BayesNet.getParentSet(nNode); m_BayesNet.getADTree().getCounts(nCounts, nNodes, nOffsets, 0, 0, false);
text.append(getBIFHeader()); text.append("\n"); text.append("\n"); text.append("<BIF VERSION=\"0.3\">\n"); text.append("<NETWORK>\n"); text.append("<NAME>" + XMLNormalize(Utils.quote(m_Instances.relationName())) + "</NAME>\n"); for (int iAttribute = 0; iAttribute < m_Instances.numAttributes(); iAttribute++) { text.append("<VARIABLE TYPE=\"nature\">\n"); text.append("<NAME>" + XMLNormalize(Utils.quote(m_Instances.attribute(iAttribute).name())) + "</NAME>\n"); for (int iValue = 0; iValue < m_Instances.attribute(iAttribute) .numValues(); iValue++) { text.append("<OUTCOME>" + XMLNormalize(Utils.quote(m_Instances.attribute(iAttribute).value(iValue))) + "</OUTCOME>\n"); text.append("<DEFINITION>\n"); text.append("<FOR>" + XMLNormalize(Utils.quote(m_Instances.attribute(iAttribute).name())) + "</FOR>\n"); for (int iParent = 0; iParent < m_ParentSets[iAttribute].getNrOfParents(); iParent++) { text .append("<GIVEN>" + XMLNormalize(Utils.quote(m_Instances.attribute( m_ParentSets[iAttribute].getParent(iParent)).name())) + "</GIVEN>\n");
if (bayesNet.getParentSet(iAttribute).getNrOfParents() > 1) { throw new Exception( "Cannot handle networks with nodes with more than 1 parent (yet)."); BayesNet EmptyNet = new BayesNet(); K2 oSearchAlgorithm = new K2(); oSearchAlgorithm.setInitAsNaiveBayes(false); oSearchAlgorithm.setMaxNrOfParents(0); EmptyNet.setSearchAlgorithm(oSearchAlgorithm); EmptyNet.buildClassifier(instances); BayesNet NBNet = new BayesNet(); oSearchAlgorithm.setInitAsNaiveBayes(true); oSearchAlgorithm.setMaxNrOfParents(1); NBNet.setSearchAlgorithm(oSearchAlgorithm); NBNet.buildClassifier(instances); for (int iParent = 0; iParent < bayesNet.getParentSet(iAttribute) .getCardinalityOfParents(); iParent++) { int nTotal = 0; int nParentValues = bayesNet.getParentSet(iAttribute) .getCardinalityOfParents(); for (int iParent = 0; iParent < nParentValues; iParent++) { for (int iParent = 0; iParent < bayesNet.getParentSet(iAttribute) .getCardinalityOfParents(); iParent++) { bayesNet.m_Distributions[iAttribute][iParent] = new DiscreteEstimatorFullBayes(
bestBayesNet = new BayesNet(); bestBayesNet.m_Instances = instances; bestBayesNet.initStructure(); copyParentSets(bestBayesNet, bayesNet);
bestBayesNet = new BayesNet(); bestBayesNet.m_Instances = instances; bestBayesNet.initStructure(); copyParentSets(bestBayesNet, bayesNet); if (bayesNet.getDebug()) { printTabuList();
BayesNet bestBayesNet = new BayesNet(); bestBayesNet.m_Instances = instances; bestBayesNet.initStructure(); copyParentSets(bestBayesNet, bayesNet); bRunSucces = true; bayesNet.getParentSet(iHeadNode).deleteParent(iTailNode, instances); double fScore = calcScore(bayesNet); fDeltaScore = fScore - fCurrentScore; } else { bayesNet.getParentSet(iHeadNode).addParent(iTailNode, instances); bayesNet.getParentSet(iHeadNode).addParent(iTailNode, instances); fCurrentScore = fScore;
/** * Main method for testing this class. * * @param argv the options */ public static void main(String[] argv) { runClassifier(new BayesNet(), argv); } // main
/** * LeaveOneOutCV returns the accuracy calculated using Leave One Out cross * validation. The dataset used is m_Instances associated with the Bayes * Network. * * @param bayesNet : Bayes Network containing structure to evaluate * @return accuracy (in interval 0..1) measured using leave one out cv. * @throws Exception passed on by updateClassifier */ public double leaveOneOutCV(BayesNet bayesNet) throws Exception { m_BayesNet = bayesNet; double fAccuracy = 0.0; double fWeight = 0.0; Instances instances = bayesNet.m_Instances; bayesNet.estimateCPTs(); for (int iInstance = 0; iInstance < instances.numInstances(); iInstance++) { Instance instance = instances.instance(iInstance); instance.setWeight(-instance.weight()); bayesNet.updateClassifier(instance); fAccuracy += accuracyIncrease(instance); fWeight += instance.weight(); instance.setWeight(-instance.weight()); bayesNet.updateClassifier(instance); } return fAccuracy / fWeight; } // LeaveOneOutCV
/** Creates a default BayesNet */ public Classifier getClassifier() { return new BayesNet(); }