List of usage examples for org.apache.commons.math.linear RealVector mapDivide
RealVector mapDivide(double d);
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 w w w . jav a 2 s .c o m } } RealVector userFeaturesAsVector = new ArrayRealVector(userFeatures); RealVector normalised = userFeaturesAsVector.mapDivide(userFeaturesAsVector.getL1Norm()); return normalised.getData(); }
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 ww w.j a v a2 s .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(); }