Example usage for org.apache.commons.math3.linear RealVector getL1Norm

List of usage examples for org.apache.commons.math3.linear RealVector getL1Norm

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealVector getL1Norm.

Prototype

public double getL1Norm() 

Source Link

Document

Returns the L1 norm of the vector.

Usage

From source file:org.lenskit.mf.funksvd.FunkSVDModelProvider.java

@Override
public FunkSVDModel get() {
    int userCount = snapshot.getUserIds().size();
    RealMatrix userFeatures = MatrixUtils.createRealMatrix(userCount, featureCount);

    int itemCount = snapshot.getItemIds().size();
    RealMatrix itemFeatures = MatrixUtils.createRealMatrix(itemCount, featureCount);

    logger.debug("Learning rate is {}", rule.getLearningRate());
    logger.debug("Regularization term is {}", rule.getTrainingRegularization());

    logger.info("Building SVD with {} features for {} ratings", featureCount, snapshot.getRatings().size());

    TrainingEstimator estimates = rule.makeEstimator(snapshot);

    List<FeatureInfo> featureInfo = new ArrayList<>(featureCount);

    // Use scratch vectors for each feature for better cache locality
    // Per-feature vectors are strided in the output matrices
    RealVector uvec = MatrixUtils.createRealVector(new double[userCount]);
    RealVector ivec = MatrixUtils.createRealVector(new double[itemCount]);

    for (int f = 0; f < featureCount; f++) {
        logger.debug("Training feature {}", f);
        StopWatch timer = new StopWatch();
        timer.start();//from  ww  w  .  j ava2s .c  o m

        uvec.set(initialValue);
        ivec.set(initialValue);

        FeatureInfo.Builder fib = new FeatureInfo.Builder(f);
        double rmse = trainFeature(f, estimates, uvec, ivec, fib);
        summarizeFeature(uvec, ivec, fib);
        featureInfo.add(fib.build());

        // Update each rating's cached value to accommodate the feature values.
        estimates.update(uvec, ivec);

        // And store the data into the matrix
        userFeatures.setColumnVector(f, uvec);
        assert Math.abs(userFeatures.getColumnVector(f).getL1Norm()
                - uvec.getL1Norm()) < 1.0e-4 : "user column sum matches";
        itemFeatures.setColumnVector(f, ivec);
        assert Math.abs(itemFeatures.getColumnVector(f).getL1Norm()
                - ivec.getL1Norm()) < 1.0e-4 : "item column sum matches";

        timer.stop();
        logger.info("Finished feature {} in {} (RMSE={})", f, timer, rmse);
    }

    // Wrap the user/item matrices because we won't use or modify them again
    return new FunkSVDModel(userFeatures, itemFeatures, snapshot.userIndex(), snapshot.itemIndex(),
            featureInfo);
}

From source file:trustframework.utils.Similarity.java

/**
 * Build a real vector from frequencies//from   w ww .  ja v a2s.  c o m
 * @param <T> Type of hask keys
 * @param freq hash with frequencies
 * @param keys set of all keys available
 * @return 
 */
private static <T> RealVector buildRealVector(Map<T, Integer> freq, Set<T> keys) {
    RealVector vector = new ArrayRealVector(keys.size());
    int i = 0;
    for (T term : keys) {
        int value = freq.containsKey(term) ? freq.get(term) : 0;
        vector.setEntry(i++, value);
    }
    vector = vector.mapDivide(vector.getL1Norm());
    return vector;
}