Example usage for weka.core Instance toDoubleArray

List of usage examples for weka.core Instance toDoubleArray

Introduction

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

Prototype

public double[] toDoubleArray();

Source Link

Document

Returns the values of each attribute as an array of doubles.

Usage

From source file:mulan.transformations.BinaryRelevanceTransformation.java

License:Open Source License

/**
 * Remove all label attributes except label at position indexToKeep
 * @param instance //w  w w .j a  v  a 2 s. c o m
 * @param labelIndices 
 * @param indexToKeep 
 * @return transformed Instance
 */
public static Instance transformInstance(Instance instance, int[] labelIndices, int indexToKeep) {
    double[] values = instance.toDoubleArray();
    double[] transformedValues = new double[values.length - labelIndices.length + 1];

    int counterTransformed = 0;
    boolean isLabel = false;

    for (int i = 0; i < values.length; i++) {
        for (int j = 0; j < labelIndices.length; j++) {
            if (i == labelIndices[j] && i != indexToKeep) {
                isLabel = true;
                break;
            }
        }

        if (!isLabel) {
            transformedValues[counterTransformed] = instance.value(i);
            counterTransformed++;
        }
        isLabel = false;
    }

    Instance transformedInstance = DataUtils.createInstance(instance, 1, transformedValues);
    return transformedInstance;
}

From source file:mulan.transformations.regression.SingleTargetTransformation.java

License:Open Source License

/**
 * Remove all target attributes except target at position indexToKeep.
 * //from  w  w w . j  a v a2 s  .co m
 * @param instance the instance to be transformed
 * @param targetIndices the target indices to transform
 * @param indexToKeep the target to keep
 * @return transformed Instance
 */
public static Instance transformInstance(Instance instance, int[] targetIndices, int indexToKeep) {
    double[] values = instance.toDoubleArray();
    double[] transformedValues = new double[values.length - targetIndices.length + 1];

    int counterTransformed = 0;
    boolean isTarget = false;

    for (int i = 0; i < values.length; i++) {
        for (int j = 0; j < targetIndices.length; j++) {
            if (i == targetIndices[j] && i != indexToKeep) {
                isTarget = true;
                break;
            }
        }

        if (!isTarget) {
            transformedValues[counterTransformed] = instance.value(i);
            counterTransformed++;
        }
        isTarget = false;
    }

    Instance transformedInstance = DataUtils.createInstance(instance, 1, transformedValues);
    return transformedInstance;
}

From source file:mulan.transformations.RemoveAllLabels.java

License:Open Source License

public static Instance transformInstance(Instance instance, int[] labelIndices) {
    double[] oldValues = instance.toDoubleArray();
    double[] newValues = new double[oldValues.length - labelIndices.length];
    int counter1 = 0;
    int counter2 = 0;
    for (int i = 0; i < oldValues.length; i++) {
        if (counter1 < labelIndices.length)
            if (i == labelIndices[counter1]) {
                counter1++;//from  ww  w.ja v a 2 s. co m
                continue;
            }
        newValues[counter2] = oldValues[i];
        counter2++;
    }
    return DataUtils.createInstance(instance, instance.weight(), newValues);
}

From source file:org.dynamicfactory.property.InstanceFactory.java

License:Open Source License

@Override
public String exportToString(Instance type, Graph g) {
    double weight = type.weight();
    double[] values = type.toDoubleArray();
    StringBuffer buffer = new StringBuffer();
    buffer.append(Double.toString(weight));
    for (int i = 0; i < values.length; ++i) {
        buffer.append(",").append(Double.toString(values[i]));
    }/*from w ww  .java 2s  . com*/
    return buffer.toString();
}

From source file:org.knime.knip.suise.node.boundarymodel.contourdata.IntervalRule.java

License:Open Source License

/**
 * {@inheritDoc}//from w ww  . jav a2  s  .c  o m
 */
@Override
public void updateClassifier(Instance instance) throws Exception {
    if (m_min == null || m_max == null) {
        m_min = new DenseInstance(1.0, instance.toDoubleArray());
        m_max = new DenseInstance(1.0, instance.toDoubleArray());
    } else {
        for (int a = 0; a < instance.numAttributes() - 1; a++) {
            m_min.setValue(a, Math.min(instance.value(a), m_min.value(a)));
            m_max.setValue(a, Math.max(instance.value(a), m_max.value(a)));
        }
    }

}

From source file:svmal.SVMStrategy.java

public static Instances InstancesToInstances2(Instances insts) {
    Instances result = new Instances(insts, 0, 0);
    for (int i = 0; i < insts.numInstances(); i++) {
        Instance orig = insts.get(i);
        Instance2 inst2 = new Instance2(orig.weight(), orig.toDoubleArray());
        inst2.setDataset(result);//w  w w  .  j ava2  s.  com
        result.add(inst2);
    }
    return result;
}

From source file:tml.utils.DistanceLib.java

License:Apache License

public static double euclidean(Instance inst1, Instance inst2) {
    DoubleVector x = new DoubleVector(inst1.toDoubleArray());
    DoubleVector y = new DoubleVector(inst2.toDoubleArray());

    double distance = x.minus(y).norm2();
    return distance;
}

From source file:tml.utils.DistanceLib.java

License:Apache License

public static double cosine(Instance inst1, Instance inst2) {
    DoubleVector x = new DoubleVector(inst1.toDoubleArray());
    DoubleVector y = new DoubleVector(inst2.toDoubleArray());

    double dotXY = x.times(y).norm1();
    double cosim = dotXY / (x.norm2() * y.norm2());

    return cosim;
}

From source file:tml.utils.DistanceLib.java

License:Apache License

public static double jaccard(Instance inst1, Instance inst2) {
    DoubleVector x = new DoubleVector(inst1.toDoubleArray());
    DoubleVector y = new DoubleVector(inst2.toDoubleArray());

    double intersection = 0.0;

    for (int i = 0; i < x.size(); i++) {
        intersection += Math.min(x.get(i), y.get(i));
    }//w w  w .  j  a v a2 s.  c om
    if (intersection > 0.0) {
        double union = x.norm1() + y.norm1() - intersection;
        return intersection / union;
    } else {
        return 0.0;
    }
}

From source file:tubesduaai.FFNN.java

public double classifyInstance(Instance instnc) throws Exception {
    double[] temp = instnc.toDoubleArray();
    float ret = 0;
    int first = 0;
    while (!(is_output(first))) {
        first++;//w  w w  . ja  v  a2  s.c o  m
    }
    int imax = first;
    if (hidden_neuron == 0) {
        int i = jml_perceptron - datas.numClasses();
        for (; i < jml_perceptron; i++) {
            float out = 0;
            for (int j = 0; is_input(j); j++) {
                out += perceptron.get(j).get(i).weight * temp[j];
            }
            perceptron.get(i).get(i).output = hitung_sigmoid(out);
            ret = hitung_sigmoid(out);
        }
    }
    return get_kelas(ret);
}