it.unimi.dsi.fastutil.ints.Int2DoubleMap Java Examples

The following examples show how to use it.unimi.dsi.fastutil.ints.Int2DoubleMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FastRankingRecommender.java    From RankSys with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public FastRecommendation getRecommendation(int uidx, int maxLength, IntPredicate filter) {
    if (uidx == -1) {
        return new FastRecommendation(uidx, new ArrayList<>(0));
    }

    Int2DoubleMap scoresMap = getScoresMap(uidx);

    final IntDoubleTopN topN = new IntDoubleTopN(min(maxLength, scoresMap.size()));
    scoresMap.int2DoubleEntrySet().forEach(e -> {
        int iidx = e.getIntKey();
        double score = e.getDoubleValue();
        if (filter.test(iidx)) {
            topN.add(iidx, score);
        }
    });

    topN.sort();

    List<Tuple2id> items = topN.reverseStream()
            .collect(toList());

    return new FastRecommendation(uidx, items);
}
 
Example #2
Source File: VectorSimilarity.java    From RankSys with Mozilla Public License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: Int2DoubleRowTest.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Test
public void testValues() {
	Int2DoubleMap map = new Int2DoubleOpenHashMap();
	map.put(1, 0.5);
	map.put(2, 0.4);
	Int2DoubleRow row = createRow(map, 3);
	assertThat(row.values()).contains(0.5, 0.4);
}
 
Example #4
Source File: BinomialModelTest.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Tests the local (user) probability method with alpha = 0.0.
 */
@Test
public void testLocalModelAlpha00() {
    Int2DoubleMap expectedLocalP = new Int2DoubleOpenHashMap();
    expectedLocalP.put(1, 0.8);
    expectedLocalP.put(2, 0.2);
    
    checkLocalModel(0.0, expectedLocalP);
}
 
Example #5
Source File: BinomialModelTest.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Tests the global probability method (BinomialModel::p).
 */
@Test
public void testGlobalModel() {
    Int2DoubleMap expectedGlobalP = new Int2DoubleOpenHashMap();
    expectedGlobalP.put(1, 0.8);
    expectedGlobalP.put(2, 0.2);
    
    BinomialModel<Integer, Integer, Integer> bm = new BinomialModel<>(false, Stream.empty(), preferences, featureData, 0.0);
    
    assertEquals(expectedGlobalP.keySet(), bm.getFeatures());
    expectedGlobalP.forEach((f, p) -> assertEquals(p, bm.p(f), 0.0001));
}
 
Example #6
Source File: LinearUCB.java    From samantha with MIT License 5 votes vote down vote up
private RealVector extractDenseVector(int dim, LearningInstance instance) {
    Int2DoubleMap features = ((StandardLearningInstance) instance).getFeatures();
    RealVector x = MatrixUtils.createRealVector(new double[dim]);
    for (Int2DoubleMap.Entry entry : features.int2DoubleEntrySet()) {
        x.setEntry(entry.getIntKey(), entry.getDoubleValue());
    }
    return x;
}
 
Example #7
Source File: XGBoostInstance.java    From samantha with MIT License 5 votes vote down vote up
public LabeledPoint getLabeledPoint() {
    Int2DoubleMap features = instance.getFeatures();
    float[] values = new float[features.size()];
    double[] froms = features.values().toDoubleArray();
    for (int i=0; i<froms.length; i++) {
        values[i] = (float) froms[i];
    }
    return new LabeledPoint((float) instance.getLabel(), features.keySet().toIntArray(), values);
}
 
Example #8
Source File: BinomialModelTest.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Tests the local (user) probability method with alpha = 0.5.
 */
@Test
public void testLocalModelAlpha05() {
    Int2DoubleMap expectedLocalP = new Int2DoubleOpenHashMap();
    expectedLocalP.put(1, 0.9);
    expectedLocalP.put(2, 0.1);
    
    checkLocalModel(0.5, expectedLocalP);
}
 
Example #9
Source File: BinomialModelTest.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Tests the local (user) probability method with alpha = 1.0.
 */
@Test
public void testLocalModelAlpha10() {
    Int2DoubleMap expectedLocalP = new Int2DoubleOpenHashMap();
    expectedLocalP.put(1, 1.0);
    
    checkLocalModel(1.0, expectedLocalP);
}
 
Example #10
Source File: StandardFeaturizer.java    From samantha with MIT License 5 votes vote down vote up
public LearningInstance featurize(JsonNode entity, boolean update) {
    Map<String, List<Feature>> feaMap = new HashMap<>();
    for (FeatureExtractor extractor : featureExtractors) {
        feaMap.putAll(extractor.extract(entity, update,
                indexSpace));
    }
    Int2DoubleMap feas = new Int2DoubleOpenHashMap();
    for (String fea : features) {
        if (feaMap.containsKey(fea)) {
            for (Feature feature : feaMap.get(fea)) {
                feas.put(feature.getIndex(), feature.getValue());
            }
        }
    }
    double label = StandardLearningInstance.defaultLabel;
    double weight = StandardLearningInstance.defaultWeight;
    if (entity.has(labelName)) {
        label = entity.get(labelName).asDouble();
    } else if (feaMap.containsKey(labelName)) {
        label = feaMap.get(labelName).get(0).getIndex();
    }
    if (entity.has(weightName)) {
        weight = entity.get(weightName).asDouble();
    } else if (feaMap.containsKey(weightName)) {
        weight = feaMap.get(weightName).get(0).getValue();
    }
    String group = null;
    if (groupKeys != null && groupKeys.size() > 0) {
        group = FeatureExtractorUtilities.composeConcatenatedKey(entity, groupKeys);
    }
    return new StandardLearningInstance(feas, label, weight, group);
}
 
Example #11
Source File: BinomialModelTest.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
private void checkLocalModel(double alpha, Int2DoubleMap expectedLocalP) {
    BinomialModel<Integer, Integer, Integer> bm = new BinomialModel<>(false, Stream.empty(), preferences, featureData, alpha);
    BinomialModel<Integer, Integer, Integer>.UserBinomialModel ubm = bm.getModel(1);
    
    assertEquals(expectedLocalP.keySet(), ubm.getFeatures());
    expectedLocalP.forEach((f, p) -> assertEquals(p, ubm.p(f), 0.0001));
}
 
Example #12
Source File: Int2DoubleRowTest.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetOrDefault() {
	Int2DoubleMap map = new Int2DoubleOpenHashMap();
	map.put(1, 0.5);
	map.put(2, 0.4);
	Int2DoubleRow row = createRow(map, 3);
	assertThat(row.getOrDefault(0)).isEqualTo(0.0);
	assertThat(row.getOrDefault(1)).isEqualTo(0.5);
	assertThat(row.getOrDefault(2)).isEqualTo(0.4);
}
 
Example #13
Source File: Int2DoubleRowTest.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsMap() {
	Int2DoubleMap map = new Int2DoubleOpenHashMap();
	map.put(1, 0.5);
	map.put(2, 0.4);
	Int2DoubleRow row = createRow(map, 3);
	assertThat(row.asMap()).hasSize(2);
	assertThat(row.asMap()).containsEntry(1, 0.5);
	assertThat(row.asMap()).containsEntry(2, 0.4);
}
 
Example #14
Source File: ItemNeighborhoodRecommender.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #15
Source File: Int2DoubleArrayRowTest.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2DoubleRow createRow(Int2DoubleMap map, int size) {
	double[] row = new double[size];
	for (Entry entry : map.int2DoubleEntrySet()) {
		int columnKey = entry.getIntKey();
		row[columnKey] = entry.getDoubleValue();
	}
	return new Int2DoubleArrayRow(row);
}
 
Example #16
Source File: UserNeighborhoodRecommender.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * 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);
    neighborhood.getNeighbors(uidx).forEach(vs -> {
        double w = pow(vs.v2, q);
        data.getUidxPreferences(vs.v1).forEach(iv -> {
            double p = w * iv.v2;
            scoresMap.addTo(iv.v1, p);
        });
    });

    return scoresMap;
}
 
Example #17
Source File: FastEnsembleRecommender.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #18
Source File: Int2DoubleArrayRow.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Override
public Int2DoubleMap asMap() {
	Int2DoubleMap mapRow = new Int2DoubleOpenHashMap(array.length);
	for (int i = 0; i < array.length; i++) {
		double value = array[i];
		if (isNotDefault(value)) {
			mapRow.put(i, value);
		}
	}
	return mapRow;
}
 
Example #19
Source File: SlimSimilarityIndex.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
private IntCollection getSimilarRecords(int leftValue) {
	Int2DoubleMap row = similarityTable.row(leftValue).asMap();
	IntCollection result = new IntArrayList();
	Seq.seq(row.int2DoubleEntrySet())
		.filter(this::isAbove)
		.map(this::getRecords)
		.forEach(result::addAll);
	return result;
}
 
Example #20
Source File: SimilarityMapRowBuilder.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
private static WithRow with(Int2DoubleMap row) {
	return new WithRow(row);
}
 
Example #21
Source File: StandardLearningInstance.java    From samantha with MIT License 4 votes vote down vote up
public Int2DoubleMap getFeatures() {
    return features;
}
 
Example #22
Source File: StandardLearningInstance.java    From samantha with MIT License 4 votes vote down vote up
public StandardLearningInstance(Int2DoubleMap features, double label, double weight, String group) {
    super(group);
    this.features = features;
    this.weight = weight;
    this.label = label;
}
 
Example #23
Source File: Int2DoubleMapRowTest.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
@Override
protected Int2DoubleRow createRow(Int2DoubleMap map, int size) {
	return new Int2DoubleMapRow(map);
}
 
Example #24
Source File: Int2DoubleMapRow.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
@Override
public Int2DoubleMap asMap() {
	return map;
}
 
Example #25
Source File: SimilarityMapRowBuilder.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
@Override
public Int2DoubleRow create(Collection<To> similarities) {
	int size = similarities.size();
	Int2DoubleMap row = new Int2DoubleOpenHashMap(size);
	return with(row).process(similarities);
}
 
Example #26
Source File: FMInstance.java    From JavaFM with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor.
 *
 * @param target target value
 * @param features sparse feature vector
 */
public FMInstance(double target, Int2DoubleMap features) {
    this.features = features;
    this.target = target;
}
 
Example #27
Source File: FastRankingRecommender.java    From RankSys with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns a map of item-score pairs.
 *
 * @param uidx index of the user whose scores are predicted
 * @return a map of item-score pairs
 */
public abstract Int2DoubleMap getScoresMap(int uidx);
 
Example #28
Source File: Int2DoubleRowTest.java    From metanome-algorithms with Apache License 2.0 votes vote down vote up
protected abstract Int2DoubleRow createRow(Int2DoubleMap map, int size); 
Example #29
Source File: Int2Int2DoubleTable.java    From metanome-algorithms with Apache License 2.0 votes vote down vote up
Int2DoubleMap asMap();