Example usage for weka.core Instance attributeSparse

List of usage examples for weka.core Instance attributeSparse

Introduction

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

Prototype

public Attribute attributeSparse(int indexOfIndex);

Source Link

Document

Returns the attribute with the given index in the sparse representation.

Usage

From source file:MPCKMeans.java

License:Open Source License

/** Actual KMeans function */
protected void runKMeans() throws Exception {
    boolean converged = false;
    m_Iterations = 0;/*from w  ww.  ja v a2s . c o m*/
    m_numBlankIterations = 0;
    m_Objective = Double.POSITIVE_INFINITY;

    if (!m_isOfflineMetric) {
        if (m_useMultipleMetrics) {
            for (int i = 0; i < m_metrics.length; i++) {
                m_metrics[i].resetMetric();
                m_metricLearners[i].resetLearner();
            }
        } else {
            m_metric.resetMetric();
            m_metricLearner.resetLearner();
        }
        // initialize max CL penalties
        if (m_ConstraintsHash.size() > 0) {
            m_maxCLPenalties = calculateMaxCLPenalties();
        }
    }

    // initialize m_ClusterAssignments
    for (int i = 0; i < m_NumClusters; i++) {
        m_ClusterAssignments[i] = -1;
    }

    PrintStream fincoh = null;
    if (m_ConstraintIncoherenceFile != null) {
        fincoh = new PrintStream(new FileOutputStream(m_ConstraintIncoherenceFile));
    }

    while (!converged) {
        System.out.println("\n" + m_Iterations + ". Objective function: " + ((float) m_Objective));
        m_OldObjective = m_Objective;

        // E-step
        int numMovedPoints = findBestAssignments();

        m_numBlankIterations = (numMovedPoints == 0) ? m_numBlankIterations + 1 : 0;

        //      calculateObjectiveFunction(false);
        System.out.println((float) m_Objective + " - Objective function after point assignment(CALC)");
        System.out.println("\tvar=" + ((float) m_objVariance) + "\tC=" + ((float) m_objCannotLinks) + "\tM="
                + ((float) m_objMustLinks) + "\tLOG=" + ((float) m_objNormalizer) + "\tREG="
                + ((float) m_objRegularizer));

        // M-step
        updateClusterCentroids();

        //      calculateObjectiveFunction(false);
        System.out.println((float) m_Objective + " - Objective function after centroid estimation");
        System.out.println("\tvar=" + ((float) m_objVariance) + "\tC=" + ((float) m_objCannotLinks) + "\tM="
                + ((float) m_objMustLinks) + "\tLOG=" + ((float) m_objNormalizer) + "\tREG="
                + ((float) m_objRegularizer));

        if (m_Trainable == TRAINING_INTERNAL && !m_isOfflineMetric) {
            updateMetricWeights();
            if (m_verbose) {
                calculateObjectiveFunction(true);
                System.out.println((float) m_Objective + " - Objective function after metric update");
                System.out.println("\tvar=" + ((float) m_objVariance) + "\tC=" + ((float) m_objCannotLinks)
                        + "\tM=" + ((float) m_objMustLinks) + "\tLOG=" + ((float) m_objNormalizer) + "\tREG="
                        + ((float) m_objRegularizer));
            }

            if (m_ConstraintsHash.size() > 0) {
                m_maxCLPenalties = calculateMaxCLPenalties();
            }
        }

        if (fincoh != null) {
            printConstraintIncoherence(fincoh);
        }

        converged = convergenceCheck(m_OldObjective, m_Objective);
        m_Iterations++;
    }

    if (fincoh != null) {
        fincoh.close();
    }
    System.out.println("Converged!");
    System.err.print("Its\t" + m_Iterations + "\t");

    if (m_verbose) {
        System.out.println("Done clustering; top cluster features: ");
        for (int i = 0; i < m_NumClusters; i++) {
            System.out.println("Centroid " + i);
            TreeMap map = new TreeMap(Collections.reverseOrder());
            Instance centroid = m_ClusterCentroids.instance(i);
            for (int j = 0; j < centroid.numValues(); j++) {
                Attribute attr = centroid.attributeSparse(j);
                map.put(new Double(centroid.value(attr)), attr.name());
            }
            Iterator it = map.entrySet().iterator();
            for (int j = 0; j < 5 && it.hasNext(); j++) {
                Map.Entry entry = (Map.Entry) it.next();
                System.out.println("\t" + entry.getKey() + "\t" + entry.getValue());
            }
        }
    }
}

From source file:FeatureSelection.ReliefFAttributeEval.java

License:Open Source License

/**
 * Updates the minimum and maximum values for all the attributes based on a
 * new instance.//from  w  w  w  .jav  a 2s.com
 *
 * @param instance
 *            the new instance
 */
private void updateMinMax(Instance instance) {
    // for (int j = 0; j < m_numAttribs; j++) {
    try {
        for (int j = 0; j < instance.numValues(); j++) {
            if ((instance.attributeSparse(j).isNumeric()) && (!instance.isMissingSparse(j))) {
                if (Double.isNaN(m_minArray[instance.index(j)])) {
                    m_minArray[instance.index(j)] = instance.valueSparse(j);
                    m_maxArray[instance.index(j)] = instance.valueSparse(j);
                } else {
                    if (instance.valueSparse(j) < m_minArray[instance.index(j)]) {
                        m_minArray[instance.index(j)] = instance.valueSparse(j);
                    } else {
                        if (instance.valueSparse(j) > m_maxArray[instance.index(j)]) {
                            m_maxArray[instance.index(j)] = instance.valueSparse(j);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        System.err.println(ex);
        ex.printStackTrace();
    }
}