Example usage for weka.attributeSelection PrincipalComponents getCorrelationMatrix

List of usage examples for weka.attributeSelection PrincipalComponents getCorrelationMatrix

Introduction

In this page you can find the example usage for weka.attributeSelection PrincipalComponents getCorrelationMatrix.

Prototype

public double[][] getCorrelationMatrix() 

Source Link

Document

Return the correlation/covariance matrix

Usage

From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java

License:Open Source License

public static ArrayList<String> preprocessInstances(Instances retval) {
    double[][] cMatrix;
    ArrayList<String> result = new ArrayList<String>();
    ArrayList<String> deleteAttNames = new ArrayList<String>();
    PrincipalComponents pc = new PrincipalComponents();
    HashMap<Integer, ArrayList<Integer>> filter = new HashMap<Integer, ArrayList<Integer>>();
    try {/*from ww  w  .ja  v a  2  s  .  c o m*/
        pc.buildEvaluator(retval);
        cMatrix = pc.getCorrelationMatrix();
        for (int i = 0; i < cMatrix.length; i++) {
            ArrayList<Integer> record = new ArrayList<Integer>();
            for (int j = i + 1; j < cMatrix.length; j++)
                if (cMatrix[i][j] >= correlationFactorThreshold
                        || cMatrix[i][j] <= -correlationFactorThreshold) {
                    record.add(j);
                }
            if (record.size() != 0) {
                filter.put(i, record);
            }
        }
        Iterator<Map.Entry<Integer, ArrayList<Integer>>> iter = filter.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<Integer, ArrayList<Integer>> entry = iter.next();
            ArrayList<Integer> arr = entry.getValue();
            for (int i = 0; i < arr.size(); i++)
                if (arr.get(i) != cMatrix.length - 1
                        && !deleteAttNames.contains(retval.attribute(arr.get(i)).name())) {
                    deleteAttNames.add(retval.attribute(arr.get(i)).name());
                }
            if (arr.contains(cMatrix.length - 1)) {
                result.add(retval.attribute(Integer.parseInt(entry.getKey().toString())).name());
            }
        }
        for (int i = 0; i < deleteAttNames.size(); i++) {
            retval.deleteAttributeAt(retval.attribute(deleteAttNames.get(i)).index());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}