public RandomAccessSparseVector(int cardinality, int initialCapacity) { super(cardinality); values = new Int2DoubleOpenHashMap(initialCapacity, .5f); }
@Override public double getQuick(int index) { return values.get(index); }
@Override public IntToDoubleFunction similarity(int idx1) { Int2DoubleOpenHashMap map = new Int2DoubleOpenHashMap(); data.getUidxPreferences(idx1).forEach(iv -> map.put(iv.v1, iv.v2)); double norm2A = norm2Map.get(idx1); return idx2 -> { double product = data.getUidxPreferences(idx2) .mapToDouble(iv -> iv.v2 * map.get(iv.v1)) .sum(); return sim(product, norm2A, norm2Map.get(idx2)); }; }
Int2DoubleOpenHashMap map = new Int2DoubleOpenHashMap(NUM_RECORDS); double value = random.nextDouble(); pq.put(i, value); map.put(i, value); double value = random.nextDouble(); pq.put(key, value); map.put(key, value); for (Int2DoubleMap.Entry entry : map.int2DoubleEntrySet()) { list.add(new Pairs.IntDoublePair(entry.getKey(), entry.getValue()));
@Override public SparseFloatVector newNumberVector(Int2DoubleOpenHashMap dvalues, int maxdim) { int[] indexes = new int[dvalues.size()]; float[] values = new float[dvalues.size()]; // Import and sort the indexes ObjectIterator<Int2DoubleMap.Entry> iter = dvalues.int2DoubleEntrySet().fastIterator(); for(int i = 0; iter.hasNext(); i++) { indexes[i] = iter.next().getIntKey(); } Arrays.sort(indexes); // Import the values accordingly for(int i = 0; i < dvalues.size(); i++) { values[i] = (float) dvalues.get(indexes[i]); } return new SparseFloatVector(indexes, values, maxdim); }
/** * Constructor. * * @param size Expected size * @param def Default value */ public MapIntegerDBIDDoubleStore(int size, double def) { super(); map = new Int2DoubleOpenHashMap(size); map.defaultReturnValue(def); }
/** * Returns a map of item-score pairs. * * @param uidx index of the user whose scores are predicted * @return a map of item-score pairs */ @Override public Int2DoubleMap getScoresMap(int uidx) { Int2DoubleOpenHashMap scoresMap = new Int2DoubleOpenHashMap(); scoresMap.defaultReturnValue(0.0); data.getUidxPreferences(uidx) .forEach(jp -> neighborhood.getNeighbors(jp.v1) .forEach(is -> { double w = pow(is.v2, q); scoresMap.addTo(is.v1, w * jp.v2); })); return scoresMap; }
private Int2DoubleMap getProductMap(int uidx) { Int2DoubleOpenHashMap productMap = new Int2DoubleOpenHashMap(); productMap.defaultReturnValue(0.0); if (data.useIteratorsPreferentially()) { IntIterator iidxs = data.getUidxIidxs(uidx); DoubleIterator ivs = data.getUidxVs(uidx); while (iidxs.hasNext()) { int iidx = iidxs.nextInt(); double iv = ivs.nextDouble(); IntIterator vidxs = data.getIidxUidxs(iidx); DoubleIterator vvs = data.getIidxVs(iidx); while (vidxs.hasNext()) { productMap.addTo(vidxs.nextInt(), iv * vvs.nextDouble()); } } } else { data.getUidxPreferences(uidx) .forEach(ip -> data.getIidxPreferences(ip.v1) .forEach(up -> productMap.addTo(up.v1, ip.v2 * up.v2))); } productMap.remove(uidx); return productMap; }
@Override protected void prepareProcessInstance(V featureVector) { for(int it = featureVector.iter(); featureVector.iterValid(it); it = featureVector.iterAdvance(it)) { if(featureVector.iterDoubleValue(it) >= 0.) { final int dim = featureVector.iterDim(it); idf.put(dim, idf.get(dim) + 1); } } objcnt += 1; }
public static Int2DoubleOpenHashMap loadSparseDoubleRowFromPartition(FSDataInputStream input, MatrixPartitionMeta partMeta, int rowId) throws IOException { //RowOffset rowOffset = partMeta.getRowMetas().get(rowId); //input.seek(rowOffset.getOffset()); Preconditions.checkState(input.readInt() == rowId); int num = input.readInt(); Int2DoubleOpenHashMap row = new Int2DoubleOpenHashMap(); for (int i = 0; i < num; i++) { row.put(input.readInt(), input.readDouble()); } return row; }
@Override public void set(int index, double value) { values.put(index, value); }
/** * Multiple a value at a given index * * @param idx The index * @param i The value */ @Override public void multiply(int idx, double i) { if (this.containsKey(idx)) { final double val = super.get(idx); if (Math.abs(val * i - defaultValue) <= epsilon) { super.remove(idx); } else { super.put(idx, val * i); } } else { if (defaultValue != 0.0 && i != 0.0) { super.put(idx, defaultValue * i); } } }
++numterms; values.put(curdim, attribute); len += attribute; curterm = null; for(ObjectIterator<Int2DoubleMap.Entry> iter = values.int2DoubleEntrySet().fastIterator(); iter.hasNext();) { Int2DoubleMap.Entry entry = iter.next(); entry.setValue(entry.getDoubleValue() / len); values.clear(); labels.clear(); return true;
/** * Returns a map of item-score pairs. * * @param uidx index of the user whose scores are predicted * @return a map of item-score pairs */ @Override public Int2DoubleMap getScoresMap(int uidx) { Int2DoubleOpenHashMap scoresMap = new Int2DoubleOpenHashMap(); for (Entry<FastRankingRecommender<U, I>, Double> rw : recommenders) { double w = rw.getValue(); rw.getKey().getScoresMap(uidx).int2DoubleEntrySet() .forEach(e -> scoresMap.addTo(e.getIntKey(), w * e.getDoubleValue())); } return scoresMap; }
@Override public int getNumEntries() { return values.size(); }
@Override public void incrementQuick(int index, double increment) { invalidateCachedLength(); values.addTo( index, increment); }