Example usage for weka.attributeSelection PrincipalComponents setCenterData

List of usage examples for weka.attributeSelection PrincipalComponents setCenterData

Introduction

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

Prototype

public void setCenterData(boolean center) 

Source Link

Document

Set whether to center (rather than standardize) the data.

Usage

From source file:PCADetector.java

License:Apache License

public boolean runPCA(ArrayList<Double> newData, int slidewdSz, double cAlpha, int nAttrs) {
    try {//  www .  j a v a  2  s. co m
        if (m_nDims == 0) {
            m_nDims = nAttrs;
            for (int i = 0; i < this.m_nDims; i++) {
                m_oriDataMatrix.add(new ArrayList<Double>()); // one list for each attribute
            }
        }
        verifyData(newData);
        this.c_alpha = cAlpha;
        if (false == prepareData(newData, slidewdSz))
            return false;
        Instances oriDataInsts = getInstances();
        if (oriDataInsts != null) {
            // standardization + PCA covariance matrix
            m_scaledInstances = new Instances(oriDataInsts);
            Standardize filter = new Standardize();

            filter.setInputFormat(m_scaledInstances);
            m_scaledInstances = Standardize.useFilter(m_scaledInstances, filter); // standardization

            PrincipalComponents PCA = new PrincipalComponents();
            PCA.setVarianceCovered(1.0); // means 100%
            PCA.setMaximumAttributeNames(-1);
            PCA.setCenterData(true);
            Ranker ranker = new Ranker();
            AttributeSelection selector = new AttributeSelection();
            selector.setSearch(ranker);
            selector.setEvaluator(PCA);
            selector.SelectAttributes(m_scaledInstances);
            //                Instances transformedData = selector.reduceDimensionality(m_scaledInstances);

            // get sorted eigens
            double[] eigenValues = PCA.getEigenValues();
            // eigenVectors[i][j]  i: rows; j: cols
            double[][] eigenVectors = PCA.getUnsortedEigenVectors();
            Sort(eigenValues, eigenVectors);
            setEigens(eigenValues);

            // get residual start dimension
            int residualStartDimension = -1;
            double sum = 0;
            double major = 0;
            for (int ss = 0; ss < eigenValues.length; ss++) {
                sum += eigenValues[ss];
            }
            for (int ss = 0; ss < eigenValues.length; ss++) {
                major += eigenValues[ss];
                if ((residualStartDimension < 0) && (major / sum > 0.95)) {
                    residualStartDimension = ss + 1;
                    break;
                }
            }
            //            System.out.println("residualStartDim: "+residualStartDimension);
            m_threshold = computeThreshold(eigenValues, residualStartDimension);

            // check new data abnormal or not
            boolean bAbnormal = checkSPE(eigenVectors, residualStartDimension, newData);
            computeProjPCs(eigenVectors, residualStartDimension, newData); // only for demo

            if (bAbnormal) { // anomaly, now to diagnosis
                // check original space using all the lists
                diagnosis(eigenVectors, residualStartDimension, newData);
            }

        }

    } catch (Exception exc) {
    }
    return true;
}

From source file:it.poliba.sisinflab.simlib.featureSelection.methods.PCA.java

public void execute(String dataset) {
    try {//from   w  w w  .  j ava2s .c o  m

        if (dataset.length() == 0)
            throw new IllegalArgumentException();
        // Load input dataset.
        DataSource source = new DataSource(dataset);
        Instances data = source.getDataSet();

        // Performs a principal components analysis.
        PrincipalComponents pcaEvaluator = new PrincipalComponents();

        // Sets the amount of variance to account for when retaining principal
        // components.
        pcaEvaluator.setVarianceCovered(1.0);
        // Sets maximum number of attributes to include in transformed attribute
        // names.
        pcaEvaluator.setMaximumAttributeNames(-1);

        // Scaled X such that the variance of each feature is 1.
        boolean scale = true;
        if (scale) {
            pcaEvaluator.setCenterData(true);
        } else {
            pcaEvaluator.setCenterData(false);
        }

        // Ranking the attributes.
        Ranker ranker = new Ranker();

        ranker.setNumToSelect(-1);

        AttributeSelection selector = new AttributeSelection();
        selector.setSearch(ranker);
        selector.setEvaluator(pcaEvaluator);
        selector.SelectAttributes(data);

        // Transform data into eigenvector basis.
        Instances transformedData = selector.reduceDimensionality(data);
        PrintStream o = new PrintStream(new File("data/" + "PCAResults" + ".txt"));
        System.setOut(o);
        System.out.println(Arrays.toString(selector.rankedAttributes()));
        System.out.println(Arrays.toString(selector.selectedAttributes()));
        //System.out.println(selector.CVResultsString());
        System.out.println(selector.toResultsString());

        System.out.println();

    } catch (IllegalArgumentException e) {
        System.err.println("Error");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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  a va  2s . 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;
}