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

The following examples show how to use it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap. 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: Int2Int2DoubleTableTest.java    From metanome-algorithms with Apache License 2.0 6 votes vote down vote up
@Test
public void testRow() {
	Int2Int2DoubleTable table = createTable(3);
	assertThat(table.row(1).asMap()).hasSize(0);
	assertThat(table.row(2).asMap()).hasSize(0);
	table.putRow(1, new Int2DoubleMapRow(Int2DoubleMaps.singleton(1, 1.0)));
	assertThat(table.row(1).asMap()).hasSize(1);
	assertThat(table.row(1).asMap()).containsEntry(1, 1.0);
	assertThat(table.row(2).asMap()).hasSize(0);
	table.putRow(1, new Int2DoubleMapRow(Int2DoubleMaps.singleton(1, 3.0)));
	assertThat(table.row(1).asMap()).hasSize(1);
	assertThat(table.row(1).asMap()).containsEntry(1, 3.0);
	assertThat(table.row(2).asMap()).hasSize(0);
	table.putRow(1,
		new Int2DoubleMapRow(new Int2DoubleOpenHashMap(ImmutableMap.of(1, 3.0, 2, 2.0))));
	assertThat(table.row(1).asMap()).hasSize(2);
	assertThat(table.row(1).asMap()).containsEntry(1, 3.0);
	assertThat(table.row(1).asMap()).containsEntry(2, 2.0);
	assertThat(table.row(2).asMap()).hasSize(0);
	table.putRow(2, new Int2DoubleMapRow(Int2DoubleMaps.singleton(1, 2.0)));
	assertThat(table.row(1).asMap()).hasSize(2);
	assertThat(table.row(1).asMap()).containsEntry(1, 3.0);
	assertThat(table.row(1).asMap()).containsEntry(2, 2.0);
	assertThat(table.row(2).asMap()).hasSize(1);
	assertThat(table.row(2).asMap()).containsEntry(1, 2.0);
}
 
Example #2
Source File: Int2Int2DoubleTableTest.java    From metanome-algorithms with Apache License 2.0 6 votes vote down vote up
@Test
public void values() {
	Int2Int2DoubleTable table = createTable(3);
	assertThat(table.values()).isEmpty();
	table.putRow(1, new Int2DoubleMapRow(Int2DoubleMaps.singleton(1, 1.0)));
	assertThat(table.values()).hasSize(1);
	assertThat(table.values()).contains(1.0);
	table.putRow(1, new Int2DoubleMapRow(Int2DoubleMaps.singleton(1, 2.0)));
	assertThat(table.values()).hasSize(1);
	assertThat(table.values()).contains(2.0);
	table.putRow(1,
		new Int2DoubleMapRow(new Int2DoubleOpenHashMap(ImmutableMap.of(1, 2.0, 2, 2.0))));
	assertThat(table.values()).hasSize(1);
	assertThat(table.values()).contains(2.0);
	table.putRow(2, new Int2DoubleMapRow(Int2DoubleMaps.singleton(1, 3.0)));
	assertThat(table.values()).hasSize(2);
	assertThat(table.values()).contains(2.0, 3.0);
}
 
Example #3
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 #4
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 #5
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 #6
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 #7
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 #8
Source File: VectorSimilarity.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
@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));
    };
}
 
Example #9
Source File: VectorSimilarity.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor. Uses maps for internal calculation.
 *
 * @param data  preference data
 * @param dense true for array-based calculations, false to map-based
 */
public VectorSimilarity(FastPreferenceData<?, ?> data, boolean dense) {
    this.data = data;
    this.dense = dense;
    if (dense) {
        this.norm2Map = null;
        this.norm2Array = new double[data.numUsers()];
        data.getUidxWithPreferences().forEach(idx -> norm2Array[idx] = getNorm2(idx));
    } else {
        this.norm2Map = new Int2DoubleOpenHashMap();
        this.norm2Array = null;
        norm2Map.defaultReturnValue(0.0);
        data.getUidxWithPreferences().forEach(idx -> norm2Map.put(idx, getNorm2(idx)));
    }
}
 
Example #10
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 #11
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 #12
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 #13
Source File: FeatureUnitNormalizer.java    From presto with Apache License 2.0 5 votes vote down vote up
public FeatureUnitNormalizer()
{
    mins = new Int2DoubleOpenHashMap();
    maxs = new Int2DoubleOpenHashMap();

    mins.defaultReturnValue(Double.POSITIVE_INFINITY);
    maxs.defaultReturnValue(Double.NEGATIVE_INFINITY);
}
 
Example #14
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 #15
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 #16
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 #17
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 #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: 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 #20
Source File: FMInstance.java    From JavaFM with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor.
 *
 * @param target target value
 * @param k indices of non-zero features
 * @param v values of non-zero features
 */
public FMInstance(double target, int[] k, double[] v) {
    this.features = new Int2DoubleOpenHashMap(k, v);
    this.target = target;
}