Example usage for org.apache.mahout.math WeightedVector WeightedVector

List of usage examples for org.apache.mahout.math WeightedVector WeightedVector

Introduction

In this page you can find the example usage for org.apache.mahout.math WeightedVector WeightedVector.

Prototype

public WeightedVector(Vector v, Vector projection, int index) 

Source Link

Usage

From source file:io.ssc.relationdiscovery.SVD.java

License:Open Source License

private void findSingularVectors(Iterable<MatrixSlice> singularVectorCandidates) {

    Map<MatrixSlice, EigenStatus> eigenMetaData = Maps.newHashMap();

    SimpleEigenVerifier verifier = new SimpleEigenVerifier();
    for (MatrixSlice slice : singularVectorCandidates) {
        EigenStatus status = verifier.verify(A, slice.vector());
        eigenMetaData.put(slice, status);
    }/*from w  w  w .  ja  v  a 2 s .  c o m*/

    List<Map.Entry<MatrixSlice, EigenStatus>> prunedEigenMeta = Lists.newArrayList();

    for (Map.Entry<MatrixSlice, EigenStatus> entry : eigenMetaData.entrySet()) {
        if (Math.abs(1 - entry.getValue().getCosAngle()) < MAX_ERROR
                && entry.getValue().getEigenValue() > MIN_EIGENVALUE) {
            prunedEigenMeta.add(entry);
        }
    }

    Collections.sort(prunedEigenMeta, new Comparator<Map.Entry<MatrixSlice, EigenStatus>>() {
        @Override
        public int compare(Map.Entry<MatrixSlice, EigenStatus> e1, Map.Entry<MatrixSlice, EigenStatus> e2) {
            return Ints.compare(e1.getKey().index(), e2.getKey().index());
        }
    });

    int limit = prunedEigenMeta.size() > rank ? rank : prunedEigenMeta.size();
    for (int n = 0; n < limit; n++) {
        Map.Entry<MatrixSlice, EigenStatus> entry = prunedEigenMeta.get(n);
        double eigenvalue = Math.sqrt(entry.getValue().getEigenValue());
        log.info("Eigenvalue {}: {}", n, eigenvalue);
        singularVectors.add(new WeightedVector(entry.getKey().vector(), eigenvalue, n));
    }

}