Example usage for weka.attributeSelection Ranker setNumToSelect

List of usage examples for weka.attributeSelection Ranker setNumToSelect

Introduction

In this page you can find the example usage for weka.attributeSelection Ranker setNumToSelect.

Prototype

@Override
public void setNumToSelect(int n) 

Source Link

Document

Specify the number of attributes to select from the ranked list.

Usage

From source file:FeatureSelectionClass.java

public AttributeSelection withGainRatio(String path) throws Exception {
    int N;/*from  w  w  w  .  j a va  2s .c o  m*/
    PreparingSteps pr = new PreparingSteps();
    N = pr.getReadFileData(path).numAttributes();
    Instances data = pr.getReadFileData(path);

    AttributeSelection selector = new AttributeSelection();
    InfoGainAttributeEval evaluator = new InfoGainAttributeEval();
    Ranker ranker = new Ranker();
    ranker.setNumToSelect(Math.min(500, N - 1));
    selector.setEvaluator(evaluator);
    selector.setSearch(ranker);
    selector.SelectAttributes(data);
    return selector;

}

From source file:FeatureSelectionClass.java

public AttributeSelection withInfoGain(String path) throws Exception {
    int N;/*from w w  w .  j a v  a  2 s  .c o m*/
    PreparingSteps pr = new PreparingSteps();
    N = pr.getReadFileData(path).numAttributes();
    Instances data = pr.getReadFileData(path);

    AttributeSelection selector = new AttributeSelection();
    GainRatioAttributeEval evaluator = new GainRatioAttributeEval();
    Ranker ranker = new Ranker();
    ranker.setNumToSelect(Math.min(500, N - 1));
    selector.setEvaluator(evaluator);
    selector.setSearch(ranker);
    selector.SelectAttributes(data);
    return selector;
}

From source file:FeatureSelectionClass.java

public AttributeSelection withChiSquare(String path) throws Exception {
    int N;//from ww  w. ja v a2  s . co  m
    PreparingSteps pr = new PreparingSteps();
    N = pr.getReadFileData(path).numAttributes();
    Instances data = pr.getReadFileData(path);

    AttributeSelection selector = new AttributeSelection();
    ChiSquaredAttributeEval evaluator = new ChiSquaredAttributeEval();
    Ranker ranker = new Ranker();
    ranker.setNumToSelect(Math.min(500, N - 1));
    selector.setEvaluator(evaluator);
    selector.setSearch(ranker);
    selector.SelectAttributes(data);
    return selector;

}

From source file:es.uvigo.ei.sing.gc.model.entities.GeneSelectionMetaData.java

License:Open Source License

public FeatureSelector createFeatureSelection() throws Exception {
    final Ranker ranker = new Ranker();
    ranker.setNumToSelect(this.numGenes);

    return new WekaFeatureSelector(this.getASEvaluationBuilder().buildEvaluator(), ranker);
}

From source file:etc.aloe.oilspill2010.FeatureGenerationImpl.java

License:Open Source License

protected Filter getFeatureSelectionFilter(ExampleSet examples) throws Exception {

    AttributeSelection filter = new AttributeSelection(); // package weka.filters.supervised.attribute!
    //CfsSubsetEval eval = new CfsSubsetEval();

    //CorrelationAttributeEval eval = new CorrelationAttributeEval();
    //InfoGainAttributeEval eval = new InfoGainAttributeEval();

    ReliefFAttributeEval eval = new ReliefFAttributeEval();

    //GreedyStepwise search = new GreedyStepwise();
    //search.setNumToSelect(980);
    //search.setSearchBackwards(true);

    Ranker search = new Ranker();
    search.setNumToSelect(980);

    filter.setEvaluator(eval);//from   www.  j  a  v a 2s  .c om
    filter.setSearch(search);

    filter.setInputFormat(examples.getInstances());
    Instances filtered = Filter.useFilter(examples.getInstances(), filter);
    examples.setInstances(filtered);

    return filter;
}

From source file:it.poliba.sisinflab.simlib.featureSelection.methods.CHI.java

public void execute(String dataset) {
    try {/*  www. j  a  v  a 2s  . co m*/

        if (dataset.length() == 0)
            throw new IllegalArgumentException();
        // Load input dataset.
        DataSource source = new DataSource(dataset);
        System.out.println("Reading instances...");
        Instances data = source.getDataSet();

        // Performs a principal components analysis.
        ChiSquaredAttributeEval chiEvaluator = new ChiSquaredAttributeEval();

        // Ranking the attributes.
        Ranker ranker = new Ranker();
        // Specify the number of attributes to select from the ranked list.
        /*ranker.setThreshold(-1.7976931348623157E308);
        ranker.setNumToSelect(-1);
        ranker.setGenerateRanking(true);*/
        ranker.setNumToSelect(-1);

        AttributeSelection selector = new AttributeSelection();
        System.out.println("Selecting attributes...");
        selector.setSearch(ranker);
        selector.setEvaluator(chiEvaluator);
        selector.SelectAttributes(data);

        PrintStream o = new PrintStream(new File("data/" + "CHIResults" + ".txt"));
        System.setOut(o);
        System.out.println(Arrays.toString(selector.rankedAttributes()));
        System.out.println(Arrays.toString(selector.selectedAttributes()));
        //System.out.println(selector.CVResultsString());
        System.out.println(selector.toResultsString());

        System.out.println();

    } catch (IllegalArgumentException e) {
        System.err.println("Error");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:it.poliba.sisinflab.simlib.featureSelection.methods.PCA.java

public void execute(String dataset) {
    try {/*ww w  .  j a v  a 2  s  .  c  o m*/

        if (dataset.length() == 0)
            throw new IllegalArgumentException();
        // Load input dataset.
        DataSource source = new DataSource(dataset);
        Instances data = source.getDataSet();

        // Performs a principal components analysis.
        PrincipalComponents pcaEvaluator = new PrincipalComponents();

        // Sets the amount of variance to account for when retaining principal
        // components.
        pcaEvaluator.setVarianceCovered(1.0);
        // Sets maximum number of attributes to include in transformed attribute
        // names.
        pcaEvaluator.setMaximumAttributeNames(-1);

        // Scaled X such that the variance of each feature is 1.
        boolean scale = true;
        if (scale) {
            pcaEvaluator.setCenterData(true);
        } else {
            pcaEvaluator.setCenterData(false);
        }

        // Ranking the attributes.
        Ranker ranker = new Ranker();

        ranker.setNumToSelect(-1);

        AttributeSelection selector = new AttributeSelection();
        selector.setSearch(ranker);
        selector.setEvaluator(pcaEvaluator);
        selector.SelectAttributes(data);

        // Transform data into eigenvector basis.
        Instances transformedData = selector.reduceDimensionality(data);
        PrintStream o = new PrintStream(new File("data/" + "PCAResults" + ".txt"));
        System.setOut(o);
        System.out.println(Arrays.toString(selector.rankedAttributes()));
        System.out.println(Arrays.toString(selector.selectedAttributes()));
        //System.out.println(selector.CVResultsString());
        System.out.println(selector.toResultsString());

        System.out.println();

    } catch (IllegalArgumentException e) {
        System.err.println("Error");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:moa.reduction.core.ReductionClassifier.java

License:Open Source License

private Instance performFS(Instance rinst) {
    // Feature selection process performed before
    weka.core.Instance winst = new weka.core.DenseInstance(rinst.weight(), rinst.toDoubleArray());

    if (fselector != null) {
        if (fselector.isUpdated() && totalCount % winSizeOption.getValue() == 0) {
            fselector.applySelection();// w ww .j a  v a 2s.  c o  m
            selector = new AttributeSelection();
            Ranker ranker = new Ranker();
            ranker.setNumToSelect(Math.min(numFeaturesOption.getValue(), winst.numAttributes() - 1));
            selector.setEvaluator((ASEvaluation) fselector);
            selector.setSearch(ranker);

            ArrayList<Attribute> list = new ArrayList<Attribute>();
            //ArrayList<Attribute> list = Collections.list(winst.enumerateAttributes());
            //list.add(winst.classAttribute());
            for (int i = 0; i < rinst.numAttributes(); i++)
                list.add(new Attribute(rinst.attribute(i).name(), i));
            //ArrayList<Attribute> list = Collections.list(winst.enumerateAttributes());
            //list.add(winst.classAttribute());
            weka.core.Instances single = new weka.core.Instances("single", list, 1);
            single.setClassIndex(rinst.classIndex());
            single.add(winst);
            try {
                selector.SelectAttributes(single);
                System.out.println("Selected features: " + selector.toResultsString());
                selectedFeatures.clear();
                for (int att : selector.selectedAttributes())
                    selectedFeatures.add(att);
                WekaToSamoaInstanceConverter convWS = new WekaToSamoaInstanceConverter();
                return convWS.samoaInstance(selector.reduceDimensionality(winst));
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return rinst;
}

From source file:mulan.classifier.transformation.MultiLabelStacking.java

License:Open Source License

/**
 * Initializes all the parameters used in the meta-level.
 * Calculates the correlated labels if meta-level pruning is applied.
 *
 * @param dataSet// ww w .j  av  a  2 s  .c om
 * @param metaClassifier
 * @param includeAttrs
 * @param metaPercentage
 * @param eval
 * @throws Exception
 */
public void initializeMetaLevel(MultiLabelInstances dataSet, Classifier metaClassifier, boolean includeAttrs,
        double metaPercentage, ASEvaluation eval) throws Exception {
    this.metaClassifier = metaClassifier;
    metaLevelEnsemble = AbstractClassifier.makeCopies(metaClassifier, numLabels);
    metaLevelData = new Instances[numLabels];
    metaLevelFilteredEnsemble = new FilteredClassifier[numLabels];
    this.includeAttrs = includeAttrs;
    // calculate the number of correlated labels that corresponds to the
    // given percentage
    topkCorrelated = (int) Math.floor(metaPercentage * numLabels);
    if (topkCorrelated < 1) {
        debug("Too small percentage, selecting k=1");
        topkCorrelated = 1;
    }
    if (topkCorrelated < numLabels) {// pruning should be applied
        selectedAttributes = new int[numLabels][];
        if (eval == null) {// calculate the PhiCoefficient
            Statistics phi = new Statistics();
            phi.calculatePhi(dataSet);
            for (int i = 0; i < numLabels; i++) {
                selectedAttributes[i] = phi.topPhiCorrelatedLabels(i, topkCorrelated);
            }
        } else {// apply feature selection
            AttributeSelection attsel = new AttributeSelection();
            Ranker rankingMethod = new Ranker();
            rankingMethod.setNumToSelect(topkCorrelated);
            attsel.setEvaluator(eval);
            attsel.setSearch(rankingMethod);
            // create a dataset consisting of all the classes of each
            // instance plus the class we want to select attributes from
            for (int i = 0; i < numLabels; i++) {
                ArrayList<Attribute> attributes = new ArrayList<Attribute>();

                for (int j = 0; j < numLabels; j++) {
                    attributes.add(train.attribute(labelIndices[j]));
                }
                attributes.add(train.attribute(labelIndices[i]).copy("meta"));

                Instances iporesult = new Instances("Meta format", attributes, 0);
                iporesult.setClassIndex(numLabels);
                for (int k = 0; k < train.numInstances(); k++) {
                    double[] values = new double[numLabels + 1];
                    for (int m = 0; m < numLabels; m++) {
                        values[m] = Double.parseDouble(train.attribute(labelIndices[m])
                                .value((int) train.instance(k).value(labelIndices[m])));
                    }
                    values[numLabels] = Double.parseDouble(train.attribute(labelIndices[i])
                            .value((int) train.instance(k).value(labelIndices[i])));
                    Instance metaInstance = DataUtils.createInstance(train.instance(k), 1, values);
                    metaInstance.setDataset(iporesult);
                    iporesult.add(metaInstance);
                }
                attsel.SelectAttributes(iporesult);
                selectedAttributes[i] = attsel.selectedAttributes();
                iporesult.delete();
            }
        }
    }
}

From source file:net.semanticmetadata.lire.classifiers.HashingSearchBasedClassifierMod.java

License:Open Source License

private static HashMap<String, Double> calculateInformationGain(String wekaFileLocation,
        double[] featureInformationGain, int featureSpace[], HashMap<String, Integer> featureSpaceHashMap,
        ArrayList<String> featureOrder, HashMap<String, Double> featureInformationGainHashMap) {

    Instances data = null;// w w w.j  a  v a2s.  co m
    try {
        data = new Instances(new BufferedReader(new FileReader(wekaFileLocation)));
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    AttributeSelection attsel = new AttributeSelection(); // package weka.attributeSelection!
    InfoGainAttributeEval eval = new InfoGainAttributeEval();
    Ranker search = new Ranker();
    search.setThreshold(-1.7976931348623157E308);
    search.setNumToSelect(-1);
    search.setGenerateRanking(true);
    attsel.setEvaluator(eval);
    attsel.setSearch(search);
    try {

        attsel.SelectAttributes(data);
    } catch (Exception e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    // obtain the attribute indices that were selected
    int[] indices = new int[0];
    double[][] rankedAttribuesArray = new double[0][0];
    try {
        rankedAttribuesArray = attsel.rankedAttributes();
    } catch (Exception e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    try {
        indices = attsel.selectedAttributes();
    } catch (Exception e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

    for (int i = 0; i < rankedAttribuesArray.length; i++) {

        int currentFeature = Integer.parseInt(data.attribute((int) rankedAttribuesArray[i][0]).name()
                .substring(0, data.attribute((int) rankedAttribuesArray[i][0]).name().indexOf("_")));
        //System.out.println("DDDDDDDDDDDDDD"+currentFeature);
        // System.out.print(data.attribute((int) rankedAttribuesArray[i][0]).name() + "/" + rankedAttribuesArray[i][0] + "/");
        //     System.out.println(rankedAttribuesArray[i][1]);
        // data.attribute((int) rankedAttribuesArray[i][0]).name().substring(0,data.attribute((int) rankedAttribuesArray[i][0]).name().indexOf("_"));
        // featureInformationGain[currentFeature] = featureInformationGain[currentFeature] + rankedAttribuesArray[i][1];
        featureInformationGainHashMap.put(featureOrder.get(currentFeature),
                featureInformationGainHashMap.get(featureOrder.get(currentFeature))
                        + rankedAttribuesArray[i][1]);
    }

    //Caalculate the mean of the information gain (better comparable)
    // for (int i = 0; i < featureInformationGain.length; i++) {
    //     featureInformationGain[i] = (featureInformationGain[i] / featureSpace[i]) * 100;
    // }

    //Calculate the mean of the information gain (better comparable)
    for (int i = 0; i < featureOrder.size(); i++) {
        //  featureInformationGainHashMap.put(featureOrder.get(i), (featureInformationGainHashMap.get(featureOrder.get(i)) / featureSpaceHashMap.get(featureOrder.get(i))) * 100);
        featureInformationGainHashMap.put(featureOrder.get(i),
                (featureInformationGainHashMap.get(featureOrder.get(i))));
    }

    // for(int i=0;i<0;i++){
    //     System.out.println(data.attribute(indices[i]).toString());
    // }
    System.out.println("Scoring finished, starting with classification! Scores: ");
    for (int i = 0; i < featureOrder.size(); i++) {
        System.out.println(featureOrder.get(i) + " " + featureInformationGainHashMap.get(featureOrder.get(i)));
        // featureInformationGainHashMap.put(featureOrder.get(i),(featureInformationGainHashMap.get(featureOrder.get(i))/featureSpaceHashMap.get(featureOrder.get(i)))*100);
    }
    // return featureInformationGain;
    File deleteFile = new File(wekaFileLocation);
    deleteFile.delete();
    return featureInformationGainHashMap;
}