Example usage for weka.core Attribute isNominal

List of usage examples for weka.core Attribute isNominal

Introduction

In this page you can find the example usage for weka.core Attribute isNominal.

Prototype


public finalboolean isNominal() 

Source Link

Document

Test if the attribute is nominal.

Usage

From source file:adams.data.instance.InstancePoint.java

License:Open Source License

/**
 * Returns a string representation of the point.
 *
 * @return      the string representation
 */// w ww. j  a v a2 s  . com
@Override
public String toString() {
    String result;
    Attribute att;

    if (getParent() != null) {
        att = ((Instance) getParent()).getDatasetHeader().attribute(getX());
        result = att.name();
        result += "=";
        if (att.isNominal())
            result += att.value(getY().intValue());
        else
            result += getY();
    } else {
        result = getX() + "," + getY();
    }

    return result;
}

From source file:adams.flow.sink.WekaInstanceViewer.java

License:Open Source License

/**
 * Displays the token (the panel and dialog have already been created at
 * this stage)./*w w  w  . j a  v  a2  s .  c  o m*/
 *
 * @param token   the token to display
 */
@Override
protected void display(Token token) {
    InstanceContainerManager manager;
    InstanceContainer cont;
    weka.core.Instance winst;
    weka.core.Attribute att;
    String id;
    adams.data.instance.Instance inst;

    if (token.getPayload() instanceof weka.core.Instance) {
        winst = (weka.core.Instance) token.getPayload();
        inst = new adams.data.instance.Instance();
        inst.set(winst);
        if (!m_ID.isEmpty()) {
            att = winst.dataset().attribute(m_ID);
            if (att != null) {
                if (att.isNominal() || att.isString())
                    id = winst.stringValue(att.index());
                else
                    id = "" + winst.value(att.index());
                inst.setID(id);
            }
        }
    } else {
        inst = (adams.data.instance.Instance) token.getPayload();
        if (inst.hasReport() && inst.getReport().hasValue(m_ID))
            inst.setID("" + inst.getReport().getValue(new Field(m_ID, DataType.UNKNOWN)));
    }

    manager = m_InstancePanel.getContainerManager();
    cont = manager.newContainer(inst);
    manager.startUpdate();
    manager.add(cont);

    m_Updater.update(m_InstancePanel, cont);
}

From source file:adams.flow.transformer.WekaInstancesInfo.java

License:Open Source License

/**
 * Generates attributes statistics./*from w  w  w  .j av a  2s  .  co m*/
 * 
 * @param data   the dataset to use
 * @param index   the 0-based index of the attribute
 */
protected SpreadSheet getAttributeStats(Instances data, int index) {
    SpreadSheet result;
    Attribute att;
    AttributeStats stats;
    Row row;
    int i;

    result = new DefaultSpreadSheet();
    result.setName("Attribute statistics - #" + (index + 1) + " " + data.attribute(index).name());

    // header
    row = result.getHeaderRow();
    row.addCell("S").setContent("Statistic");
    row.addCell("V").setContent("Value");

    // data
    att = data.attribute(index);
    if (att.isNominal()) {
        stats = data.attributeStats(index);
        addStatistic(result, "Total", stats.totalCount);
        addStatistic(result, "Missing", stats.missingCount);
        addStatistic(result, "Unique", stats.uniqueCount);
        addStatistic(result, "Distinct", stats.distinctCount);
        addStatistic(result, "Integer-like", stats.intCount);
        addStatistic(result, "Float-like", stats.realCount);
        for (i = 0; i < stats.nominalCounts.length; i++)
            addStatistic(result, "Label-" + (i + 1) + "-" + att.value(i), stats.nominalCounts[i]);
        for (i = 0; i < stats.nominalWeights.length; i++)
            addStatistic(result, "Weight-" + (i + 1) + "-" + att.value(i), stats.nominalWeights[i]);
    } else if (att.isDate()) {
        if (m_DateFormat == null)
            m_DateFormat = DateUtils.getTimestampFormatter();
        stats = data.attributeStats(index);
        addStatistic(result, "Count", stats.numericStats.count);
        addStatistic(result, "Min", formatDate(stats.numericStats.min));
        addStatistic(result, "Max", formatDate(stats.numericStats.max));
        addStatistic(result, "Mean", formatDate(stats.numericStats.mean));
        addStatistic(result, "StdDev (in days)", stats.numericStats.stdDev / 1000 / 60 / 60 / 24);
    } else if (att.isNumeric()) {
        stats = data.attributeStats(index);
        addStatistic(result, "Count", stats.numericStats.count);
        addStatistic(result, "Min", stats.numericStats.min);
        addStatistic(result, "Max", stats.numericStats.max);
        addStatistic(result, "Mean", stats.numericStats.mean);
        addStatistic(result, "StdDev", stats.numericStats.stdDev);
        addStatistic(result, "Sum", stats.numericStats.sum);
        addStatistic(result, "Sum^2", stats.numericStats.sumSq);
    }

    return result;
}

From source file:adams.flow.transformer.WekaReorderAttributesToReference.java

License:Open Source License

/**
 * Executes the flow item.//from  w ww  .  j  a v a  2  s . c o m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances dataOld;
    Instance instOld;
    Instances dataNew;
    Instance instNew;
    Attribute att;
    int i;
    StringBuilder order;
    List<Add> adds;
    Add add;
    int index;
    StringBuilder labels;
    int n;
    List<Filter> filters;
    Reorder reorder;

    result = null;

    if (m_OnTheFly && (m_Reference == null)) {
        result = setUpReference();
        if (result != null)
            return result;
    }

    dataNew = null;
    instNew = null;

    // get input data
    if (m_InputToken.getPayload() instanceof Instance) {
        instOld = (Instance) m_InputToken.getPayload();
        dataOld = instOld.dataset();
    } else {
        instOld = null;
        dataOld = (Instances) m_InputToken.getPayload();
    }

    // do we need to initialize filter?
    if (m_InitializeOnce || (m_Reorder == null)) {
        // check incoming data
        if (!m_Lenient) {
            for (i = 0; i < m_Reference.numAttributes(); i++) {
                att = m_Reference.attribute(i);
                if (dataOld.attribute(att.name()) == null) {
                    if (result == null)
                        result = "Missing attribute(s) in incoming data: " + att.name();
                    else
                        result += ", " + att.name();
                }
            }
            if (result != null)
                getLogger().severe(result);
        }

        if (result == null) {
            try {
                // determine indices
                order = new StringBuilder();
                adds = new ArrayList<Add>();
                for (i = 0; i < m_Reference.numAttributes(); i++) {
                    att = m_Reference.attribute(i);
                    if (dataOld.attribute(att.name()) == null) {
                        index = dataOld.numAttributes() + adds.size();
                        add = new Add();
                        add.setAttributeIndex("last");
                        add.setAttributeName(att.name());
                        add.setAttributeType(new SelectedTag(att.type(), Add.TAGS_TYPE));
                        if (att.isNominal()) {
                            labels = new StringBuilder();
                            for (n = 0; n < att.numValues(); n++) {
                                if (labels.length() > 0)
                                    labels.append(",");
                                labels.append(att.value(n));
                            }
                            add.setNominalLabels(labels.toString());
                        }
                        adds.add(add);
                    } else {
                        index = dataOld.attribute(att.name()).index();
                    }
                    if (order.length() > 0)
                        order.append(",");
                    order.append((index + 1));
                }

                // build reorder filter
                reorder = new Reorder();
                reorder.setAttributeIndices(order.toString());

                // build multifilter
                filters = new ArrayList<Filter>();
                filters.addAll(adds);
                filters.add(reorder);
                m_Reorder = new MultiFilter();
                m_Reorder.setFilters(filters.toArray(new Filter[filters.size()]));

                // initialize filter
                m_Reorder.setInputFormat(dataOld);
            } catch (Exception e) {
                result = handleException("Failed to initialize reorder filter!", e);
            }
        }
    }

    // reorder data
    if (result == null) {
        try {
            if (instOld != null) {
                m_Reorder.input(instOld);
                m_Reorder.batchFinished();
                instNew = m_Reorder.output();
                if (m_KeepRelationName)
                    instNew.dataset().setRelationName(dataOld.relationName());
            } else {
                dataNew = Filter.useFilter(dataOld, m_Reorder);
                if (m_KeepRelationName)
                    dataNew.setRelationName(dataOld.relationName());
            }
        } catch (Exception e) {
            result = handleException("Failed to reorder data!", e);
            instNew = null;
            dataNew = null;
        }
    }

    if (instNew != null)
        m_OutputToken = new Token(instNew);
    else if (dataNew != null)
        m_OutputToken = new Token(dataNew);

    return result;
}

From source file:boosting.classifiers.DecisionStumpWritable.java

License:Open Source License

/**
 * Returns the value as string out of the given distribution
 * /*from  w w w.  j  a va2  s.  c om*/
 * @param c the attribute to get the value for
 * @param dist the distribution to extract the value
 * @return the value
 */
private String sourceClass(Attribute c, double[] dist) {

    if (c.isNominal()) {
        return Integer.toString(Utils.maxIndex(dist));
    } else {
        return Double.toString(dist[0]);
    }
}

From source file:boosting.classifiers.DecisionStumpWritable.java

License:Open Source License

/**
 * Returns a description of the classifier.
 *
 * @return a description of the classifier as a string.
 */// ww w.j  a v  a2 s .  c  o  m
public String toString() {

    // only ZeroR model?
    if (m_ZeroR != null) {
        StringBuffer buf = new StringBuffer();
        buf.append(this.getClass().getName().replaceAll(".*\\.", "") + "\n");
        buf.append(this.getClass().getName().replaceAll(".*\\.", "").replaceAll(".", "=") + "\n\n");
        buf.append("Warning: No model could be built, hence ZeroR model is used:\n\n");
        buf.append(m_ZeroR.toString());
        return buf.toString();
    }

    if (m_Instances == null) {
        return "Decision Stump: No model built yet.";
    }
    try {
        StringBuffer text = new StringBuffer();

        text.append("Decision Stump\n\n");
        text.append("Classifications\n\n");
        Attribute att = m_Instances.attribute(m_AttIndex);
        if (att.isNominal()) {
            text.append(att.name() + " = " + att.value((int) m_SplitPoint) + " : ");
            text.append(printClass(m_Distribution[0]));
            text.append(att.name() + " != " + att.value((int) m_SplitPoint) + " : ");
            text.append(printClass(m_Distribution[1]));
        } else {
            text.append(att.name() + " <= " + m_SplitPoint + " : ");
            text.append(printClass(m_Distribution[0]));
            text.append(att.name() + " > " + m_SplitPoint + " : ");
            text.append(printClass(m_Distribution[1]));
        }
        text.append(att.name() + " is missing : ");
        text.append(printClass(m_Distribution[2]));

        if (m_Instances.classAttribute().isNominal()) {
            text.append("\nClass distributions\n\n");
            if (att.isNominal()) {
                text.append(att.name() + " = " + att.value((int) m_SplitPoint) + "\n");
                text.append(printDist(m_Distribution[0]));
                text.append(att.name() + " != " + att.value((int) m_SplitPoint) + "\n");
                text.append(printDist(m_Distribution[1]));
            } else {
                text.append(att.name() + " <= " + m_SplitPoint + "\n");
                text.append(printDist(m_Distribution[0]));
                text.append(att.name() + " > " + m_SplitPoint + "\n");
                text.append(printDist(m_Distribution[1]));
            }
            text.append(att.name() + " is missing\n");
            text.append(printDist(m_Distribution[2]));
        }

        return text.toString();
    } catch (Exception e) {
        return "Can't print decision stump classifier!";
    }
}

From source file:br.ufrn.ia.core.clustering.EMIaProject.java

License:Open Source License

public String toString() {
    if (m_displayModelInOldFormat) {
        return toStringOriginal();
    }//from  w  w  w .j a  v  a  2 s .  c  o  m

    if (m_priors == null) {
        return "No clusterer built yet!";
    }
    StringBuffer temp = new StringBuffer();
    temp.append("\nEM\n==\n");
    if (m_initialNumClusters == -1) {
        temp.append("\nNumber of clusters selected by cross validation: " + m_num_clusters + "\n");
    } else {
        temp.append("\nNumber of clusters: " + m_num_clusters + "\n");
    }

    int maxWidth = 0;
    int maxAttWidth = 0;
    boolean containsKernel = false;

    // set up max widths
    // attributes
    for (int i = 0; i < m_num_attribs; i++) {
        Attribute a = m_theInstances.attribute(i);
        if (a.name().length() > maxAttWidth) {
            maxAttWidth = m_theInstances.attribute(i).name().length();
        }
        if (a.isNominal()) {
            // check values
            for (int j = 0; j < a.numValues(); j++) {
                String val = a.value(j) + "  ";
                if (val.length() > maxAttWidth) {
                    maxAttWidth = val.length();
                }
            }
        }
    }

    for (int i = 0; i < m_num_clusters; i++) {
        for (int j = 0; j < m_num_attribs; j++) {
            if (m_theInstances.attribute(j).isNumeric()) {
                // check mean and std. dev. against maxWidth
                double mean = Math.log(Math.abs(m_modelNormal[i][j][0])) / Math.log(10.0);
                double stdD = Math.log(Math.abs(m_modelNormal[i][j][1])) / Math.log(10.0);
                double width = (mean > stdD) ? mean : stdD;
                if (width < 0) {
                    width = 1;
                }
                // decimal + # decimal places + 1
                width += 6.0;
                if ((int) width > maxWidth) {
                    maxWidth = (int) width;
                }
            } else {
                // nominal distributions
                DiscreteEstimator d = (DiscreteEstimator) m_model[i][j];
                for (int k = 0; k < d.getNumSymbols(); k++) {
                    String size = Utils.doubleToString(d.getCount(k), maxWidth, 4).trim();
                    if (size.length() > maxWidth) {
                        maxWidth = size.length();
                    }
                }
                int sum = Utils.doubleToString(d.getSumOfCounts(), maxWidth, 4).trim().length();
                if (sum > maxWidth) {
                    maxWidth = sum;
                }
            }
        }
    }

    if (maxAttWidth < "Attribute".length()) {
        maxAttWidth = "Attribute".length();
    }

    maxAttWidth += 2;

    temp.append("\n\n");
    temp.append(pad("Cluster", " ", (maxAttWidth + maxWidth + 1) - "Cluster".length(), true));

    temp.append("\n");
    temp.append(pad("Attribute", " ", maxAttWidth - "Attribute".length(), false));

    // cluster #'s
    for (int i = 0; i < m_num_clusters; i++) {
        String classL = "" + i;
        temp.append(pad(classL, " ", maxWidth + 1 - classL.length(), true));
    }
    temp.append("\n");

    // cluster priors
    temp.append(pad("", " ", maxAttWidth, true));
    for (int i = 0; i < m_num_clusters; i++) {
        String priorP = Utils.doubleToString(m_priors[i], maxWidth, 2).trim();
        priorP = "(" + priorP + ")";
        temp.append(pad(priorP, " ", maxWidth + 1 - priorP.length(), true));
    }

    temp.append("\n");
    temp.append(pad("", "=", maxAttWidth + (maxWidth * m_num_clusters) + m_num_clusters + 1, true));
    temp.append("\n");

    for (int i = 0; i < m_num_attribs; i++) {
        String attName = m_theInstances.attribute(i).name();
        temp.append(attName + "\n");

        if (m_theInstances.attribute(i).isNumeric()) {
            String meanL = "  mean";
            temp.append(pad(meanL, " ", maxAttWidth + 1 - meanL.length(), false));
            for (int j = 0; j < m_num_clusters; j++) {
                // means
                String mean = Utils.doubleToString(m_modelNormal[j][i][0], maxWidth, 4).trim();
                temp.append(pad(mean, " ", maxWidth + 1 - mean.length(), true));
            }
            temp.append("\n");
            // now do std deviations
            String stdDevL = "  std. dev.";
            temp.append(pad(stdDevL, " ", maxAttWidth + 1 - stdDevL.length(), false));
            for (int j = 0; j < m_num_clusters; j++) {
                String stdDev = Utils.doubleToString(m_modelNormal[j][i][1], maxWidth, 4).trim();
                temp.append(pad(stdDev, " ", maxWidth + 1 - stdDev.length(), true));
            }
            temp.append("\n\n");
        } else {
            Attribute a = m_theInstances.attribute(i);
            for (int j = 0; j < a.numValues(); j++) {
                String val = "  " + a.value(j);
                temp.append(pad(val, " ", maxAttWidth + 1 - val.length(), false));
                for (int k = 0; k < m_num_clusters; k++) {
                    DiscreteEstimator d = (DiscreteEstimator) m_model[k][i];
                    String count = Utils.doubleToString(d.getCount(j), maxWidth, 4).trim();
                    temp.append(pad(count, " ", maxWidth + 1 - count.length(), true));
                }
                temp.append("\n");
            }
            // do the totals
            String total = "  [total]";
            temp.append(pad(total, " ", maxAttWidth + 1 - total.length(), false));
            for (int k = 0; k < m_num_clusters; k++) {
                DiscreteEstimator d = (DiscreteEstimator) m_model[k][i];
                String count = Utils.doubleToString(d.getSumOfCounts(), maxWidth, 4).trim();
                temp.append(pad(count, " ", maxWidth + 1 - count.length(), true));
            }
            temp.append("\n");
        }
    }

    return temp.toString();
}

From source file:cn.edu.xjtu.dbmine.source.NaiveBayes.java

License:Open Source License

/**
 * Returns a description of the classifier.
 *
 * @return a description of the classifier as a string.
 *///  w ww .j ava 2s . com
public String toString() {
    if (m_displayModelInOldFormat) {
        return toStringOriginal();
    }

    StringBuffer temp = new StringBuffer();
    temp.append("Naive Bayes Classifier");
    if (m_Instances == null) {
        temp.append(": No model built yet.");
    } else {

        int maxWidth = 0;
        int maxAttWidth = 0;
        boolean containsKernel = false;

        // set up max widths
        // class values
        for (int i = 0; i < m_Instances.numClasses(); i++) {
            if (m_Instances.classAttribute().value(i).length() > maxWidth) {
                maxWidth = m_Instances.classAttribute().value(i).length();
            }
        }
        // attributes
        for (int i = 0; i < m_Instances.numAttributes(); i++) {
            if (i != m_Instances.classIndex()) {
                Attribute a = m_Instances.attribute(i);
                if (a.name().length() > maxAttWidth) {
                    maxAttWidth = m_Instances.attribute(i).name().length();
                }
                if (a.isNominal()) {
                    // check values
                    for (int j = 0; j < a.numValues(); j++) {
                        String val = a.value(j) + "  ";
                        if (val.length() > maxAttWidth) {
                            maxAttWidth = val.length();
                        }
                    }
                }
            }
        }

        for (int i = 0; i < m_Distributions.length; i++) {
            for (int j = 0; j < m_Instances.numClasses(); j++) {
                if (m_Distributions[i][0] instanceof NormalEstimator) {
                    // check mean/precision dev against maxWidth
                    NormalEstimator n = (NormalEstimator) m_Distributions[i][j];
                    double mean = Math.log(Math.abs(n.getMean())) / Math.log(10.0);
                    double precision = Math.log(Math.abs(n.getPrecision())) / Math.log(10.0);
                    double width = (mean > precision) ? mean : precision;
                    if (width < 0) {
                        width = 1;
                    }
                    // decimal + # decimal places + 1
                    width += 6.0;
                    if ((int) width > maxWidth) {
                        maxWidth = (int) width;
                    }
                } else if (m_Distributions[i][0] instanceof KernelEstimator) {
                    containsKernel = true;
                    KernelEstimator ke = (KernelEstimator) m_Distributions[i][j];
                    int numK = ke.getNumKernels();
                    String temps = "K" + numK + ": mean (weight)";
                    if (maxAttWidth < temps.length()) {
                        maxAttWidth = temps.length();
                    }
                    // check means + weights against maxWidth
                    if (ke.getNumKernels() > 0) {
                        double[] means = ke.getMeans();
                        double[] weights = ke.getWeights();
                        for (int k = 0; k < ke.getNumKernels(); k++) {
                            String m = Utils.doubleToString(means[k], maxWidth, 4).trim();
                            m += " (" + Utils.doubleToString(weights[k], maxWidth, 1).trim() + ")";
                            if (maxWidth < m.length()) {
                                maxWidth = m.length();
                            }
                        }
                    }
                } else if (m_Distributions[i][0] instanceof DiscreteEstimator) {
                    DiscreteEstimator d = (DiscreteEstimator) m_Distributions[i][j];
                    for (int k = 0; k < d.getNumSymbols(); k++) {
                        String size = "" + d.getCount(k);
                        if (size.length() > maxWidth) {
                            maxWidth = size.length();
                        }
                    }
                    int sum = ("" + d.getSumOfCounts()).length();
                    if (sum > maxWidth) {
                        maxWidth = sum;
                    }
                }
            }
        }

        // Check width of class labels
        for (int i = 0; i < m_Instances.numClasses(); i++) {
            String cSize = m_Instances.classAttribute().value(i);
            if (cSize.length() > maxWidth) {
                maxWidth = cSize.length();
            }
        }

        // Check width of class priors
        for (int i = 0; i < m_Instances.numClasses(); i++) {
            String priorP = Utils
                    .doubleToString(((DiscreteEstimator) m_ClassDistribution).getProbability(i), maxWidth, 2)
                    .trim();
            priorP = "(" + priorP + ")";
            if (priorP.length() > maxWidth) {
                maxWidth = priorP.length();
            }
        }

        if (maxAttWidth < "Attribute".length()) {
            maxAttWidth = "Attribute".length();
        }

        if (maxAttWidth < "  weight sum".length()) {
            maxAttWidth = "  weight sum".length();
        }

        if (containsKernel) {
            if (maxAttWidth < "  [precision]".length()) {
                maxAttWidth = "  [precision]".length();
            }
        }

        maxAttWidth += 2;

        temp.append("\n\n");
        temp.append(pad("Class", " ", (maxAttWidth + maxWidth + 1) - "Class".length(), true));

        temp.append("\n");
        temp.append(pad("Attribute", " ", maxAttWidth - "Attribute".length(), false));
        // class labels
        for (int i = 0; i < m_Instances.numClasses(); i++) {
            String classL = m_Instances.classAttribute().value(i);
            temp.append(pad(classL, " ", maxWidth + 1 - classL.length(), true));
        }
        temp.append("\n");
        // class priors
        temp.append(pad("", " ", maxAttWidth, true));
        for (int i = 0; i < m_Instances.numClasses(); i++) {
            String priorP = Utils
                    .doubleToString(((DiscreteEstimator) m_ClassDistribution).getProbability(i), maxWidth, 2)
                    .trim();
            priorP = "(" + priorP + ")";
            temp.append(pad(priorP, " ", maxWidth + 1 - priorP.length(), true));
        }
        temp.append("\n");
        temp.append(pad("", "=",
                maxAttWidth + (maxWidth * m_Instances.numClasses()) + m_Instances.numClasses() + 1, true));
        temp.append("\n");

        // loop over the attributes
        int counter = 0;
        for (int i = 0; i < m_Instances.numAttributes(); i++) {
            if (i == m_Instances.classIndex()) {
                continue;
            }
            String attName = m_Instances.attribute(i).name();
            temp.append(attName + "\n");

            if (m_Distributions[counter][0] instanceof NormalEstimator) {
                String meanL = "  mean";
                temp.append(pad(meanL, " ", maxAttWidth + 1 - meanL.length(), false));
                for (int j = 0; j < m_Instances.numClasses(); j++) {
                    // means
                    NormalEstimator n = (NormalEstimator) m_Distributions[counter][j];
                    String mean = Utils.doubleToString(n.getMean(), maxWidth, 4).trim();
                    temp.append(pad(mean, " ", maxWidth + 1 - mean.length(), true));
                }
                temp.append("\n");
                // now do std deviations
                String stdDevL = "  std. dev.";
                temp.append(pad(stdDevL, " ", maxAttWidth + 1 - stdDevL.length(), false));
                for (int j = 0; j < m_Instances.numClasses(); j++) {
                    NormalEstimator n = (NormalEstimator) m_Distributions[counter][j];
                    String stdDev = Utils.doubleToString(n.getStdDev(), maxWidth, 4).trim();
                    temp.append(pad(stdDev, " ", maxWidth + 1 - stdDev.length(), true));
                }
                temp.append("\n");
                // now the weight sums
                String weightL = "  weight sum";
                temp.append(pad(weightL, " ", maxAttWidth + 1 - weightL.length(), false));
                for (int j = 0; j < m_Instances.numClasses(); j++) {
                    NormalEstimator n = (NormalEstimator) m_Distributions[counter][j];
                    String weight = Utils.doubleToString(n.getSumOfWeights(), maxWidth, 4).trim();
                    temp.append(pad(weight, " ", maxWidth + 1 - weight.length(), true));
                }
                temp.append("\n");
                // now the precisions
                String precisionL = "  precision";
                temp.append(pad(precisionL, " ", maxAttWidth + 1 - precisionL.length(), false));
                for (int j = 0; j < m_Instances.numClasses(); j++) {
                    NormalEstimator n = (NormalEstimator) m_Distributions[counter][j];
                    String precision = Utils.doubleToString(n.getPrecision(), maxWidth, 4).trim();
                    temp.append(pad(precision, " ", maxWidth + 1 - precision.length(), true));
                }
                temp.append("\n\n");

            } else if (m_Distributions[counter][0] instanceof DiscreteEstimator) {
                Attribute a = m_Instances.attribute(i);
                for (int j = 0; j < a.numValues(); j++) {
                    String val = "  " + a.value(j);
                    temp.append(pad(val, " ", maxAttWidth + 1 - val.length(), false));
                    for (int k = 0; k < m_Instances.numClasses(); k++) {
                        DiscreteEstimator d = (DiscreteEstimator) m_Distributions[counter][k];
                        String count = "" + d.getCount(j);
                        temp.append(pad(count, " ", maxWidth + 1 - count.length(), true));
                    }
                    temp.append("\n");
                }
                // do the totals
                String total = "  [total]";
                temp.append(pad(total, " ", maxAttWidth + 1 - total.length(), false));
                for (int k = 0; k < m_Instances.numClasses(); k++) {
                    DiscreteEstimator d = (DiscreteEstimator) m_Distributions[counter][k];
                    String count = "" + d.getSumOfCounts();
                    temp.append(pad(count, " ", maxWidth + 1 - count.length(), true));
                }
                temp.append("\n\n");
            } else if (m_Distributions[counter][0] instanceof KernelEstimator) {
                String kL = "  [# kernels]";
                temp.append(pad(kL, " ", maxAttWidth + 1 - kL.length(), false));
                for (int k = 0; k < m_Instances.numClasses(); k++) {
                    KernelEstimator ke = (KernelEstimator) m_Distributions[counter][k];
                    String nk = "" + ke.getNumKernels();
                    temp.append(pad(nk, " ", maxWidth + 1 - nk.length(), true));
                }
                temp.append("\n");
                // do num kernels, std. devs and precisions
                String stdDevL = "  [std. dev]";
                temp.append(pad(stdDevL, " ", maxAttWidth + 1 - stdDevL.length(), false));
                for (int k = 0; k < m_Instances.numClasses(); k++) {
                    KernelEstimator ke = (KernelEstimator) m_Distributions[counter][k];
                    String stdD = Utils.doubleToString(ke.getStdDev(), maxWidth, 4).trim();
                    temp.append(pad(stdD, " ", maxWidth + 1 - stdD.length(), true));
                }
                temp.append("\n");
                String precL = "  [precision]";
                temp.append(pad(precL, " ", maxAttWidth + 1 - precL.length(), false));
                for (int k = 0; k < m_Instances.numClasses(); k++) {
                    KernelEstimator ke = (KernelEstimator) m_Distributions[counter][k];
                    String prec = Utils.doubleToString(ke.getPrecision(), maxWidth, 4).trim();
                    temp.append(pad(prec, " ", maxWidth + 1 - prec.length(), true));
                }
                temp.append("\n");
                // first determine max number of kernels accross the classes
                int maxK = 0;
                for (int k = 0; k < m_Instances.numClasses(); k++) {
                    KernelEstimator ke = (KernelEstimator) m_Distributions[counter][k];
                    if (ke.getNumKernels() > maxK) {
                        maxK = ke.getNumKernels();
                    }
                }
                for (int j = 0; j < maxK; j++) {
                    // means first
                    String meanL = "  K" + (j + 1) + ": mean (weight)";
                    temp.append(pad(meanL, " ", maxAttWidth + 1 - meanL.length(), false));
                    for (int k = 0; k < m_Instances.numClasses(); k++) {
                        KernelEstimator ke = (KernelEstimator) m_Distributions[counter][k];
                        double[] means = ke.getMeans();
                        double[] weights = ke.getWeights();
                        String m = "--";
                        if (ke.getNumKernels() == 0) {
                            m = "" + 0;
                        } else if (j < ke.getNumKernels()) {
                            m = Utils.doubleToString(means[j], maxWidth, 4).trim();
                            m += " (" + Utils.doubleToString(weights[j], maxWidth, 1).trim() + ")";
                        }
                        temp.append(pad(m, " ", maxWidth + 1 - m.length(), true));
                    }
                    temp.append("\n");
                }
                temp.append("\n");
            }

            counter++;
        }
    }

    return temp.toString();
}

From source file:com.deafgoat.ml.prognosticator.AppClassifier.java

License:Apache License

/**
 * Returns the Weka type of the given attribute
 *//*w  ww.j ava2 s .  c  o  m*/
public String getAttributeType(Attribute attribute) {
    if (attribute.isDate()) {
        return "date";
    } else if (attribute.isNominal()) {
        return "nominal";
    } else if (attribute.isNumeric()) {
        return "numeric";
    } else {
        return "string";
    }
}

From source file:com.yahoo.labs.samoa.instances.WekaToSamoaInstanceConverter.java

License:Apache License

/**
* Get Samoa attribute from a weka attribute.
*
* @param index the index//from   w  w w . ja va2  s .c om
* @param attribute the attribute
* @return the attribute
*/
protected Attribute samoaAttribute(int index, weka.core.Attribute attribute) {
    Attribute samoaAttribute;
    if (attribute.isNominal()) {
        Enumeration enu = attribute.enumerateValues();
        List<String> attributeValues = new ArrayList<String>();
        while (enu.hasMoreElements()) {
            attributeValues.add((String) enu.nextElement());
        }
        samoaAttribute = new Attribute(attribute.name(), attributeValues);
    } else {
        samoaAttribute = new Attribute(attribute.name());
    }
    return samoaAttribute;
}