Example usage for weka.attributeSelection PrincipalComponents buildEvaluator

List of usage examples for weka.attributeSelection PrincipalComponents buildEvaluator

Introduction

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

Prototype

@Override
public void buildEvaluator(Instances data) throws Exception 

Source Link

Document

Initializes principal components and performs the analysis

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 w ww .j a  v a 2  s.co 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;
}

From source file:com.rapidminer.operator.features.transformation.PrincipalComponentsTransformation.java

License:Open Source License

public IOObject[] apply() throws OperatorException {
    ExampleSet exampleSet = getInput(ExampleSet.class);

    PrincipalComponents transformation = new PrincipalComponents();
    transformation.setNormalize(false); // if the user wants to normalize
    // the data he has to apply the
    // filter before
    transformation.setVarianceCovered(getParameterAsDouble(PARAMETER_MIN_VARIANCE_COVERAGE));

    log(getName() + ": Converting to Weka instances.");
    Instances instances = WekaTools.toWekaInstances(exampleSet, "PCAInstances", WekaInstancesAdaptor.LEARNING);
    try {/*w  ww  . j av  a 2 s . co  m*/
        log(getName() + ": Building principal components.");
        transformation.buildEvaluator(instances);
    } catch (Exception e) {
        throw new UserError(this, e, 905, new Object[] { "PrincipalComponents", e });
    }

    ExampleSet result = null;
    try {
        Instances transformed = transformation.transformedData();
        result = WekaTools.toRapidMinerExampleSet(transformed, "pc");
    } catch (Exception e) {
        throw new UserError(this, 905, "Principal Components Transformation",
                "Cannot convert to principal components (" + e.getMessage() + ")");
    }
    return new IOObject[] { result };
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Return the PCA of the 3d points/*from w  w  w  . j av a 2 s.  c  o  m*/
 */
public PrincipalComponents GetPCA(List<Point3d> points, boolean center_data) {

    // pca
    PrincipalComponents pca = new PrincipalComponents();

    // Create the x y z attributes
    FastVector atts = new FastVector();

    Attribute x = new Attribute("x");
    Attribute y = new Attribute("y");
    Attribute z = new Attribute("z");

    atts.addElement(x);
    atts.addElement(y);
    atts.addElement(z);

    // Create instances
    Instances points_dataset = new Instances("PointsPCA", atts, points.size());

    // iterate through all the points
    for (int i = 0; i < points.size(); i++) {

        // new intance of 3 values
        Instance inst = new SparseInstance(3);

        // get pos point
        Point3d pos = points.get(i);

        // Set instance's values for the attributes x, y, z
        inst.setValue(x, pos.x);
        inst.setValue(y, pos.y);
        inst.setValue(z, pos.z);

        // add instance to dataset
        points_dataset.add(inst);
    }

    // center data
    pca.setCenterData(center_data);

    try {
        // build evaluator
        pca.buildEvaluator(points_dataset);

    } catch (java.lang.Exception e) {
        e.printStackTrace();
    }
    //      System.out.println(points_dataset.toSummaryString());
    //      System.out.println(pca.toString());      
    return pca;
}