Example usage for weka.core Instances kthSmallestValue

List of usage examples for weka.core Instances kthSmallestValue

Introduction

In this page you can find the example usage for weka.core Instances kthSmallestValue.

Prototype

public double kthSmallestValue(int attIndex, int k) 

Source Link

Document

Returns the kth-smallest attribute value of a numeric attribute.

Usage

From source file:org.isep.simizer.example.policy.utils.IterativeSimpleKMeans.java

License:Open Source License

/**
 * Move the centroid to it's new coordinates. Generate the centroid
 * coordinates based on it's members (objects assigned to the cluster of the
 * centroid) and the distance function being used.
 *
 * @param centroidIndex index of the centroid which the coordinates will be
 * computed/*from   ww  w .jav  a2 s  .co m*/
 * @param members the objects that are assigned to the cluster of this
 * centroid
 * @param updateClusterInfo if the method is supposed to update the
 * m_Cluster arrays
 * @return the centroid coordinates
 */
protected double[] moveCentroid(int centroidIndex, Instances members, boolean updateClusterInfo) {
    double[] vals = new double[members.numAttributes()];

    //used only for Manhattan Distance
    Instances sortedMembers = null;
    int middle = 0;
    boolean dataIsEven = false;

    if (m_DistanceFunction instanceof ManhattanDistance) {
        middle = (members.numInstances() - 1) / 2;
        dataIsEven = ((members.numInstances() % 2) == 0);
        if (m_PreserveOrder) {
            sortedMembers = members;
        } else {
            sortedMembers = new Instances(members);
        }
    }

    for (int j = 0; j < members.numAttributes(); j++) {

        //in case of Euclidian distance the centroid is the mean point
        //in case of Manhattan distance the centroid is the median point
        //in both cases, if the attribute is nominal, the centroid is the mode
        if (m_DistanceFunction instanceof EuclideanDistance || members.attribute(j).isNominal()) {
            vals[j] = members.meanOrMode(j);
        } else if (m_DistanceFunction instanceof ManhattanDistance) {
            //singleton special case
            if (members.numInstances() == 1) {
                vals[j] = members.instance(0).value(j);
            } else {
                vals[j] = sortedMembers.kthSmallestValue(j, middle + 1);
                if (dataIsEven) {
                    vals[j] = (vals[j] + sortedMembers.kthSmallestValue(j, middle + 2)) / 2;
                }
            }
        }

        if (updateClusterInfo) {
            m_ClusterMissingCounts[centroidIndex][j] = members.attributeStats(j).missingCount;
            m_ClusterNominalCounts[centroidIndex][j] = members.attributeStats(j).nominalCounts;
            if (members.attribute(j).isNominal()) {
                if (m_ClusterMissingCounts[centroidIndex][j] > m_ClusterNominalCounts[centroidIndex][j][Utils
                        .maxIndex(m_ClusterNominalCounts[centroidIndex][j])]) {
                    vals[j] = Instance.missingValue(); // mark mode as missing
                }
            } else {
                if (m_ClusterMissingCounts[centroidIndex][j] == members.numInstances()) {
                    vals[j] = Instance.missingValue(); // mark mean as missing
                }
            }
        }
    }
    if (updateClusterInfo) {
        m_ClusterCentroids.add(new Instance(1.0, vals));
    }
    return vals;
}

From source file:org.opentox.jaqpot3.qsar.util.WekaInstancesProcess.java

License:Open Source License

private static double attributeMinValue(Instances dataInst, int attributeIndex) {
    return dataInst.kthSmallestValue(attributeIndex, 1);
}

From source file:org.vimarsha.utils.impl.ArffAttributeInfoExtractor.java

License:Open Source License

/**
 * Returns a table model with the attribute related info when the selected attribute index is passed.
 *
 * @param index index of the attribute// w ww  . ja  va2 s.  com
 * @return DefaultTableModel
 */
public DefaultTableModel getArffAttributeInfo(int index) {
    DefaultTableModel defaultTableModel = new DefaultTableModel();
    Instances temp = this.arffData;
    //since kthSmallestValue cannot handle missing values, they need to be removed.
    temp.deleteWithMissing(index);
    ArrayList<String> tmp = new ArrayList<String>();
    defaultTableModel.addColumn("Statistics", new String[] { "Name", "Variance", "Min", "Max", "Mean" });
    tmp.add(temp.attribute(index).name());
    tmp.add(String.valueOf(temp.variance(index)));
    tmp.add(String.valueOf(temp.kthSmallestValue(index, 1))); //min value is the 1st smallest value
    tmp.add(String.valueOf(temp.kthSmallestValue(index, temp.numInstances()))); //max value is the last smallest value
    tmp.add(String.valueOf(temp.meanOrMode(index)));
    defaultTableModel.addColumn("Value", tmp.toArray());
    return defaultTableModel;
}