Example usage for weka.core Instance setValue

List of usage examples for weka.core Instance setValue

Introduction

In this page you can find the example usage for weka.core Instance setValue.

Prototype

public void setValue(Attribute att, String value);

Source Link

Document

Sets a value of an nominal or string attribute to the given value.

Usage

From source file:adams.opt.optimise.genetic.fitnessfunctions.AttributeSelection.java

License:Open Source License

public double evaluate(OptData opd) {
    init();/*from   www .j  a  v a2 s .  c  o  m*/
    int cnt = 0;
    int[] weights = getWeights(opd);
    Instances newInstances = new Instances(getInstances());
    for (int i = 0; i < getInstances().numInstances(); i++) {
        Instance in = newInstances.instance(i);
        cnt = 0;
        for (int a = 0; a < getInstances().numAttributes(); a++) {
            if (a == getInstances().classIndex())
                continue;
            if (weights[cnt++] == 0) {
                in.setValue(a, 0);
            } else {
                in.setValue(a, in.value(a));
            }
        }
    }
    Classifier newClassifier = null;

    try {
        newClassifier = (Classifier) OptionUtils.shallowCopy(getClassifier());
        // evaluate classifier on data
        Evaluation evaluation = new Evaluation(newInstances);
        evaluation.crossValidateModel(newClassifier, newInstances, getFolds(),
                new Random(getCrossValidationSeed()));

        // obtain measure
        double measure = 0;
        if (getMeasure() == Measure.ACC)
            measure = evaluation.pctCorrect();
        else if (getMeasure() == Measure.CC)
            measure = evaluation.correlationCoefficient();
        else if (getMeasure() == Measure.MAE)
            measure = evaluation.meanAbsoluteError();
        else if (getMeasure() == Measure.RAE)
            measure = evaluation.relativeAbsoluteError();
        else if (getMeasure() == Measure.RMSE)
            measure = evaluation.rootMeanSquaredError();
        else if (getMeasure() == Measure.RRSE)
            measure = evaluation.rootRelativeSquaredError();
        else
            throw new IllegalStateException("Unhandled measure '" + getMeasure() + "'!");
        measure = getMeasure().adjust(measure);

        return (measure);
        // process fitness

    } catch (Exception e) {
        getLogger().log(Level.SEVERE, "Error evaluating", e);
    }

    return 0;
}

From source file:adams.opt.optimise.genetic.fitnessfunctions.AttributeSelection.java

License:Open Source License

/**
 * Callback for best measure so far//from  w w w. j  ava  2 s .  c o m
 */
@Override
public void newBest(double val, OptData opd) {
    int cnt = 0;
    int[] weights = getWeights(opd);
    Instances newInstances = new Instances(getInstances());
    for (int i = 0; i < getInstances().numInstances(); i++) {
        Instance in = newInstances.instance(i);
        cnt = 0;
        for (int a = 0; a < getInstances().numAttributes(); a++) {
            if (a == getInstances().classIndex())
                continue;
            if (weights[cnt++] == 0) {
                in.setValue(a, 0);
            } else {
                in.setValue(a, in.value(a));
            }
        }
    }
    try {
        File file = new File(getOutputDirectory().getAbsolutePath() + File.separator
                + Double.toString(getMeasure().adjust(val)) + ".arff");
        file.createNewFile();
        Writer writer = new BufferedWriter(new FileWriter(file));
        Instances header = new Instances(newInstances, 0);

        // remove filter setup
        Remove remove = new Remove();
        remove.setAttributeIndices(getRemoveAsString(weights));
        remove.setInvertSelection(true);

        header.setRelationName(OptionUtils.getCommandLine(remove));

        writer.write(header.toString());
        writer.write("\n");
        for (int i = 0; i < newInstances.numInstances(); i++) {
            writer.write(newInstances.instance(i).toString());
            writer.write("\n");
        }
        writer.flush();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:anndl.Anndl.java

private void ClassifyInstance(String fileinput) throws Exception {
    System.out.println("Membaca File Model ..");
    Classifier cls = (Classifier) weka.core.SerializationHelper.read(fileinput);

    System.out.println("Masukan Data yang ingin diklasifikasikan, pisahkan dengan spasi :");
    String testset = scanner.nextLine();
    testset = "1 1 0";
    String[] exploded = testset.split(" ");

    Instance newinstance = new Instance(exploded.length);
    for (int i = 0; i < exploded.length; i++) {
        String exploded1 = exploded[i];
        System.out.println(exploded[i] + ";");

        newinstance.setValue(i, Integer.valueOf(exploded[i]));
    }/*from   w  ww . jav  a 2  s  .c o m*/

    System.out.println("Melakukan klasifikasi ... ");
    double result = cls.classifyInstance(newinstance);
    System.out.println("Data ini:");
    System.out.println(testset);
    System.out.println("memiliki label :");
    System.out.println(newinstance.value(newinstance.classIndex()));
}

From source file:ant.Game.java

private char keyFromWeka(int classifier) {

    char key = W;

    Instance i = new Instance(m_ant.getNumAttributes() + 1);
    i.setDataset(m_data);//w  ww.  j av  a 2 s .c om
    for (int j = 0; j < i.numAttributes() - 1; ++j) {
        i.setValue(new Attribute(Integer.toString(j), j), see(j));
    }

    i.setClassValue(1);
    double p = -1;
    try {
        p = m_wrapper10x1.classifyInstance(i);
    } catch (Exception ex) {
        Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
    }

    switch ((int) p) {
    case 0:
        key = W;
        break;
    case 1:
        key = A;
        break;
    case 2:
        key = S;
        break;
    case 3:
        key = D;
        break;
    default:
        System.err.println("Unexpected classifier output!");
        break;
    }

    return key;
}

From source file:at.ac.tuwien.ifs.myluceneanalyzers.fa.algorithm.PersianDictionaryCountCompoundWord.java

/**
 * Same as {@link DictionaryCompoundWordTokenFilter#decompose()} except if it found a match, it moves the pointer to the end of it
 * @throws Exception //from ww w  .  j  a  v  a  2 s .c o m
 */
public List<String> decompose(String token) throws Exception {
    List<String> result = new ArrayList<String>();
    List<String> bestTokens = new ArrayList<String>();

    if (!dictionary.contains(token)) {
        if (!dictionary.contains(stem(token))) {
            final int len = token.length();
            int i = 0;
            List<ArrayList<String>> listMatchTokens = new ArrayList<ArrayList<String>>();
            for (int j = this.minSubwordSize; j <= len - this.minSubwordSize; ++j) {
                if (i + j > len) {
                    break;
                }
                if (dictionary.contains(token.substring(i, j))
                        && dictionary.contains(token.substring(j, len))) {
                    String matchToken1 = token.substring(i, j);
                    String matchToken2 = token.substring(j, len);
                    ArrayList<String> tokens = new ArrayList<String>();
                    tokens.add(matchToken1);
                    tokens.add(matchToken2);
                    listMatchTokens.add(tokens);
                }
            }

            Double maxcountavg = mapWordCount.get(token.toString());
            for (ArrayList<String> matchTokens : listMatchTokens) {
                List<Double> wordCounts = new ArrayList<Double>();
                for (String matchToken : matchTokens) {
                    Double wordCount = mapWordCount.get(matchToken);
                    if (wordCount == null)
                        wordCount = 0.0;

                    wordCounts.add(wordCount);
                }

                //mean
                double sum = 0.0;
                for (Double a : wordCounts)
                    sum += a;
                Double countmean = (sum / matchTokens.size());
                //var
                double temp = 0;
                for (Double a : wordCounts)
                    temp += (countmean - a) * (countmean - a);
                Double countvar = Math.sqrt((temp / matchTokens.size()));
                //harmmean
                Double countharmmean = 0.0;
                if ((wordCounts.get(0) != 0) && (wordCounts.get(1) != 0))
                    countharmmean = ((2 * wordCounts.get(0) * wordCounts.get(1))
                            / (wordCounts.get(0) + wordCounts.get(1)));

                //check with the max
                if (maxcountavg <= countmean) {
                    bestTokens.clear();
                    for (String matchToken : matchTokens) {
                        bestTokens.add(matchToken);
                    }
                    bestTokens.add(String.valueOf((int) Math.floor(countmean)));
                    bestTokens.add(String.valueOf((int) Math.floor(countvar)));
                    bestTokens.add(String.valueOf((int) Math.floor(countharmmean)));
                    maxcountavg = countmean;
                }
            }
        }
    }
    if (bestTokens.size() > 0) {
        //check the classifier
        //Evaluation eTest = new Evaluation(instances);
        //eTest.evaluateModel(cls, instances);
        //String strSummary = eTest.toSummaryString();
        //System.out.println(strSummary);

        Instance instance = new Instance(2);
        int harmmean = Integer.valueOf(bestTokens.get(bestTokens.size() - 1));
        instance.setValue(attributeHermmean, harmmean);
        instances.add(instance);

        double value = cls.classifyInstance(instances.instance(0));
        String prediction = instances.classAttribute().value((int) value);

        if (prediction.equals("1")) {
            result = bestTokens;
        }
    }

    return result;
}

From source file:at.aictopic1.sentimentanalysis.machinelearning.impl.TwitterClassifer.java

public Integer classify(Tweet[] tweets) {
    // TEST//from w  w w .  ja  v  a  2s  . c  om

    // Generate two tweet examples
    Tweet exOne = new Tweet("This is good and fantastic");
    exOne.setPreprocessedText("This is good and fantastic");
    Tweet exTwo = new Tweet("Horribly, terribly bad and more");
    exTwo.setPreprocessedText("Horribly, terribly bad and more");
    Tweet exThree = new Tweet(
            "I want to update lj and read my friends list, but I\\'m groggy and sick and blargh.");
    exThree.setPreprocessedText(
            "I want to update lj and read my friends list, but I\\'m groggy and sick and blargh.");
    Tweet exFour = new Tweet("bad hate worst sick");
    exFour.setPreprocessedText("bad hate worst sick");
    tweets = new Tweet[] { exOne, exTwo, exThree, exFour };
    // TEST

    // Load model
    //        loadModel();
    // Convert Tweet to Instance type 
    // Get String Data
    // Create attributes for the Instances set
    Attribute twitter_id = new Attribute("twitter_id");
    //        Attribute body = new Attribute("body");

    FastVector classVal = new FastVector(2);
    classVal.addElement("pos");
    classVal.addElement("neg");

    Attribute class_attr = new Attribute("class_attr", classVal);

    // Add them to a list
    FastVector attrVector = new FastVector(3);
    //        attrVector.addElement(twitter_id);
    //        attrVector.addElement(new Attribute("body", (FastVector) null));
    //        attrVector.addElement(class_attr);

    // Get the number of tweets and then create predictSet
    int numTweets = tweets.length;
    Enumeration structAttrs = dataStructure.enumerateAttributes();

    //        ArrayList<Attribute> attrList = new ArrayList<Attribute>(dataStructure.numAttributes());
    while (structAttrs.hasMoreElements()) {
        attrVector.addElement((Attribute) structAttrs.nextElement());
    }
    Instances predictSet = new Instances("predictInstances", attrVector, numTweets);
    //        Instances predictSet = new Instances(dataStructure);
    predictSet.setClassIndex(2);

    // init prediction
    double prediction = -1;

    System.out.println("PredictSet matches source structure: " + predictSet.equalHeaders(dataStructure));

    System.out.println("PredSet struct: " + predictSet.attribute(0));
    System.out.println("PredSet struct: " + predictSet.attribute(1));
    System.out.println("PredSet struct: " + predictSet.attribute(2));
    // Array to return predictions 
    //double[] tweetsClassified = new double[2][numTweets];
    //List<Integer, Double> tweetsClass = new ArrayList<Integer, Double>(numTweets);
    for (int i = 0; i < numTweets; i++) {
        String content = (String) tweets[i].getPreprocessedText();

        System.out.println("Tweet content: " + content);

        //            attrList
        Instance tweetInstance = new Instance(predictSet.numAttributes());

        tweetInstance.setDataset(predictSet);

        tweetInstance.setValue(predictSet.attribute(0), i);
        tweetInstance.setValue(predictSet.attribute(1), content);
        tweetInstance.setClassMissing();

        predictSet.add(tweetInstance);

        try {
            // Apply string filter
            StringToWordVector filter = new StringToWordVector();

            filter.setInputFormat(predictSet);
            Instances filteredPredictSet = Filter.useFilter(predictSet, filter);

            // Apply model
            prediction = trainedModel.classifyInstance(filteredPredictSet.instance(i));
            filteredPredictSet.instance(i).setClassValue(prediction);
            System.out.println("Classification: " + filteredPredictSet.instance(i).toString());
            System.out.println("Prediction: " + prediction);

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

    return 0;
}

From source file:at.tuflowgraphy.semanticapps.semdroid.DalvikBaseAnalyzer.java

License:Apache License

protected void addLastXWekaInstances(Instances instances, int numberOfInstances, List<Object> linkedObjects) {
    // TODO: use ResultEntry instead of List<Object>
    if (numberOfInstances <= 0) {
        return;//ww  w.j a  va 2s.  c om
    }
    List<DActivationPatternPackage> dActivationPatternPackages = mAnalysisChain.getFinalLayers().get(0)
            .getResultAnalysisPackage().getActivationPatternPackages();
    int counter = 0;
    for (DActivationPatternPackage dActivationPatternPackage : dActivationPatternPackages) {
        DActivationPattern activationPatternTemp = dActivationPatternPackage.getActivationPatterns().get(0);
        if (counter > 0) {
            throw new RuntimeException("More than one DActivationPatternPackage found!");
        }
        counter++;

        for (int i = numberOfInstances - 1; i >= 0; i--) {
            DActivationPattern activationPattern = dActivationPatternPackage.getActivationPatterns()
                    .get(dActivationPatternPackage.getActivationPatterns().size() - 1 - i);
            Instance instance = new Instance(activationPatternTemp.getRawPattern().length + 1);

            for (int j = 0; j < activationPattern.getRawPattern().length; j++) {
                instance.setValue(j, activationPattern.getRawPattern()[j]);
            }
            instances.add(instance);
            instance.setDataset(instances);

            if (linkedObjects != null) {
                DBasicMetaData d = activationPattern.getMetaData();
                if (d instanceof DObjectLinkMetaData) {
                    Object o = ((DObjectLinkMetaData) d).getLinkedObject();
                    linkedObjects.add(o);
                } else {
                    throw new IllegalArgumentException(
                            "Wrong metadata type attached! Must be DObjectLinkMetaData!");
                }
            }
        }

    }
}

From source file:at.tuflowgraphy.semanticapps.semdroid.DalvikBaseAnalyzer.java

License:Apache License

public Instances getWekaInstances() {
    Instances instances = null;/*from  w  w  w. j a  v a2  s .c  o  m*/
    List<DActivationPatternPackage> dActivationPatternPackages = mAnalysisChain.getFinalLayers().get(0)
            .getResultAnalysisPackage().getActivationPatternPackages();
    int counter = 0;
    for (DActivationPatternPackage dActivationPatternPackage : dActivationPatternPackages) {
        if (counter > 0) {
            throw new RuntimeException("More than one DActivationPatternPackage found!");
        }
        counter++;
        DActivationPattern activationPatternTemp = dActivationPatternPackage.getActivationPatterns().get(0);
        FastVector fvWekaAttributes = new FastVector(activationPatternTemp.getRawPattern().length);
        for (int j = 0; j < activationPatternTemp.getRawPattern().length; j++) {
            Attribute attribute = new Attribute(j + "");
            fvWekaAttributes.addElement(attribute);
        }

        Set<String> labelSet = getLabelSet(dActivationPatternPackage);

        FastVector classValues = new FastVector(labelSet.size());
        for (String label : labelSet) {
            classValues.addElement(label);
        }

        Attribute classAttribute = new Attribute("Class", classValues);
        fvWekaAttributes.addElement(classAttribute);

        instances = new Instances(mAnalysisConfig.getApplicationAnalysisName(), fvWekaAttributes,
                dActivationPatternPackage.getActivationPatterns().size());
        instances.setClassIndex(instances.numAttributes() - 1);

        for (int i = 0; i < dActivationPatternPackage.getActivationPatterns().size(); i++) {
            DActivationPattern activationPattern = dActivationPatternPackage.getActivationPatterns().get(i);
            Instance instance = new Instance(fvWekaAttributes.size());

            for (int j = 0; j < activationPattern.getRawPattern().length; j++) {
                instance.setValue((Attribute) fvWekaAttributes.elementAt(j),
                        activationPattern.getRawPattern()[j]);
            }

            instance.setDataset(instances);

            DSimpleStringMetaData metadata = (DSimpleStringMetaData) activationPattern.getMetaData();
            List<String> keys = metadata.getMetaDataKeys();
            for (int k = 0; k < keys.size(); k++) {
                if (keys.get(k).equals(DalvikInputPlugin.TAG_LABEL)) {
                    String label = metadata.getMetaDataEntries().get(k);
                    instance.setClassValue(label);
                    break;
                }
            }

            instances.add(instance);
        }

    }
    return instances;
}

From source file:at.tuflowgraphy.semanticapps.semdroid.utils.ArffHelper.java

License:Apache License

public Instances getWekaInstances(AnalysisChain analysisChain, String name) {
    Instances instances = null;//from w  w w .  j av  a2 s  .  c  om
    List<DActivationPatternPackage> dActivationPatternPackages = analysisChain.getFinalLayers().get(0)
            .getResultAnalysisPackage().getActivationPatternPackages();
    int counter = 0;
    for (DActivationPatternPackage dActivationPatternPackage : dActivationPatternPackages) {
        if (counter > 0) {
            //                String resultFileName = arffFile.getName();
            //                String newName = resultFileName.split("_")[0];
            //                int index = resultFileName.indexOf("_");
            //                newName += "-MISSING-" + counter + "-"
            //                        + resultFileName.substring(index);
            //                arffFileToWriteTo = new File(arffFile.getParentFile(), newName);
            System.err.println("ERROR: Multiple activation pattern packages found! Should not happen...");
        }
        counter++;
        DActivationPattern activationPatternTemp = dActivationPatternPackage.getActivationPatterns().get(0);
        FastVector fvWekaAttributes = new FastVector(activationPatternTemp.getRawPattern().length);
        for (int j = 0; j < activationPatternTemp.getRawPattern().length; j++) {
            Attribute attribute = new Attribute(j + "");
            fvWekaAttributes.addElement(attribute);
        }

        Set<String> labelSet = getLabelSet(dActivationPatternPackage);

        FastVector classValues = new FastVector(labelSet.size());
        for (String label : labelSet) {
            classValues.addElement(label);
        }

        Attribute classAttribute = new Attribute("Class", classValues);
        fvWekaAttributes.addElement(classAttribute);

        instances = new Instances(name, fvWekaAttributes,
                dActivationPatternPackage.getActivationPatterns().size());
        instances.setClassIndex(instances.numAttributes() - 1);

        for (int i = 0; i < dActivationPatternPackage.getActivationPatterns().size(); i++) {
            DActivationPattern activationPattern = dActivationPatternPackage.getActivationPatterns().get(i);
            Instance instance = new Instance(fvWekaAttributes.size());

            for (int j = 0; j < activationPattern.getRawPattern().length; j++) {
                instance.setValue((Attribute) fvWekaAttributes.elementAt(j),
                        activationPattern.getRawPattern()[j]);
            }

            instance.setDataset(instances);

            DSimpleStringMetaData metadata = (DSimpleStringMetaData) activationPattern.getMetaData();
            List<String> keys = metadata.getMetaDataKeys();
            for (int k = 0; k < keys.size(); k++) {
                if (keys.get(k).equals(DalvikInputPlugin.TAG_LABEL)) {
                    String label = metadata.getMetaDataEntries().get(k);

                    // TODO: dynamically add new labels to instances so that getLabelSet for-loop is not required

                    // System.out.println(label);
                    // if(!labelSet.contains(label)) {
                    // labelSet.add(label);
                    // // classValues.addElement(label);
                    // classAttribute.addStringValue(label);
                    // instances.attribute(instances.classIndex()).addValue(label);
                    // System.out.println("ADDED " + label);
                    // }
                    instance.setClassValue(label);
                    // TODO: only first class value used
                    break;
                }
            }
            instances.add(instance);
        }

    }
    return instances;
}

From source file:boa.aggregators.DecisionTreeAggregator.java

License:Apache License

/** {@inheritDoc} */
@Override//from w  ww. ja  v a2 s. c o m
public void finish() throws IOException, InterruptedException {
    int NumOfAttributes = this.getVectorSize();
    List<Attribute> attributes = new ArrayList<Attribute>();
    FastVector fvAttributes = new FastVector(NumOfAttributes);

    for (int i = 0; i < NumOfAttributes; i++) {
        attributes.add(new Attribute("Attribute" + i));
        fvAttributes.addElement(attributes.get(i));
    }

    Instances trainingSet = new Instances("DecisionTree", fvAttributes, 1);
    trainingSet.setClassIndex(NumOfAttributes - 1);

    for (List<Double> vector : this.vectors.values()) {
        Instance instance = new Instance(NumOfAttributes);
        for (int i = 0; i < vector.size(); i++) {
            instance.setValue((Attribute) fvAttributes.elementAt(i), vector.get(i));
        }
        trainingSet.add(instance);
    }

    try {
        this.model = new J48();
        this.model.setOptions(options);
        this.model.buildClassifier(trainingSet);
    } catch (Exception ex) {
    }

    this.saveModel(this.model);
}