Example usage for weka.core Instance value

List of usage examples for weka.core Instance value

Introduction

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

Prototype

public double value(Attribute att);

Source Link

Document

Returns an instance's attribute value in internal format.

Usage

From source file:meka.core.MLUtils.java

License:Open Source License

/** 
 * ToIntArray - raw instance to int[] representation
 *//* w ww  . j  a v a 2  s. c o  m*/
public static final int[] toIntArray(Instance x, int L) {
    int y[] = new int[L];
    for (int j = 0; j < L; j++) {
        // added the following if-statement to change missing values to -1 
        if (x.isMissing(j)) {
            y[j] = -1;
        } else {
            y[j] = (int) Math.round(x.value(j));
        }
    }
    return y;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * CopyValues - Set x_dest[j+offset] = x_src[i+from].
 *//*from w ww  .j a  v a  2s  . c o m*/
public static final Instance copyValues(Instance x_dest, Instance x_src, int from, int offset) {
    int d = x_src.numAttributes();
    for (int i = from, j = 0; i < d; i++, j++) {
        x_dest.setValue(j + offset, x_src.value(i));
    }
    return x_dest;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * CopyValues - Set x_dest[i++] = x_src[j] for all j in indices[].
 *//*from   w  w  w  .j av a2 s.c  om*/
public static final Instance copyValues(Instance x_dest, Instance x_src, int indices[]) {
    int i = 0;
    for (int j : indices) {
        x_dest.setValue(i++, x_src.value(j));
    }
    return x_dest;
}

From source file:meka.core.MLUtils.java

License:Open Source License

public static final String toDebugString(Instance x) {
    int L = x.classIndex();
    StringBuilder sb = new StringBuilder();
    sb.append("y = [");
    for (int j = 0; j < L; j++) {
        sb.append(x.value(j) + " ");
    }//from w  ww.jav  a 2 s .co m
    sb.append("], x = [");
    for (int j = L; j < L + 10; j++) {
        sb.append(x.value(j) + " ");
    }
    sb.append(" ... ]");
    return sb.toString();
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * Get K - get the number of values associated with each label L.
 * @param   D    a dataset//  w w w  .  java2s .  c  o  m
 * @return   a vector of size L: K_1,...,K_L
 */
public int[] getK(Instances D) {
    int L = D.classIndex();
    HashSet counts[] = new HashSet[L];
    int K[] = new int[L];
    for (int j = 0; j < L; j++) {
        counts[j] = new HashSet<Integer>();
        for (Instance x : D) {
            int k = (int) x.value(j);
            counts[j].add(k);
        }
        K[j] = counts[j].size();
        /*
           System.out.println(""+j+" = "+counts[j]);
           if (counts[j].size() < 2) {
           System.out.println("OK, this is a problem ...");
        //System.exit(1);
           }
           */
    }
    return K;
}

From source file:meka.core.StatUtils.java

License:Open Source License

/**
 * Delta(x_1,x_2,x_3 = v_1,v_2,v_3) for j = 1,2,3, k = 1,2,3.
 *//*from w  w  w . j a  v a  2 s.co  m*/
private static boolean match(Instance x, int indices[], int values[]) {
    for (int j = 0; j < indices.length; j++) {
        int v = (int) Math.round(x.value(indices[j]));
        if (v != values[j]) {
            return false;
        }
    }
    return true;
}

From source file:meka.core.SuperLabelUtils.java

License:Open Source License

/** Encode a vector of integer values to a string */
public static String encodeValue(Instance x, int indices[]) {
    int values[] = new int[indices.length];

    for (int j = 0; j < indices.length; j++) {
        values[j] = (int) x.value(indices[j]);
    }//www .  j  a  va 2s.  c  om
    return new LabelVector(values).toString();
}

From source file:meka.experiment.statisticsexporters.WekaFilter.java

License:Open Source License

/**
 * Converts the Instances back into statistics.
 *
 * @param data          the data to convert
 * @return              the generated statistics
 *//*  w w  w . ja  va 2 s  .c o  m*/
protected List<EvaluationStatistics> fromInstances(Instances data) {
    List<EvaluationStatistics> result;
    EvaluationStatistics stat;
    MultiLabelClassifier cls;
    String rel;
    int i;
    int n;
    Instance inst;

    result = new ArrayList<>();

    if (data.attribute(EvaluationStatistics.KEY_CLASSIFIER) == null) {
        log("Failed to locate attribute: " + EvaluationStatistics.KEY_CLASSIFIER);
        return result;
    }
    if (data.attribute(EvaluationStatistics.KEY_RELATION) == null) {
        log("Failed to locate attribute: " + EvaluationStatistics.KEY_RELATION);
        return result;
    }

    for (i = 0; i < data.numInstances(); i++) {
        inst = data.instance(i);
        try {
            cls = OptionUtils.fromCommandLine(MultiLabelClassifier.class,
                    inst.stringValue(data.attribute(EvaluationStatistics.KEY_CLASSIFIER)));
            rel = inst.stringValue(data.attribute(EvaluationStatistics.KEY_RELATION));
            stat = new EvaluationStatistics(cls, rel, null);
            for (n = 0; n < inst.numAttributes(); n++) {
                if (inst.attribute(n).isNumeric() && !inst.isMissing(n)) {
                    stat.put(inst.attribute(n).name(), inst.value(n));
                }
            }
            result.add(stat);
        } catch (Exception e) {
            handleException("Failed to process instance: " + inst, e);
        }
    }

    return result;
}

From source file:milk.classifiers.MINND.java

License:Open Source License

/**
 * Calculates the distance between two instances
 *
 * @param first the first instance/*from ww w.j  a  v  a2s  .c o m*/
 * @param second the second instance
 * @return the distance between the two given instances
 */
private double distance(Instance first, double[] mean, double[] var, int pos) {

    double diff, distance = 0;
    int j = 0;
    for (int i = 0; i < first.numAttributes(); i++) {
        // Skipp nominal attributes (incl. class & ID)
        if ((i == m_ClassIndex) || (i == m_IdIndex))
            continue;

        // If attribute is numeric
        if (first.attribute(i).isNumeric()) {
            if (!first.isMissing(i)) {
                diff = first.value(i) - mean[j];
                if (Utils.gr(var[j], m_ZERO))
                    distance += m_Change[pos][j] * var[j] * diff * diff;
                else
                    distance += m_Change[pos][j] * diff * diff;
            } else {
                if (Utils.gr(var[j], m_ZERO))
                    distance += m_Change[pos][j] * var[j];
                else
                    distance += m_Change[pos][j] * 1.0;
            }
        }
        j++;
    }

    return distance;
}

From source file:milk.classifiers.MINND.java

License:Open Source License

/**
 * Updates the minimum and maximum values for all the attributes
 * based on a new exemplar./*from  w  w w . j a  va2 s. c  o m*/
 *
 * @param ex the new exemplar
 */
private void updateMinMax(Exemplar ex) {
    Instances insts = ex.getInstances();
    int m = 0;
    for (int j = 0; j < insts.numAttributes(); j++) {
        if ((j != ex.idIndex()) && (j != ex.classIndex())) {
            if (insts.attribute(j).isNumeric()) {
                for (int k = 0; k < insts.numInstances(); k++) {
                    Instance ins = insts.instance(k);
                    if (!ins.isMissing(j)) {
                        if (Double.isNaN(m_MinArray[m])) {
                            m_MinArray[m] = ins.value(j);
                            m_MaxArray[m] = ins.value(j);
                        } else {
                            if (ins.value(j) < m_MinArray[m])
                                m_MinArray[m] = ins.value(j);
                            else if (ins.value(j) > m_MaxArray[m])
                                m_MaxArray[m] = ins.value(j);
                        }
                    }
                }
            }
            m++;
        }
    }
}