Package weka.filters.unsupervised.attribute

Examples of weka.filters.unsupervised.attribute.Remove


      if (m_ignoreKeyList.isSelectedIndex(classIndex)) {
  m_ignoreKeyList.removeSelectionInterval(classIndex, classIndex);
      }
    }
    int [] selected = m_ignoreKeyList.getSelectedIndices();
    Remove af = new Remove();
    Instances retI = null;

    try {
      af.setAttributeIndicesArray(selected);
      af.setInvertSelection(false);
      af.setInputFormat(inst);
      retI = Filter.useFilter(inst, af);
    } catch (Exception e) {
      e.printStackTrace();
    }
   
View Full Code Here


    return retI;
  }

  private Instances removeIgnoreCols(Instances inst, int[] toIgnore) {

    Remove af = new Remove();
    Instances retI = null;

    try {
      af.setAttributeIndicesArray(toIgnore);
      af.setInvertSelection(false);
      af.setInputFormat(inst);
      retI = Filter.useFilter(inst, af);
    } catch (Exception e) {
      e.printStackTrace();
    }
   
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);
                    ((BayesNet) zBN).setSearchAlgorithm(zK2);
                    ((BayesNet) zBN).setEstimator(zSE);

                for (int iteration_counter = 0; iteration_counter < removeIterations; iteration_counter++){

                    Instances learnFinalRound = new Instances(learnPreRandRemove);
                    Instances holdFinalRound = new Instances(holdPreRandRemove);

                     rndd.setSeed(iteration_counter);
                     StringBuilder remString = new StringBuilder("");


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

                        String theInt = String.valueOf(1+rndd.nextInt(learnPreRandRemove.numAttributes()-1-rmv_counter));
                        Remove rmv = new Remove();
                        remString.append(theInt).append(";");
                        rmv.setAttributeIndices(theInt);

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

                     // now the postRandRemove instances are configured

View Full Code Here

            Instances filt_learn = new Instances(learn);
            Instances filt_hold = new Instances(hold);

            if (!aRemoves.equals("")) {
                Remove theRemove = new Remove();
                String[] opt = {"-R", aRemoves};
                theRemove.setOptions(opt);
                theRemove.setInputFormat(filt_learn);
                filt_learn = Filter.useFilter(filt_learn, theRemove);
                theRemove.setInputFormat(filt_hold);
                filt_hold = Filter.useFilter(filt_hold, theRemove);
            }


            if (!aDiscrete.isEmpty()){
View Full Code Here

   * @throws Exception
   */
    private void oldRunAttributeSelection(Instances data, int classIndex, Random rnd) throws Exception {
        /// START LOOP - REMOVE ALL BUT ONE ATTRIBUTE + CLASS

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

        Instances setOf2;

        Classifier bn = new BayesNet();
        Classifier bnCopy;

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

        m_ROCs = new double[data.numAttributes()];

        for (int att_counter = 0; att_counter < data.numAttributes(); att_counter++){
            if (att_counter != classIndex){

                // Remove all attributes but the current attribute and the class variable
                rm.setAttributeIndicesArray(new int[] {att_counter,classIndex});
                rm.setInputFormat(data);
                setOf2 = rm.useFilter(data, rm);

               

                bnCopy = Classifier.makeCopy(bn);
                ev = new Evaluation(setOf2);
View Full Code Here

            }
        }
        cutOffString.append("last");

        // removes beyond threshold
        Remove rm = new Remove();
        rm.setInvertSelection(true);
        rm.setAttributeIndices(cutOffString.toString());


        rm.setInputFormat(learnPostSel);
        learnFinal = Filter.useFilter(learnPostSel, rm);
        holdFinal = Filter.useFilter(holdPostSel, rm);

        saveFile();
    }
View Full Code Here

TOP

Related Classes of weka.filters.unsupervised.attribute.Remove

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.