Package weka.classifiers.bayes

Examples of weka.classifiers.bayes.BayesNet


   *
   * @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
View Full Code Here


    for (int iAttribute = 0; iAttribute < instances.numAttributes(); iAttribute++) {
      fCurrentScore += calcNodeScore(iAttribute);
    }

    // keeps track of best structure found so far
    BayesNet bestBayesNet;

    // initialize bestBayesNet
    fBestScore = fCurrentScore;
    bestBayesNet = new BayesNet();
    bestBayesNet.m_Instances = instances;
    bestBayesNet.initStructure();
    copyParentSets(bestBayesNet, bayesNet);
   
               
        // go do the search       
        for (int iRun = 0; iRun < m_nRuns; iRun++) {
View Full Code Here

    for (int iAttribute = 0; iAttribute < instances.numAttributes(); iAttribute++) {
      fCurrentScore += calcNodeScore(iAttribute);
    }

    // keeps track of best structure found so far
    BayesNet bestBayesNet;

    // initialize bestBayesNet
    fBestScore = fCurrentScore;
    bestBayesNet = new BayesNet();
    bestBayesNet.m_Instances = instances;
    bestBayesNet.initStructure();
    copyParentSets(bestBayesNet, bayesNet);
   
               
        // go do the search       
        for (int iRun = 0; iRun < m_nRuns; iRun++) {
View Full Code Here

            }
        }
        // ok, now all data is binary, except the class attribute
        // now learn the empty and tree network

        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);

        // estimate CPTs
        for (int iAttribute = 0; iAttribute < instances.numAttributes(); iAttribute++) {
            if (iAttribute != instances.classIndex()) {
                  double w1 = 0.0, w2 = 0.0;
View Full Code Here

    }

    private Classifier getClassifier() throws Exception {
        if (classifier == null) {
            // create classifier used to build models
            classifier = new BayesNet();
            K2 xK2 = new K2();
            xK2.setInitAsNaiveBayes(true);
            xK2.setMaxNrOfParents(1);
            ((BayesNet) classifier).setSearchAlgorithm(xK2);
        }
View Full Code Here

        Arrays.sort(wrep.BGS_univariate_ROC);
       
        // remove-naive bayes loop
        Remove rm = new Remove();

        Classifier bn = new BayesNet();
//        Classifier bnCopy;
        Classifier bnCopyEv;

        Evaluation ev;
        K2 xK2 = new K2();
        xK2.setInitAsNaiveBayes(true);
        xK2.setMaxNrOfParents(1);
        ((BayesNet) bn).setSearchAlgorithm(xK2);

        int winningNaiveRemove = -1;
        double[] accuracyRes = new double[learnPostSel.numAttributes()];
        double[] ROCRes = new double[learnPostSel.numAttributes()];


        for (int removeNaive_counter = learnPostSel.numAttributes() -2;removeNaive_counter > 1 ; removeNaive_counter--){
            StringBuilder sb = new StringBuilder("");
            sb.append(removeNaive_counter).append("-").append(learnPostSel.numAttributes()-1);
            rm.setAttributeIndices(sb.toString());
            rm.setInputFormat(learnPostSel);
            Instances learnPostSelNaive = rm.useFilter(learnPostSel, rm);

            // TODO: make a chart here.

//            bnCopy = Classifier.makeCopy(bn);
//            bnCopyEv = Classifier.makeCopy(bn);
            ev = new Evaluation(learnPostSelNaive);
            ev.crossValidateModel(Classifier.makeCopy(bn), learnPostSelNaive,Math.min(CVfoldNum,learn.numInstances()),new Random(removeNaive_counter));
//            bnCopy.buildClassifier(learnPostSelNaive);   //this only gives the bayes score.. is that interesting?

            accuracyRes[removeNaive_counter] = ev.pctCorrect();
            ROCRes[removeNaive_counter] = ev.weightedAreaUnderROC();
        }

        wrep.BGS_multivariate_ROC_ACC = new double[2][learn.numAttributes()-3];

//        for (int mv_counter = 0; mv_counter<accuracyRes.length-2; mv_counter++){
            System.arraycopy(ROCRes, 2, wrep.BGS_multivariate_ROC_ACC[0], 0, learn.numAttributes()-3);
            System.arraycopy(accuracyRes, 2, wrep.BGS_multivariate_ROC_ACC[1], 0, learn.numAttributes()-3);
//            wrep.BGS_multivariate_ROC_ACC[1] = accuracyRes;
//        }

        winningNaiveRemove = pickWinner(accuracyRes,ROCRes);

            StringBuilder sb = new StringBuilder("");
            sb.append(winningNaiveRemove).append("-").append(learnPostSel.numAttributes()-1);

            rm.setAttributeIndices(sb.toString());
            rm.setInputFormat(learnPostSel);
            Instances learnPreRandRemove = rm.useFilter(learnPostSel, rm);
            Instances holdPreRandRemove = rm.useFilter(holdPostSel, rm);

            wrep.BGS_midAttributeSelectionNumberOfFeatures = learnPreRandRemove.numAttributes();
           
            ev = new Evaluation(learnPreRandRemove);
            ev.crossValidateModel(Classifier.makeCopy(bn), learnPreRandRemove,Math.min(CVfoldNum,learn.numInstances()),new Random(winningNaiveRemove));
//            bnCopy.buildClassifier(learnPostSelNaive);   //this only gives the bayes score.. is that interesting?

            double midAttACC = ev.pctCorrect();
            double midAttROC = ev.weightedAreaUnderROC();

            wrep.BGS_midAttributeACC = midAttACC;
            wrep.BGS_midAttributeROC = midAttROC;


        // random removal loop
            int numCats = Math.min(10, learnPreRandRemove.numAttributes()-1);
            int[] numToRem = new int[numCats];
            numToRem[0]= 1;
           
            for (int setup_counter = 1 ; setup_counter<numCats; setup_counter++){
                numToRem[setup_counter] = Math.round(learnPreRandRemove.numAttributes()/numCats)*setup_counter;
            }
           
            double[][] accPostRandRemove = new double[numCats][removeIterations];
            double[][] rocPostRandRemove = new double[numCats][removeIterations];
           
           

                                Classifier yBN = new BayesNet();
                    K2 yK2 = new K2();
                    yK2.setMaxNrOfParents(1);
                    yK2.setInitAsNaiveBayes(true);
                    SimpleEstimator ySE = new SimpleEstimator();
    //                ySE.setAlpha(5);
                    ((BayesNet) yBN).setSearchAlgorithm(yK2);
                    ((BayesNet) yBN).setEstimator(ySE);


            for (int numToRem_counter = 0; numToRem_counter<numToRem.length;numToRem_counter++){
                for (int iteration_counter = 0; iteration_counter < removeIterations; iteration_counter++){

                    Instances learnPostRandRemove = new Instances(learnPreRandRemove);
//                    Instances holdPostRandRemove = new Instances(holdPreRandRemove);

                     Random rnd = new Random(1+numToRem_counter*10000+iteration_counter*10);
//                     StringBuilder remString = new StringBuilder("");


                     for (int rmv_counter = 0 ; rmv_counter < numToRem[numToRem_counter]; rmv_counter++){

                        String theInt = String.valueOf(1+rnd.nextInt(learnPreRandRemove.numAttributes()-1-rmv_counter));
                        Remove rmv = new Remove();
//                        System.out.println(1+numToRem_counter*10000+iteration_counter*10);
//                        System.out.println(theInt);
//                        remString.append(theInt).append(";");
                        rmv.setAttributeIndices(theInt);

                        try{
                            rmv.setInputFormat(learnPostRandRemove);
                            learnPostRandRemove = rmv.useFilter(learnPostRandRemove, rmv);
//                            holdPostRandRemove = rmv.useFilter(holdPostRandRemove, rmv);
                        } catch (Exception ex) {System.out.println(ex.toString());}
                    } // rmv_counter

                     // now the postRandRemove instances are configured



                    Classifier yBNcopy = Classifier.makeCopy(yBN);

                    Evaluation evPostRand = new Evaluation(learnPostRandRemove);
                    evPostRand.crossValidateModel(yBNcopy, learnPostRandRemove,Math.min(CVfoldNum,learn.numInstances()),new Random(winningNaiveRemove));

                    accPostRandRemove[numToRem_counter][iteration_counter] = evPostRand.pctCorrect();
                    rocPostRandRemove[numToRem_counter][iteration_counter] = evPostRand.weightedAreaUnderROC();
//                    if ((numToRem_counter % 100)==0){System.out.print(numToRem_counter);System.out.println("RandomRemovalLoop: ".concat(String.valueOf(evPostRand.weightedAreaUnderROC())));}
                }
            }

            //pick a winner

            wrep.BGS_removes_ROC_HI_LO_AVG = new double[3][numCats];

              for (int numToRem_counter = 0; numToRem_counter<numToRem.length;numToRem_counter++){
                  Arrays.sort(rocPostRandRemove[numToRem_counter]);

                  wrep.BGS_removes_ROC_HI_LO_AVG[0][numToRem_counter] = rocPostRandRemove[numToRem_counter][rocPostRandRemove[numToRem_counter].length-1];
                  wrep.BGS_removes_ROC_HI_LO_AVG[1][numToRem_counter] = rocPostRandRemove[numToRem_counter][0];
                  wrep.BGS_removes_ROC_HI_LO_AVG[2][numToRem_counter] = rocPostRandRemove[numToRem_counter][Math.round((float) Math.ceil((rocPostRandRemove[numToRem_counter].length-1)/2))];
              }


        int[] postRandWinnerArray = new int[numCats];
        for (int numToRem_counter = 0; numToRem_counter<numToRem.length;numToRem_counter++){
            postRandWinnerArray[numToRem_counter] = pickWinner(accPostRandRemove[numToRem_counter], rocPostRandRemove[numToRem_counter]);
        }

        // TODO: make a chart here.



        double[] accWinners = new double[numCats];
        double[] rocWinners = new double[numCats];
        for (int numToRem_counter = 0; numToRem_counter<numToRem.length;numToRem_counter++){
            accWinners[numToRem_counter] = accPostRandRemove[numToRem_counter][postRandWinnerArray[numToRem_counter]];
            rocWinners[numToRem_counter] = rocPostRandRemove[numToRem_counter][postRandWinnerArray[numToRem_counter]];
        }





        int winningPostRandRemove = pickWinner(accWinners, rocWinners);

            double[] accFinalRound = new double[removeIterations*10];
            double[] rocFinalRound = new double[removeIterations*10];



            double accFinal = -1;
            double rocFinal = -1;


                    Classifier zBN = new BayesNet();
                    K2 zK2 = new K2();
                    zK2.setMaxNrOfParents(1);
                    zK2.setInitAsNaiveBayes(true);
                    SimpleEstimator zSE = new SimpleEstimator();
    //                ySE.setAlpha(5);
View Full Code Here

        Remove rm = new Remove();
        rm.setInvertSelection(true);

        Instances setOf2;

        Classifier bn = new BayesNet();
        Classifier bnCopy;

        Evaluation ev;
        K2 xK2 = new K2();
        xK2.setInitAsNaiveBayes(true);
View Full Code Here

TOP

Related Classes of weka.classifiers.bayes.BayesNet

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.