Example usage for weka.core Instance toString

List of usage examples for weka.core Instance toString

Introduction

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

Prototype

public String toString(Attribute att);

Source Link

Document

Returns the description of one value of the instance as a string.

Usage

From source file:bme.mace.logicdomain.Evaluation.java

License:Open Source License

/**
 * store the prediction made by the classifier as a string
 * /*from   www.j  av a  2 s. c  o m*/
 * @param classifier the classifier to use
 * @param inst the instance to generate text from
 * @param instNum the index in the dataset
 * @param attributesToOutput the indices of the attributes to output
 * @param printDistribution prints the complete distribution for nominal
 *          classes, not just the predicted value
 * @return the prediction as a String
 * @throws Exception if something goes wrong
 * @see #printClassifications(Classifier, Instances, String, int, Range,
 *      boolean)
 */
protected static String predictionText(Classifier classifier, Instance inst, int instNum,
        Range attributesToOutput, boolean printDistribution)

        throws Exception {

    StringBuffer result = new StringBuffer();
    int width = 10;
    int prec = 3;

    Instance withMissing = (Instance) inst.copy();
    withMissing.setDataset(inst.dataset());
    withMissing.setMissing(withMissing.classIndex());
    double predValue = classifier.classifyInstance(withMissing);

    // index
    result.append(Utils.padLeft("" + (instNum + 1), 6));

    if (inst.dataset().classAttribute().isNumeric()) {
        // actual
        if (inst.classIsMissing()) {
            result.append(" " + Utils.padLeft("?", width));
        } else {
            result.append(" " + Utils.doubleToString(inst.classValue(), width, prec));
        }
        // predicted
        if (Instance.isMissingValue(predValue)) {
            result.append(" " + Utils.padLeft("?", width));
        } else {
            result.append(" " + Utils.doubleToString(predValue, width, prec));
        }
        // error
        if (Instance.isMissingValue(predValue) || inst.classIsMissing()) {
            result.append(" " + Utils.padLeft("?", width));
        } else {
            result.append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec));
        }
    } else {
        // actual
        result.append(" "
                + Utils.padLeft(((int) inst.classValue() + 1) + ":" + inst.toString(inst.classIndex()), width));
        // predicted
        if (Instance.isMissingValue(predValue)) {
            result.append(" " + Utils.padLeft("?", width));
        } else {
            result.append(" " + Utils.padLeft(
                    ((int) predValue + 1) + ":" + inst.dataset().classAttribute().value((int) predValue),
                    width));
        }
        // error?
        if (!Instance.isMissingValue(predValue) && !inst.classIsMissing()
                && ((int) predValue + 1 != (int) inst.classValue() + 1)) {
            result.append(" " + "  +  ");
        } else {
            result.append(" " + "     ");
        }
        // prediction/distribution
        if (printDistribution) {
            if (Instance.isMissingValue(predValue)) {
                result.append(" " + "?");
            } else {
                result.append(" ");
                double[] dist = classifier.distributionForInstance(withMissing);
                for (int n = 0; n < dist.length; n++) {
                    if (n > 0) {
                        result.append(",");
                    }
                    if (n == (int) predValue) {
                        result.append("*");
                    }
                    result.append(Utils.doubleToString(dist[n], prec));
                }
            }
        } else {
            if (Instance.isMissingValue(predValue)) {
                result.append(" " + "?");
            } else {
                result.append(" " + Utils.doubleToString(
                        classifier.distributionForInstance(withMissing)[(int) predValue], prec));
            }
        }
    }

    // attributes
    result.append(" " + attributeValuesString(withMissing, attributesToOutput) + "\n");

    return result.toString();
}

From source file:bme.mace.logicdomain.Evaluation.java

License:Open Source License

/**
 * Builds a string listing the attribute values in a specified range of
 * indices, separated by commas and enclosed in brackets.
 * //  w  w w .j a va 2  s.c  o m
 * @param instance the instance to print the values from
 * @param attRange the range of the attributes to list
 * @return a string listing values of the attributes in the range
 */
protected static String attributeValuesString(Instance instance, Range attRange) {
    StringBuffer text = new StringBuffer();
    if (attRange != null) {
        boolean firstOutput = true;
        attRange.setUpper(instance.numAttributes() - 1);
        for (int i = 0; i < instance.numAttributes(); i++) {
            if (attRange.isInRange(i) && i != instance.classIndex()) {
                if (firstOutput) {
                    text.append("(");
                } else {
                    text.append(",");
                }
                text.append(instance.toString(i));
                firstOutput = false;
            }
        }
        if (!firstOutput) {
            text.append(")");
        }
    }
    return text.toString();
}

From source file:com.hack23.cia.service.impl.action.user.wordcount.WordCounterImpl.java

License:Apache License

@Override
public Map<String, Integer> calculateWordCount(final DocumentContentData documentContentData,
        final int maxResult) {

    final String html = documentContentData.getContent();

    final Attribute input = new Attribute("html", (ArrayList<String>) null);

    final ArrayList<Attribute> inputVec = new ArrayList<>();
    inputVec.add(input);//  w  w  w.j  a v a2  s  . c  o m

    final Instances htmlInst = new Instances("html", inputVec, 1);

    htmlInst.add(new DenseInstance(1));
    htmlInst.instance(0).setValue(0, html);

    final StopwordsHandler StopwordsHandler = new StopwordsHandler() {

        @Override
        public boolean isStopword(final String word) {

            return word.length() < 5;
        }
    };

    final NGramTokenizer tokenizer = new NGramTokenizer();
    tokenizer.setNGramMinSize(1);
    tokenizer.setNGramMaxSize(1);
    tokenizer.setDelimiters(" \r\n\t.,;:'\"()?!'");

    final StringToWordVector filter = new StringToWordVector();
    filter.setTokenizer(tokenizer);
    filter.setStopwordsHandler(StopwordsHandler);
    filter.setLowerCaseTokens(true);
    filter.setOutputWordCounts(true);
    filter.setWordsToKeep(maxResult);

    final Map<String, Integer> result = new HashMap<>();

    try {
        filter.setInputFormat(htmlInst);
        final Instances dataFiltered = Filter.useFilter(htmlInst, filter);

        final Instance last = dataFiltered.lastInstance();

        final int numAttributes = last.numAttributes();

        for (int i = 0; i < numAttributes; i++) {
            result.put(last.attribute(i).name(), Integer.valueOf(last.toString(i)));
        }
    } catch (final Exception e) {
        LOGGER.warn("Problem calculating wordcount for : {} , exception:{}", documentContentData.getId(), e);
    }

    return result;
}

From source file:core.ClusterEvaluationEX.java

License:Open Source License

/**
 * Builds a string listing the attribute values in a specified range of indices,
 * separated by commas and enclosed in brackets.
 *
 * @param instance the instance to print the values from
 * @param attRange the range of the attributes to list
 * @return a string listing values of the attributes in the range
 *//*ww  w  .j a v  a  2 s  .  com*/
private static String attributeValuesString(Instance instance, Range attRange) {
    StringBuffer text = new StringBuffer();
    if (attRange != null) {
        boolean firstOutput = true;
        attRange.setUpper(instance.numAttributes() - 1);
        for (int i = 0; i < instance.numAttributes(); i++)
            if (attRange.isInRange(i)) {
                if (firstOutput)
                    text.append("(");
                else
                    text.append(",");
                text.append(instance.toString(i));
                firstOutput = false;
            }
        if (!firstOutput)
            text.append(")");
    }
    return text.toString();
}

From source file:cotraining.copy.Evaluation_D.java

License:Open Source License

/**
 * store the prediction made by the classifier as a string
 * //from www .j a  va  2 s. co  m
 * @param classifier      the classifier to use
 * @param inst      the instance to generate text from
 * @param instNum      the index in the dataset
 * @param attributesToOutput   the indices of the attributes to output
 * @param printDistribution   prints the complete distribution for nominal 
 *             classes, not just the predicted value
 * @return                    the prediction as a String
 * @throws Exception      if something goes wrong
 * @see         #printClassifications(Classifier, Instances, String, int, Range, boolean)
 */
protected static String predictionText(Classifier classifier, Instance inst, int instNum,
        Range attributesToOutput, boolean printDistribution)

        throws Exception {

    StringBuffer result = new StringBuffer();
    int width = 10;
    int prec = 3;

    Instance withMissing = (Instance) inst.copy();
    withMissing.setDataset(inst.dataset());
    withMissing.setMissing(withMissing.classIndex());
    double predValue = classifier.classifyInstance(withMissing);

    // index
    result.append(Utils.padLeft("" + (instNum + 1), 6));

    if (inst.dataset().classAttribute().isNumeric()) {
        // actual
        if (inst.classIsMissing())
            result.append(" " + Utils.padLeft("?", width));
        else
            result.append(" " + Utils.doubleToString(inst.classValue(), width, prec));
        // predicted
        if (Instance.isMissingValue(predValue))
            result.append(" " + Utils.padLeft("?", width));
        else
            result.append(" " + Utils.doubleToString(predValue, width, prec));
        // error
        if (Instance.isMissingValue(predValue) || inst.classIsMissing())
            result.append(" " + Utils.padLeft("?", width));
        else
            result.append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec));
    } else {
        // actual
        result.append(" "
                + Utils.padLeft(((int) inst.classValue() + 1) + ":" + inst.toString(inst.classIndex()), width));
        // predicted
        if (Instance.isMissingValue(predValue))
            result.append(" " + Utils.padLeft("?", width));
        else
            result.append(" " + Utils.padLeft(
                    ((int) predValue + 1) + ":" + inst.dataset().classAttribute().value((int) predValue),
                    width));
        // error?
        if (!Instance.isMissingValue(predValue) && !inst.classIsMissing()
                && ((int) predValue + 1 != (int) inst.classValue() + 1))
            result.append(" " + "  +  ");
        else
            result.append(" " + "     ");
        // prediction/distribution
        if (printDistribution) {
            if (Instance.isMissingValue(predValue)) {
                result.append(" " + "?");
            } else {
                result.append(" ");
                double[] dist = classifier.distributionForInstance(withMissing);
                for (int n = 0; n < dist.length; n++) {
                    if (n > 0)
                        result.append(",");
                    if (n == (int) predValue)
                        result.append("*");
                    result.append(Utils.doubleToString(dist[n], prec));
                }
            }
        } else {
            if (Instance.isMissingValue(predValue))
                result.append(" " + "?");
            else
                result.append(" " + Utils.doubleToString(
                        classifier.distributionForInstance(withMissing)[(int) predValue], prec));
        }
    }

    // attributes
    result.append(" " + attributeValuesString(withMissing, attributesToOutput) + "\n");

    return result.toString();
}

From source file:cotraining.copy.Evaluation_D.java

License:Open Source License

/**
 * Builds a string listing the attribute values in a specified range of indices,
 * separated by commas and enclosed in brackets.
 *
 * @param instance the instance to print the values from
 * @param attRange the range of the attributes to list
 * @return a string listing values of the attributes in the range
 *///from  w  w w.  j a  va  2 s.  com
protected static String attributeValuesString(Instance instance, Range attRange) {
    StringBuffer text = new StringBuffer();
    if (attRange != null) {
        boolean firstOutput = true;
        attRange.setUpper(instance.numAttributes() - 1);
        for (int i = 0; i < instance.numAttributes(); i++)
            if (attRange.isInRange(i) && i != instance.classIndex()) {
                if (firstOutput)
                    text.append("(");
                else
                    text.append(",");
                text.append(instance.toString(i));
                firstOutput = false;
            }
        if (!firstOutput)
            text.append(")");
    }
    return text.toString();
}

From source file:couchdb.CouchDBService.java

/**
 * Metoda do zapisywania danych do bazy.
 *
 * @param fileName nazwa pliku z danymi, akceptowalny format to pliki ARFF
 * @param dataBaseName nazwa bazy danych
 *//* w ww. ja va  2 s.  c o  m*/
public void importData(String fileName, String dataBaseName) {
    WekaService ws = new WekaService(fileName);
    ArrayList<String> listOfAttributesNames = ws.getAttributesName();
    ArrayList<Instance> listOfInstances = ws.getInstances();
    Database db = session.getDatabase(dataBaseName);
    for (int i = 0; i < listOfInstances.size(); i++) {
        Instance instance = listOfInstances.get(i);
        Document doc = new Document();
        doc.setId(String.valueOf(i));
        for (int j = 0; j < instance.numAttributes(); j++) {
            doc.put(listOfAttributesNames.get(j), instance.toString(j));
        }
        db.saveDocument(doc);
    }

}

From source file:dataMining.KMeans.java

/**
 * Metoda do sprawdzania czy instancje podane jako parametr s takie same.
 *
 * @param insA piewrsza instanca/*from   w  ww  .  j  a v a 2  s .  c  om*/
 * @param insB druga instancja
 * @return True, jeli s takie same, false w przeciwnym wypadku.
 */
private boolean equals(Instance insA, Instance insB) {
    if (insA.numAttributes() != insB.numAttributes()) {
        throw new NullPointerException("Rna liczba atrybutw");
    } else {
        int countTheSame = 0;
        for (int i = 0; i < insA.numAttributes(); i++) {
            double a = 0;
            double b = 0;
            try {
                a = Double.parseDouble(insA.toString(i));
            } catch (NumberFormatException ex) {
                a = 0;
            }
            try {
                b = Double.parseDouble(insB.toString(i));
            } catch (NumberFormatException ex) {
                b = 0;
            }
            if (Math.abs(a - b) == 0) {
                countTheSame++;
            }

        }
        int countAttr = insB.numAttributes();

        if (countTheSame == countAttr) {
            return true;
        } else {
            return false;
        }

    }

}

From source file:dataMining.KMeans.java

/**
 * Metoda do wyznaczania nowych rodkw grup.
 *
 * @return lista zawierajca nowe rodki.// ww w.j a v a2 s. co  m
 */
private ArrayList<Instance> makeNewMeans() {
    ArrayList<Instance> listOfMeans = new ArrayList<>();
    for (Instance i : groups.keySet()) {
        ArrayList<Instance> list = groups.get(i);
        double[] tab = new double[i.numAttributes()];
        for (Instance in : list) {
            for (int j = 0; j < tab.length; j++) {
                double d = 0;
                try {
                    d = Double.parseDouble(in.toString(j));
                } catch (NumberFormatException ex) {
                    d = 0;
                }
                tab[j] = tab[j] + d;
            }
        }
        for (int j = 0; j < tab.length; j++) {
            tab[j] = tab[j] / list.size();
        }
        Instance ins = new Instance(tab.length);
        for (int j = 0; j < tab.length; j++) {
            ins.setValue(j, tab[j]);
        }
        listOfMeans.add(ins);
    }
    return listOfMeans;
}

From source file:dataMining.KMeans.java

/**
 * Metoda do wyznaczania odlegoci pomiedzy obiektami podanymi jako
 * parametr. Odlego liczona metryk Euklidesow.
 *
 * @param a pierwszy obiekt/*from   w  w w . j  a  v a 2 s.  c om*/
 * @param b drugi obiekt
 * @return Odlego pomidzy obiektami.
 */
private double euclid(Instance a, Instance b) {
    float sum = 0;
    float attrA = 0;
    float attrB = 0;
    for (int i = 0; i < a.numAttributes(); i++) {
        try {
            attrA = Float.parseFloat(a.toString(i));
        } catch (NumberFormatException ex) {
            attrA = 0;
        }
        try {
            attrB = Float.parseFloat(b.toString(i));
        } catch (NumberFormatException ex) {
            attrB = 0;
        }

        float d = attrA - attrB;
        sum = sum + d * d;
    }
    return Math.sqrt(sum);
}