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

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

Introduction

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

Prototype

double getL1Norm();

Source Link

Document

Returns the L1 norm of the vector.

Usage

From source file:io.seldon.mf.RecentMfRecommender.java

public double[] createAvgProductVector(List<Long> recentitemInteractions, Map<Long, float[]> productFeatures) {
    int numLatentFactors = productFeatures.values().iterator().next().length;
    double[] userFeatures = new double[numLatentFactors];
    for (Long item : recentitemInteractions) {
        float[] productFactors = productFeatures.get(item);
        if (productFactors != null) {
            for (int feature = 0; feature < numLatentFactors; feature++) {
                userFeatures[feature] += productFactors[feature];
            }/*from   www  . ja  v  a  2s  .co m*/
        }
    }
    RealVector userFeaturesAsVector = new ArrayRealVector(userFeatures);
    RealVector normalised = userFeaturesAsVector.mapDivide(userFeaturesAsVector.getL1Norm());

    return normalised.getData();
}

From source file:com.opengamma.analytics.math.matrix.CommonsMatrixAlgebra.java

/**
 * {@inheritDoc}/*from   w  w w. j  a  va  2  s  .  com*/
 */
@Override
public double getNorm1(final Matrix<?> m) {
    Validate.notNull(m, "m");
    if (m instanceof DoubleMatrix1D) {
        final RealVector temp = CommonsMathWrapper.wrap((DoubleMatrix1D) m);
        return temp.getL1Norm();
    } else if (m instanceof DoubleMatrix2D) {
        final RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix2D) m);
        // TODO find if commons implements this anywhere, so we are not doing it
        // by hand
        double max = 0.0;
        for (int col = temp.getColumnDimension() - 1; col >= 0; col--) {
            max = Math.max(max, temp.getColumnVector(col).getL1Norm());
        }
        return max;

    }
    throw new IllegalArgumentException("Can only find norm1 of DoubleMatrix2D; have " + m.getClass());
}

From source file:org.plista.kornakapi.core.recommender.FoldingFactorization.java

public double[] foldInAnonymousUser(long[] itemIDs) throws NoSuchItemException {

    double[] userFeatures = new double[factorization.numFeatures()];
    for (long itemID : itemIDs) {
        try {/*from w w  w.  j  a  va2s. c o  m*/
            int itemIndex = factorization.itemIndex(itemID);
            for (int feature = 0; feature < factorization.numFeatures(); feature++) {
                userFeatures[feature] += factorization.allItemFeatures()[itemIndex][feature];
            }
        } catch (NoSuchItemException e) {
            if (log.isDebugEnabled()) {
                log.debug("Item unknown: {}", itemID);
                if (itemIDs.length == 1) {
                    throw new NoSuchItemException("At least one item must be known");
                }
            }
        }
    }
    RealVector userFeaturesAsVector = new ArrayRealVector(userFeatures);
    RealVector normalised = userFeaturesAsVector.mapDivide(userFeaturesAsVector.getL1Norm());

    return normalised.getData();
}