Example usage for weka.attributeSelection PrincipalComponents PrincipalComponents

List of usage examples for weka.attributeSelection PrincipalComponents PrincipalComponents

Introduction

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

Prototype

PrincipalComponents

Source Link

Usage

From source file:PCADetector.java

License:Apache License

public boolean runPCA(ArrayList<Double> newData, int slidewdSz, double cAlpha, int nAttrs) {
    try {/*from w  w w .java2 s  . c  om*/
        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: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 {/*w  ww.  j  a  va  2s .c  om*/
        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 {//  ww  w. j  a va  2s  .c o  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:it.poliba.sisinflab.simlib.featureSelection.methods.PCA.java

public void execute(String dataset) {
    try {/*from   w w  w  .j av a 2  s. 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 av  a2s  . co  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;
}