Package org.apache.commons.math3.linear

Examples of org.apache.commons.math3.linear.OpenMapRealVector$OpenMapSparseIterator


     * Creates a diagonal weight matrix.
     *
     * @param weight List of the values of the diagonal.
     */
    public Weight(double[] weight) {
        weightMatrix = new DiagonalMatrix(weight);
    }
View Full Code Here


     * @return the square-root of the weight matrix.
     */
    private RealMatrix squareRoot(RealMatrix m) {
        if (m instanceof DiagonalMatrix) {
            final int dim = m.getRowDimension();
            final RealMatrix sqrtM = new DiagonalMatrix(dim);
            for (int i = 0; i < dim; i++) {
                sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
            }
            return sqrtM;
        } else {
            final EigenDecomposition dec = new EigenDecomposition(m);
            return dec.getSquareRoot();
View Full Code Here

     * Creates a diagonal weight matrix.
     *
     * @param weight List of the values of the diagonal.
     */
    public Weight(double[] weight) {
        weightMatrix = new DiagonalMatrix(weight);
    }
View Full Code Here

     * Creates a diagonal weight matrix.
     *
     * @param weight List of the values of the diagonal.
     */
    public Weight(double[] weight) {
        weightMatrix = new DiagonalMatrix(weight);
    }
View Full Code Here

            for (int i = 0; i < dim; i++) {
                sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
            }
            return sqrtM;
        } else {
            final EigenDecomposition dec = new EigenDecomposition(m);
            return dec.getSquareRoot();
        }
    }
View Full Code Here

     * @throws NonSquareMatrixException if the argument is not
     * a square matrix.
     */
    public Weight(RealMatrix weight) {
        if (weight.getColumnDimension() != weight.getRowDimension()) {
            throw new NonSquareMatrixException(weight.getColumnDimension(),
                                               weight.getRowDimension());
        }

        weightMatrix = weight.copy();
    }
View Full Code Here

     * @throws NonSquareMatrixException if the argument is not
     * a square matrix.
     */
    public Weight(RealMatrix weight) {
        if (weight.getColumnDimension() != weight.getRowDimension()) {
            throw new NonSquareMatrixException(weight.getColumnDimension(),
                                               weight.getRowDimension());
        }

        weightMatrix = weight.copy();
    }
View Full Code Here

     * @throws NonSquareMatrixException if the argument is not
     * a square matrix.
     */
    public Weight(RealMatrix weight) {
        if (weight.getColumnDimension() != weight.getRowDimension()) {
            throw new NonSquareMatrixException(weight.getColumnDimension(),
                                               weight.getRowDimension());
        }

        weightMatrix = weight.copy();
    }
View Full Code Here

        Integer uindex = userIndexMap.get(userID);
        item.setEntry(uindex, rating);
        // update user History
        RealVector user = userHistory.get(userID);
        if (user == null) {
          user = new OpenMapRealVector(numOfItems);
          userHistory.put(userID, user);
        }
        user.setEntry(iindex, rating);
      }
      itemVectors.put(itemID, item);
    }

    // calculate sim

    Map<Integer, RealVector> itemSimilarity = new HashMap<Integer, RealVector>();
    List<Integer> item1List = new ArrayList<Integer>(itemIndexMap.keySet());
    List<Integer> item2List = new ArrayList<Integer>(item1List);

    int numSimilarItems = 100;
    Comparator<IndexAndScore> comparator = new IndexAndScoreComparator();
    Map<Integer, Queue<IndexAndScore>> topItemSimilarity =
      new HashMap<Integer, Queue<IndexAndScore>>();

    for (Integer itemID1 : item1List) {
      item2List.remove(0);
      Integer index1 = itemIndexMap.get(itemID1);
      for (Integer itemID2: item2List) {
        RealVector vector1 = itemVectors.get(itemID1);
        RealVector vector2 = itemVectors.get(itemID2);
        double score = vector1.cosine(vector2);
        if (score > params.threshold) {
          Integer index2 = itemIndexMap.get(itemID2);
          setTopItemSimilarity(topItemSimilarity, itemID1, index2, score, numSimilarItems,
            comparator);
          setTopItemSimilarity(topItemSimilarity, itemID2, index1, score, numSimilarItems,
            comparator);
        }
      }
    }

    for (Map.Entry<Integer, Queue<IndexAndScore>> entry : topItemSimilarity.entrySet()) {
      Iterator<IndexAndScore> it = entry.getValue().iterator();
      RealVector vector = new OpenMapRealVector(numOfItems);
      while (it.hasNext()) {
        IndexAndScore d = it.next();
        vector.setEntry(d.index, d.score);
      }
      itemSimilarity.put(entry.getKey(), vector);
    }

    return new CollaborativeFilteringModel(itemSimilarity, userHistory);
View Full Code Here

        Integer uindex = userIndexMap.get(userID);
        item.setEntry(uindex, rating);
        // update user History
        RealVector user = userHistory.get(userID);
        if (user == null) {
          user = new OpenMapRealVector(numOfItems);
          userHistory.put(userID, user);
        }
        user.setEntry(iindex, rating);
      }
      itemVectors.put(itemID, item);
    }

    // calculate sim

    Map<Integer, RealVector> itemSimilarity = new HashMap<Integer, RealVector>();
    List<Integer> item1List = new ArrayList<Integer>(itemIndexMap.keySet());
    List<Integer> item2List = new ArrayList<Integer>(item1List);

    int numSimilarItems = 100;
    Comparator<IndexAndScore> comparator = new IndexAndScoreComparator();
    Map<Integer, Queue<IndexAndScore>> topItemSimilarity =
      new HashMap<Integer, Queue<IndexAndScore>>();

    for (Integer itemID1 : item1List) {
      item2List.remove(0);
      Integer index1 = itemIndexMap.get(itemID1);
      for (Integer itemID2: item2List) {
        RealVector vector1 = itemVectors.get(itemID1);
        RealVector vector2 = itemVectors.get(itemID2);
        double score = vector1.cosine(vector2);
        if (score > params.threshold) {
          Integer index2 = itemIndexMap.get(itemID2);
          setTopItemSimilarity(topItemSimilarity, itemID1, index2, score, numSimilarItems,
            comparator);
          setTopItemSimilarity(topItemSimilarity, itemID2, index1, score, numSimilarItems,
            comparator);
        }
      }
    }

    for (Map.Entry<Integer, Queue<IndexAndScore>> entry : topItemSimilarity.entrySet()) {
      Iterator<IndexAndScore> it = entry.getValue().iterator();
      RealVector vector = new OpenMapRealVector(numOfItems);
      while (it.hasNext()) {
        IndexAndScore d = it.next();
        vector.setEntry(d.index, d.score);
      }
      itemSimilarity.put(entry.getKey(), vector);
    }

    return new Model(itemSimilarity, userHistory);
View Full Code Here

TOP

Related Classes of org.apache.commons.math3.linear.OpenMapRealVector$OpenMapSparseIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.