Example usage for weka.filters.unsupervised.attribute Remove setOptions

List of usage examples for weka.filters.unsupervised.attribute Remove setOptions

Introduction

In this page you can find the example usage for weka.filters.unsupervised.attribute Remove setOptions.

Prototype

@Override
public void setOptions(String[] options) throws Exception 

Source Link

Document

Parses a given list of options.

Usage

From source file:fcul.viegas.ml.learners.NetworkStreamLearningClassifierMapFunction.java

public void open(Configuration parameters) throws Exception {

    try {/*w  ww  . j av a2s  .co  m*/
        BufferedReader reader = new BufferedReader(new FileReader(NetworkPacketsDefinitions.arffPath));
        ArffLoader.ArffReader arff = new ArffLoader.ArffReader(reader);
        weka.core.Instances dataTrain = arff.getData();
        dataTrain.setClassIndex(dataTrain.numAttributes() - 1);

        Random rand = new Random();
        rand.setSeed(this.hashCode() ^ 83484856847L);
        ArrayList<Integer> listaFeaturesChoosen = new ArrayList<>();
        for (int j = 1; j < dataTrain.numAttributes(); j++) {
            listaFeaturesChoosen.add(j);
        }

        //chose features to remove
        int numberOfFeaturesToBeRemoved = (int) (listaFeaturesChoosen.size()
                * (1.0f - percentOfSubsetFeatures));
        for (int j = 0; j < numberOfFeaturesToBeRemoved; j++) {
            int numberOfFeature = rand.nextInt(listaFeaturesChoosen.size());
            listaFeaturesChoosen.remove(numberOfFeature);
        }
        System.out.println("LEARNING: featureSubset: " + NetworkStreamLearningClassifierMapFunction
                .printFeatures(listaFeaturesChoosen, dataTrain.numAttributes() - 1));
        mappingFeatures = "featureSubset: " + NetworkStreamLearningClassifierMapFunction
                .printFeatures(listaFeaturesChoosen, dataTrain.numAttributes() - 1);
        FastVector attributes = new FastVector();
        int indexLista = 0;
        for (int i = 0; i < NetworkPacketsDefinitions.numberOfFeatures; i++) {
            if (indexLista < listaFeaturesChoosen.size() && listaFeaturesChoosen.get(indexLista) == (i + 1)) {
                indexLista++;
                attributes.addElement(new Attribute("att" + (i + 1)));
            }
        }
        FastVector classLabels = new FastVector();
        for (int i = 0; i < NetworkPacketsDefinitions.numberOfClasses; i++) {
            classLabels.addElement("class" + (i + 1));
        }
        attributes.addElement(new Attribute("class", classLabels));

        this.streamLearningInstanceHeader = new InstancesHeader(new Instances("FlinkPhd", attributes, 0));

        this.streamLearningInstanceHeader.setClassIndex(attributes.size());

        listaFeaturesChoosen.add(dataTrain.numAttributes());

        String[] options = new String[2];
        options[0] = "-R";

        String optRemove = "";
        for (int j = 0; j < listaFeaturesChoosen.size() - 1; j++) {
            optRemove = optRemove + listaFeaturesChoosen.get(j) + ",";
        }
        optRemove = optRemove + listaFeaturesChoosen.get(listaFeaturesChoosen.size() - 1);
        options[1] = optRemove;

        Remove remove = new Remove();
        remove.setOptions(options);
        remove.setInvertSelection(true);
        remove.setInputFormat(dataTrain);

        weka.core.Instances newdataFeat = Filter.useFilter(dataTrain, remove);

        HoeffdingTree tree = new HoeffdingTree();

        weka.classifiers.misc.InputMappedClassifier classifier = new weka.classifiers.misc.InputMappedClassifier();
        classifier.setModelHeader(newdataFeat);
        classifier.setClassifier(tree);
        classifier.buildClassifier(newdataFeat);
        this.classifier = classifier;
        this.coreInstances = dataTrain;
        this.coreInstances.clear();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:fcul.viegas.ml.learners.NetworkStreamLearningClassifierUpdateMapFunction.java

public void open(Configuration parameters) throws Exception {

    try {//from   ww w  . j  a v  a  2s  . co  m
        BufferedReader reader = new BufferedReader(new FileReader(NetworkPacketsDefinitions.arffPath));
        ArffLoader.ArffReader arff = new ArffLoader.ArffReader(reader);
        weka.core.Instances dataTrain = arff.getData();
        dataTrain.setClassIndex(dataTrain.numAttributes() - 1);

        Random rand = new Random();
        rand.setSeed(this.hashCode() ^ 83484856847L);
        ArrayList<Integer> listaFeaturesChoosen = new ArrayList<>();
        for (int j = 1; j < dataTrain.numAttributes(); j++) {
            listaFeaturesChoosen.add(j);
        }

        //chose features to remove
        int numberOfFeaturesToBeRemoved = (int) (listaFeaturesChoosen.size()
                * (1.0f - percentOfSubsetFeatures));
        for (int j = 0; j < numberOfFeaturesToBeRemoved; j++) {
            int numberOfFeature = rand.nextInt(listaFeaturesChoosen.size());
            listaFeaturesChoosen.remove(numberOfFeature);
        }
        System.out.println("UPDATES featureSubset: " + NetworkStreamLearningClassifierMapFunction
                .printFeatures(listaFeaturesChoosen, dataTrain.numAttributes() - 1));
        mappingFeatures = "featureSubset: " + NetworkStreamLearningClassifierMapFunction
                .printFeatures(listaFeaturesChoosen, dataTrain.numAttributes() - 1);
        FastVector attributes = new FastVector();
        int indexLista = 0;
        for (int i = 0; i < NetworkPacketsDefinitions.numberOfFeatures; i++) {
            if (indexLista < listaFeaturesChoosen.size() && listaFeaturesChoosen.get(indexLista) == (i + 1)) {
                indexLista++;
                attributes.addElement(new Attribute("att" + (i + 1)));
            }
        }
        FastVector classLabels = new FastVector();
        for (int i = 0; i < NetworkPacketsDefinitions.numberOfClasses; i++) {
            classLabels.addElement("class" + (i + 1));
        }
        attributes.addElement(new Attribute("class", classLabels));

        this.streamLearningInstanceHeader = new InstancesHeader(new Instances("FlinkPhd", attributes, 0));

        this.streamLearningInstanceHeader.setClassIndex(attributes.size());

        listaFeaturesChoosen.add(dataTrain.numAttributes());

        String[] options = new String[2];
        options[0] = "-R";

        String optRemove = "";
        for (int j = 0; j < listaFeaturesChoosen.size() - 1; j++) {
            optRemove = optRemove + listaFeaturesChoosen.get(j) + ",";
        }
        optRemove = optRemove + listaFeaturesChoosen.get(listaFeaturesChoosen.size() - 1);
        options[1] = optRemove;

        Remove remove = new Remove();
        remove.setOptions(options);
        remove.setInvertSelection(true);
        remove.setInputFormat(dataTrain);

        weka.core.Instances newdataFeat = Filter.useFilter(dataTrain, remove);

        HoeffdingTree tree = new HoeffdingTree();

        weka.classifiers.misc.InputMappedClassifier classifier = new weka.classifiers.misc.InputMappedClassifier();
        classifier.setModelHeader(newdataFeat);
        classifier.setClassifier(tree);
        classifier.buildClassifier(newdataFeat);
        this.classifier = classifier;
        this.coreInstances = dataTrain;

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:hero.unstable.util.classification.wekaData.java

/**
 * Type of data is: "training" or "test"
 *//*from ww w.  j a  v  a2 s  .co  m*/
public Instances filterAttributes(String type, List<Integer> attributes) {
    // Copy data:        
    Instances auxData = null;
    auxData = (type.equals("training")) ? dataTraining : dataTest;

    // REMOVE non selected attributes (features)
    String[] options = new String[2];
    options[0] = "-R"; // "range"
    options[1] = attributes.toString().replace("[", "").replace("]", ""); //  range of attributes
    Remove remove = new Remove(); // new instance of filter
    try {
        remove.setOptions(options); // set options
        remove.setInputFormat(auxData); // inform filter about dataset **AFTER** setting options
        auxData = Filter.useFilter(auxData, remove); // Data with selected feautres

    } catch (Exception ex) {
        logger.info(ClusteringBinaryPD.class.getName());
        ex.printStackTrace();
    }
    return auxData;
}

From source file:ia02classificacao.IA02Classificacao.java

/**
 * @param args the command line arguments
 *///from   w  w w .j a  v  a2s  .  co  m
public static void main(String[] args) throws Exception {

    // abre o banco de dados arff e mostra a quantidade de instancias (linhas)
    DataSource arquivo = new DataSource("data/zoo.arff");
    Instances dados = arquivo.getDataSet();
    System.out.println("Instancias lidas: " + dados.numInstances());

    // FILTER: remove o atributo nome do animal da classificao
    String[] parametros = new String[] { "-R", "1" };
    Remove filtro = new Remove();
    filtro.setOptions(parametros);
    filtro.setInputFormat(dados);
    dados = Filter.useFilter(dados, filtro);

    AttributeSelection selAtributo = new AttributeSelection();
    InfoGainAttributeEval avaliador = new InfoGainAttributeEval();
    Ranker busca = new Ranker();
    selAtributo.setEvaluator(avaliador);
    selAtributo.setSearch(busca);
    selAtributo.SelectAttributes(dados);
    int[] indices = selAtributo.selectedAttributes();
    System.out.println("Selected attributes: " + Utils.arrayToString(indices));

    // Usa o algoritimo J48 e mostra a classificao dos dados em forma textual
    String[] opcoes = new String[1];
    opcoes[0] = "-U";
    J48 arvore = new J48();
    arvore.setOptions(opcoes);
    arvore.buildClassifier(dados);
    System.out.println(arvore);

    // Usa o algoritimo J48 e mostra a classificao de dados em forma grafica
    /*
    TreeVisualizer tv = new TreeVisualizer(null, arvore.graph(), new PlaceNode2());
    JFrame frame = new javax.swing.JFrame("?rvore de Conhecimento");
    frame.setSize(800,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(tv);
    frame.setVisible(true);
    tv.fitToScreen();
    */

    /*
    * Classificao de novos dados
    */

    System.out.println("\n\nCLASSIFICAO DE NOVOS DADOS");
    // criar atributos
    double[] vals = new double[dados.numAttributes()];
    vals[0] = 1.0; // hair
    vals[1] = 0.0; // feathers
    vals[2] = 0.0; // eggs
    vals[3] = 1.0; // milk
    vals[4] = 1.0; // airborne
    vals[5] = 0.0; // aquatic
    vals[6] = 0.0; // predator
    vals[7] = 1.0; // toothed
    vals[8] = 1.0; // backbone
    vals[9] = 1.0; // breathes
    vals[10] = 0.0; // venomous
    vals[11] = 0.0; // fins
    vals[12] = 4.0; // legs
    vals[13] = 1.0; // tail
    vals[14] = 1.0; // domestic
    vals[15] = 1.0; // catsize

    // Criar uma instncia baseada nestes atributos
    Instance meuUnicornio = new DenseInstance(1.0, vals);

    // Adicionar a instncia nos dados
    meuUnicornio.setDataset(dados);

    // Classificar esta nova instncia
    double label = arvore.classifyInstance(meuUnicornio);

    // Imprimir o resultado da classificao
    System.out.println("Novo Animal: Unicrnio");
    System.out.println("classificacao: " + dados.classAttribute().value((int) label));

    /*
    * Avaliao e predio de erros de mtrica
    */
    System.out.println("\n\nAVALIAO E PREDIO DE ERROS DE MTRICA");
    Classifier cl = new J48();
    Evaluation eval_roc = new Evaluation(dados);
    eval_roc.crossValidateModel(cl, dados, 10, new Random(1), new Object[] {});
    System.out.println(eval_roc.toSummaryString());

    /*
    * Matriz de confuso
    */
    System.out.println("\n\nMATRIZ DE CONFUSO");
    double[][] confusionMatrix = eval_roc.confusionMatrix();
    System.out.println(eval_roc.toMatrixString());

}

From source file:ia03classificador.jFrClassificador.java

public void doClassificate() throws Exception {

    // Quando clicado, a variavel recebe 1, quando no clicado recebe 0
    v00 = ((btn00.isSelected()) ? ((double) 1) : ((double) 0));
    v01 = ((btn01.isSelected()) ? ((double) 1) : ((double) 0));
    v02 = ((btn02.isSelected()) ? ((double) 1) : ((double) 0));
    v03 = ((btn03.isSelected()) ? ((double) 1) : ((double) 0));
    v04 = ((btn04.isSelected()) ? ((double) 1) : ((double) 0));
    v05 = ((btn05.isSelected()) ? ((double) 1) : ((double) 0));
    v06 = ((btn06.isSelected()) ? ((double) 1) : ((double) 0));
    v07 = ((btn07.isSelected()) ? ((double) 1) : ((double) 0));
    v08 = ((btn08.isSelected()) ? ((double) 1) : ((double) 0));
    v09 = ((btn09.isSelected()) ? ((double) 1) : ((double) 0));
    v10 = ((btn10.isSelected()) ? ((double) 1) : ((double) 0));
    v11 = ((btn11.isSelected()) ? ((double) 1) : ((double) 0));
    v13 = ((btn13.isSelected()) ? ((double) 1) : ((double) 0));
    v14 = ((btn14.isSelected()) ? ((double) 1) : ((double) 0));
    v15 = ((btn15.isSelected()) ? ((double) 1) : ((double) 0));
    legs = txtLegs.getText();/*from ww  w.jav  a  2  s. c o  m*/
    legs = ((legs == null || legs.trim().isEmpty() ? "2" : legs));
    name = txtName.getText();

    // abre o banco de dados arff e guarda os registros no objeto dados
    ConverterUtils.DataSource arquivo = new ConverterUtils.DataSource("data/zoo.arff");
    Instances dados = arquivo.getDataSet();

    // FILTER: remove o atributo nome do animal da classificao
    String[] parametros = new String[] { "-R", "1" };
    Remove filtro = new Remove();
    filtro.setOptions(parametros);
    filtro.setInputFormat(dados);
    dados = Filter.useFilter(dados, filtro);

    AttributeSelection selAtributo = new AttributeSelection();
    InfoGainAttributeEval avaliador = new InfoGainAttributeEval();
    Ranker busca = new Ranker();
    selAtributo.setEvaluator(avaliador);
    selAtributo.setSearch(busca);
    selAtributo.SelectAttributes(dados);
    int[] indices = selAtributo.selectedAttributes();
    //System.out.println("Selected attributes: " + Utils.arrayToString(indices));

    // Usa o algoritimo J48 para montar a arvore de dados
    String[] opcoes = new String[1];
    opcoes[0] = "-U";
    J48 arvore = new J48();
    arvore.setOptions(opcoes);
    arvore.buildClassifier(dados);

    // cria o novo elemento para comparao
    double[] vals = new double[dados.numAttributes()];
    vals[0] = v00; // hair
    vals[1] = v01; // feathers
    vals[2] = v02; // eggs
    vals[3] = v03; // milk
    vals[4] = v04; // airborne
    vals[5] = v05; // aquatic
    vals[6] = v06; // predator
    vals[7] = v07; // toothed
    vals[8] = v08; // backbone
    vals[9] = v09; // breathes
    vals[10] = v10; // venomous
    vals[11] = v11; // fins
    vals[12] = Double.parseDouble(legs); // legs
    vals[13] = v13; // tail
    vals[14] = v14; // domestic
    vals[15] = v15; // catsize

    // Criar uma instncia baseada nestes atributos
    Instance newAnimal = new DenseInstance(1.0, vals);

    // Adicionar a instncia nos dados
    newAnimal.setDataset(dados);

    // Classificar esta nova instncia
    double label = arvore.classifyInstance(newAnimal);

    // Imprimir o resultado da classificao
    lblClassification.setText(dados.classAttribute().value((int) label));

}

From source file:id3j48.WekaAccess.java

public static Instances removeAttribute(Instances data, int index) throws Exception {
    String options = "-R " + String.valueOf(index);
    Remove remove = new Remove();
    remove.setOptions(Utils.splitOptions(options));
    remove.setInputFormat(data);/*from  w ww .j a  va 2 s  . co  m*/
    Instances newDataSet = Filter.useFilter(data, remove);
    return newDataSet;
}

From source file:newclassifier.NewClassifier.java

public void removeAttribute(String[] options) throws Exception {
    Remove remove = new Remove();
    remove.setOptions(options);
    data = Filter.useFilter(data, remove);
}

From source file:nl.uva.sne.classifiers.Hierarchical.java

@Override
public Map<String, String> cluster(String inDir) throws IOException, ParseException {
    try {/*  ww w.j  a va2 s.co m*/

        Instances data = ClusterUtils.terms2Instances(inDir, false);

        //            ArffSaver s = new ArffSaver();
        //            s.setInstances(data);
        //            s.setFile(new File(inDir+"/dataset.arff"));
        //            s.writeBatch();

        DistanceFunction df;
        //            SimpleKMeans currently only supports the Euclidean and Manhattan distances.
        switch (distanceFunction) {
        case "Minkowski":
            df = new MinkowskiDistance(data);
            break;
        case "Euclidean":
            df = new EuclideanDistance(data);
            break;
        case "Chebyshev":
            df = new ChebyshevDistance(data);
            break;
        case "Manhattan":
            df = new ManhattanDistance(data);
            break;
        default:
            df = new EuclideanDistance(data);
            break;
        }

        Logger.getLogger(Hierarchical.class.getName()).log(Level.INFO, "Start clusteing");

        weka.clusterers.HierarchicalClusterer clusterer = new HierarchicalClusterer();
        clusterer.setOptions(new String[] { "-L", "COMPLETE" });
        clusterer.setDebug(true);
        clusterer.setNumClusters(numOfClusters);
        clusterer.setDistanceFunction(df);
        clusterer.setDistanceIsBranchLength(true);
        clusterer.setPrintNewick(false);

        weka.clusterers.FilteredClusterer fc = new weka.clusterers.FilteredClusterer();
        String[] options = new String[2];
        options[0] = "-R"; // "range"
        options[1] = "1"; // we want to ignore the attribute that is in the position '1'
        Remove remove = new Remove(); // new instance of filter
        remove.setOptions(options); // set options

        fc.setFilter(remove); //add filter to remove attributes
        fc.setClusterer(clusterer); //bind FilteredClusterer to original clusterer
        fc.buildClusterer(data);

        //             // Print normal
        //        clusterer.setPrintNewick(false);
        //        System.out.println(clusterer.graph());
        //        // Print Newick
        //        clusterer.setPrintNewick(true);
        //        System.out.println(clusterer.graph());
        //
        //        // Let's try to show this clustered data!
        //        JFrame mainFrame = new JFrame("Weka Test");
        //        mainFrame.setSize(600, 400);
        //        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //        Container content = mainFrame.getContentPane();
        //        content.setLayout(new GridLayout(1, 1));
        //
        //        HierarchyVisualizer visualizer = new HierarchyVisualizer(clusterer.graph());
        //        content.add(visualizer);
        //
        //        mainFrame.setVisible(true);
        return ClusterUtils.bulidClusters(clusterer, data, inDir);

    } catch (Exception ex) {
        Logger.getLogger(Hierarchical.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:nl.uva.sne.commons.ClusterUtils.java

public static Map<String, String> bulidClusters(Clusterer clusterer, Instances data, String inDir)
        throws Exception {

    FilteredClusterer fc = new FilteredClusterer();
    String[] options = new String[2];
    options[0] = "-R"; // "range"
    options[1] = "1"; // we want to ignore the attribute that is in the position '1'
    Remove remove = new Remove(); // new instance of filter
    remove.setOptions(options); // set options

    fc.setFilter(remove); //add filter to remove attributes
    fc.setClusterer(clusterer); //bind FilteredClusterer to original clusterer
    fc.buildClusterer(data);//  ww  w.  j  a v  a2 s .com

    Map<String, String> clusters = new HashMap<>();
    for (int i = 0; i < data.numInstances(); i++) {
        Instance inst = data.instance(i);
        int theClass = fc.clusterInstance(inst);
        String s = data.attribute(0).value(i);
        clusters.put(inDir + File.separator + s, String.valueOf(theClass));
        System.err.println(s + " is in cluster " + theClass);
    }
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(fc); // the cluster to evaluate
    eval.evaluateClusterer(data); // data to evaluate the clusterer on
    //        double ll = eval.getLogLikelihood();
    //        Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "LogLikelihood :{0}", ll);
    //
    //        if (clusterer instanceof SimpleKMeans) {
    //            double sqrErr = ((SimpleKMeans) clusterer).getSquaredError();
    //            Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "Squared Error:{0}", sqrErr);
    //        }

    Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "# of clusters: {0}", eval.getNumClusters());
    Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "clusterResults: {0}",
            eval.clusterResultsToString());

    return clusters;
}

From source file:org.opentox.jaqpot3.qsar.util.WekaInstancesProcess.java

License:Open Source License

public static Instances getFilteredInstances(Instances inputData, List<Feature> independentFeatures,
        Feature dependentFeature) throws JaqpotException {
    try {//from ww w .j  a  va  2s  .c o m
        List<Integer> indexArray = getDescriptorsIndexArray(inputData, independentFeatures, dependentFeature);
        //apply filter for deleting the attributes other than these descriptors 
        int[] intArray = ArrayUtils.toPrimitive(indexArray.toArray(new Integer[indexArray.size()]));
        int m = 1;
        Remove rm = new Remove();
        rm.setOptions(new String[] { "-V" });
        rm.setAttributeIndicesArray(intArray);
        rm.setInputFormat(inputData);
        Instances filteredData = Filter.useFilter(inputData, rm);

        return filteredData;

    } catch (Exception ex) {
        throw new JaqpotException(ex);
    }
}