Java Code Examples for it.unimi.dsi.fastutil.ints.IntList#size()

The following examples show how to use it.unimi.dsi.fastutil.ints.IntList#size() . 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: FeatureKnnModel.java    From samantha with MIT License 6 votes vote down vote up
private List<double[]> getNeighbors(int curIndex, IntList svdIndices,
                                    SVDFeature svdFeature,
                                    List<String> features) {
    List<double[]> raw = new ArrayList<>(svdIndices.size());
    for (int target : svdIndices) {
        if (target != curIndex && (numMatch == 0 || matchPrefixFeatures(curIndex, target, features))) {
            double[] pair = new double[2];
            pair[0] = target;
            pair[1] = svdFeature.getVectorVarByNameIndex(SVDFeatureKey.FACTORS.get(), target)
                    .cosine(svdFeature.getVectorVarByNameIndex(SVDFeatureKey.FACTORS.get(), curIndex));
            raw.add(pair);
        }
    }
    Ordering<double[]> pairDoubleOrdering = SortingUtilities.pairDoubleSecondOrdering();
    List<double[]> neighbors;
    if (reverse) {
        neighbors = pairDoubleOrdering.leastOf(raw, numNeighbors);
    } else {
        neighbors = pairDoubleOrdering.greatestOf(raw, numNeighbors);
    }
    return neighbors;
}
 
Example 2
Source File: MAPEvaluator.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
protected float measure(IntSet checkCollection, IntList rankList) {
    if (rankList.size() > size) {
        rankList = rankList.subList(0, size);
    }
    int count = 0;
    float map = 0F;
    for (int index = 0; index < rankList.size(); index++) {
        int itemIndex = rankList.get(index);
        if (checkCollection.contains(itemIndex)) {
            count++;
            map += 1F * count / (index + 1);
        }
    }
    return map / (checkCollection.size() < rankList.size() ? checkCollection.size() : rankList.size());
}
 
Example 3
Source File: GreedyReranker.java    From RankSys with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the permutation to obtain the re-ranking
 *
 * @return permutation to obtain the re-ranking
 */
public int[] rerankPermutation() {

    List<Tuple2od<I>> list = recommendation.getItems();

    IntList perm = new IntArrayList();
    IntLinkedOpenHashSet remainingI = new IntLinkedOpenHashSet();
    IntStream.range(0, list.size()).forEach(remainingI::add);

    while (!remainingI.isEmpty() && perm.size() < min(maxLength, cutoff)) {
        int bestI = selectItem(remainingI, list);

        perm.add(bestI);
        remainingI.remove(bestI);

        update(list.get(bestI));
    }

    while (perm.size() < min(maxLength, list.size())) {
        perm.add(remainingI.removeFirstInt());
    }

    return perm.toIntArray();
}
 
Example 4
Source File: NoveltyEvaluator.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate on the test set with the the list of recommended items.
 *
 * @param testMatrix      the given test set
 * @param recommendedList the list of recommended items
 * @return evaluate result
 */
@Override
protected float measure(IntSet checkCollection, IntList rankList) {
    if (rankList.size() > size) {
        rankList = rankList.subList(0, size);
    }
    float sum = 0F;
    for (int rank : rankList) {
        int count = itemCounts[rank];
        if (count > 0) {
            float probability = ((float) count) / numberOfUsers;
            float entropy = (float) -Math.log(probability);
            sum += entropy;
        }
    }
    return (float) (sum / Math.log(2F));
}
 
Example 5
Source File: BitSetUtil.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
public static BitSet convertToBitSet(IntList list) {
    BitSet set = new BitSet(list.size());
    for (int l : list) {
        set.set(l);
    }
    return set;
}
 
Example 6
Source File: AgreeSetGenerator.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
public void calculateSupersets(Set<IntList> maxSets, List<IntList> partitions) {

        List<IntList> toDelete = new LinkedList<IntList>();
        Set<IntList> toAdd = new HashSet<IntList>();
        int deleteFromPartition = -1;

        // List<IntList> remainingSets = new LinkedList<IntList>();
        // remainingSets.addAll(partition);
        for (IntList maxSet : maxSets) {
            for (IntList partition : partitions) {
                // IntList partitionCopy = new LongArrayList(partition);
                if ((maxSet.size() >= partition.size()) && (maxSet.containsAll(partition))) {
                    toAdd.remove(partition);
                    deleteFromPartition = partitions.indexOf(partition);
                    if (this.debugSysout)
                        System.out.println("MaxSet schon vorhanden");
                    break;
                }
                if ((partition.size() >= maxSet.size()) && (partition.containsAll(maxSet))) {
                    toDelete.add(maxSet);
                    if (this.debugSysout)
                        System.out.println("Neues MaxSet");
                }
                toAdd.add(partition);
            }
            if (deleteFromPartition != -1) {
                partitions.remove(deleteFromPartition);
                deleteFromPartition = -1;
            }
        }
        maxSets.removeAll(toDelete);
        maxSets.addAll(toAdd);
    }
 
Example 7
Source File: FindCoversGenerator.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
private void doRecusiveCrap(int currentAttribute, IntList currentOrdering, List<DifferenceSet> setsNotCovered,
                            IntList currentPath, List<DifferenceSet> originalDiffSet, List<FunctionalDependencyGroup2> result)
        throws CouldNotReceiveResultException, ColumnNameMismatchException {

    // Basic Case
    // FIXME
    if (!currentOrdering.isEmpty() && /* BUT */setsNotCovered.isEmpty()) {
        if (this.debugSysout)
            System.out.println("no FDs here");
        return;
    }

    if (setsNotCovered.isEmpty()) {

        List<BitSet> subSets = this.generateSubSets(currentPath);
        if (this.noOneCovers(subSets, originalDiffSet)) {
            FunctionalDependencyGroup2 fdg = new FunctionalDependencyGroup2(currentAttribute, currentPath);
            this.addFdToReceivers(fdg);
            result.add(fdg);
        } else {
            if (this.debugSysout) {
                System.out.println("FD not minimal");
                System.out.println(new FunctionalDependencyGroup2(currentAttribute, currentPath));
            }
        }

        return;
    }

    // Recusive Case
    for (int i = 0; i < currentOrdering.size(); i++) {

        List<DifferenceSet> next = this.generateNextNotCovered(currentOrdering.getInt(i), setsNotCovered);
        IntList nextOrdering = this.generateNextOrdering(next, currentOrdering, currentOrdering.getInt(i));
        IntList currentPathCopy = new IntArrayList(currentPath);
        currentPathCopy.add(currentOrdering.getInt(i));
        this.doRecusiveCrap(currentAttribute, nextOrdering, next, currentPathCopy, originalDiffSet, result);
    }

}
 
Example 8
Source File: AgreeSetGenerator.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(IntList l1, IntList l2) {

    if (l1.size() - l2.size() != 0)
        return l1.size() - l2.size();
    for (int i = 0; i < l1.size(); i++) {
        if (l1.getInt(i) == l2.getInt(i))
            continue;
        return l1.getInt(i) - l2.getInt(i);
    }
    return 0;
}
 
Example 9
Source File: AgreeSetGenerator.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
public void calculateSupersets(Set<IntList> maxSets, List<IntList> partitions) {

        List<IntList> toDelete = new LinkedList<IntList>();
        Set<IntList> toAdd = new HashSet<IntList>();
        int deleteFromPartition = -1;

        // List<IntList> remainingSets = new LinkedList<IntList>();
        // remainingSets.addAll(partition);
        for (IntList maxSet : maxSets) {
            for (IntList partition : partitions) {
                // IntList partitionCopy = new IntArrayList(partition);
                if ((maxSet.size() >= partition.size()) && (maxSet.containsAll(partition))) {
                    toAdd.remove(partition);
                    deleteFromPartition = partitions.indexOf(partition);
                    if (this.debugSysout)
                        System.out.println("MaxSet schon vorhanden");
                    break;
                }
                if ((partition.size() >= maxSet.size()) && (partition.containsAll(maxSet))) {
                    toDelete.add(maxSet);
                    if (this.debugSysout)
                        System.out.println("Neues MaxSet");
                }
                toAdd.add(partition);
            }
            if (deleteFromPartition != -1) {
                partitions.remove(deleteFromPartition);
                deleteFromPartition = -1;
            }
        }
        maxSets.removeAll(toDelete);
        maxSets.addAll(toAdd);
    }
 
Example 10
Source File: AgreeSetGenerator.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(IntList l1, IntList l2) {

    if (l1.size() - l2.size() != 0)
        return l2.size() - l1.size();
    for (int i = 0; i < l1.size(); i++) {
        if (l1.getInt(i) == l2.getInt(i))
            continue;
        return l2.getInt(i) - l1.getInt(i);
    }
    return 0;
}
 
Example 11
Source File: Statistics.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
private void increaseMeasure(final IntList measureList, final int level) {
  if (measureList.size() > level - 1) {
    final int prev = measureList.getInt(level - 1);
    measureList.set(level - 1, prev + 1);
  } else {
    final int skippedLevels = level - measureList.size() - 1;
    // add 0 for no application of the pruning rule in the skipped levels
    for (int i = 0; i < skippedLevels; i++) {
      measureList.add(0);
    }
    // add 1 for one application of the pruning rule in the new level
    measureList.add(1);
  }
}
 
Example 12
Source File: RecallEvaluator.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected float measure(IntSet checkCollection, IntList rankList) {
    if (rankList.size() > size) {
        rankList = rankList.subList(0, size);
    }
    int count = 0;
    for (int itemIndex : rankList) {
        if (checkCollection.contains(itemIndex)) {
            count++;
        }
    }
    return count / (checkCollection.size() + 0F);
}
 
Example 13
Source File: MeanSquaredError.java    From samantha with MIT License 4 votes vote down vote up
public void remove(IntList idxList, List<double[]> resps) {
    for (int i=0; i<idxList.size(); i++) {
        remove(idxList.getInt(i), resps);
    }
}
 
Example 14
Source File: MeanDivergence.java    From samantha with MIT License 4 votes vote down vote up
public void remove(IntList idxList, List<double[]> resps) {
    for (int i=0; i<idxList.size(); i++) {
        remove(idxList.getInt(i), resps);
    }
}
 
Example 15
Source File: NegativeSamplingExpander.java    From samantha with MIT License 4 votes vote down vote up
public List<ObjectNode> expand(List<ObjectNode> initialResult,
                               RequestContext requestContext) {
    int indexSize = model.getKeyMapSize(itemIndex);
    int maxVal = indexSize;
    if (maxIdx != null && maxIdx < indexSize) {
        maxVal = maxIdx;
    }
    for (ObjectNode entity : initialResult) {
        String itemStr = entity.get(itemAttr).asText();
        if (!"".equals(itemStr)) {
            String[] items = itemStr.split(separator, -1);
            IntSet trues = new IntOpenHashSet();
            for (String item : items) {
                String key = FeatureExtractorUtilities.composeKey(keyPrefix, item);
                if (model.containsKey(itemIndex, key)) {
                    trues.add(model.getIndexForKey(itemIndex, key));
                } else {
                    trues.add(0);
                }
            }
            IntList samples = getSampledIndices(trues, maxVal);
            List<String> itemArr = Lists.newArrayList(itemStr);
            List<String> labelArr = Lists.newArrayList(entity.get(labelAttr).asText());
            for (int sample : samples) {
                Map<String, String> key2val = FeatureExtractorUtilities.decomposeKey(
                        (String)model.getKeyForIndex(itemIndex, sample));
                itemArr.add(key2val.get(keyPrefix));
                labelArr.add("0");
            }
            if (fillInAttrs != null && fillInAttrs.size() > 0 && samples.size() > 0) {
                for (int i=0; i<fillInAttrs.size(); i++) {
                    String fillStr = entity.get(fillInAttrs.get(i)).asText();
                    String[] fills = fillStr.split(separator, -1);
                    List<String> fillEls = Lists.newArrayList(fillStr);
                    for (int j=0; j<samples.size(); j++) {
                        fillEls.add(fills[fills.length - 1]);
                    }
                    entity.put(fillInAttrs.get(i), StringUtils.join(fillEls, joiner));
                }
            }
            entity.put(labelAttr, StringUtils.join(labelArr, joiner));
            entity.put(itemAttr, StringUtils.join(itemArr, joiner));
        }
    }
    return initialResult;
}
 
Example 16
Source File: FindCoversGenerator.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
private IntList generateNextOrdering(List<DifferenceSet> next, IntList currentOrdering, int attribute) {

        IntList result = new IntArrayList();

        Int2IntMap counting = new Int2IntArrayMap();
        boolean seen = false;
        for (int i = 0; i < currentOrdering.size(); i++) {

            if (!seen) {
                if (currentOrdering.getInt(i) == attribute) {
                    seen = true;
                }
            } else {

                counting.put(currentOrdering.getInt(i), 0);
                for (DifferenceSet ds : next) {

                    if (ds.getAttributes().get(currentOrdering.getInt(i))) {
                        counting.put(currentOrdering.getInt(i), counting.get(currentOrdering.getInt(i)) + 1);
                    }
                }
            }
        }

        // TODO: Comperator und TreeMap --> Tommy
        while (true) {

            if (counting.size() == 0) {
                break;
            }

            int biggestAttribute = -1;
            int numberOfOcc = 0;
            for (int attr : counting.keySet()) {

                if (biggestAttribute < 0) {
                    biggestAttribute = attr;
                    numberOfOcc = counting.get(attr);
                    continue;
                }

                int tempOcc = counting.get(attr);
                if (tempOcc > numberOfOcc) {
                    numberOfOcc = tempOcc;
                    biggestAttribute = attr;
                } else if (tempOcc == numberOfOcc) {
                    if (biggestAttribute > attr) {
                        biggestAttribute = attr;
                    }
                }
            }

            if (numberOfOcc == 0) {
                break;
            }

            result.add(biggestAttribute);
            counting.remove(biggestAttribute);
        }

        return result;
    }
 
Example 17
Source File: StrippedPartitionGenerator.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
private void executeStrippedPartitionGenerationTask(int i) {

        StrippedPartition sp = new StrippedPartition(i);
        this.returnValue.add(sp);

        Map<String, IntList> toItterate = this.translationMaps.get(i);

        for (IntList it : toItterate.values()) {

            if (it.size() > 1) {
                sp.addElement(it);
            }

        }

        // Nach getaner Arbeit aufräumen
        this.translationMaps.get(i).clear();
    }
 
Example 18
Source File: IdList.java    From WorldGrower with GNU General Public License v3.0 4 votes vote down vote up
public boolean intersects(IdList otherIdList) {
	IntList copyIds = new IntArrayList(this.ids);
	copyIds.removeAll(otherIdList.ids);
	
	return (copyIds.size() != this.ids.size());
}
 
Example 19
Source File: AgreeSetGenerator.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
public Set<IntList> computeMaximumSetsAlternative(List<StrippedPartition> partitions) {

        if (this.debugSysout) {
            System.out.println("\tstartet calculation of maximal partitions");
        }
        long start = System.currentTimeMillis();

        Set<IntList> sortedPartitions = new TreeSet<IntList>(new ListComparator());
        for (StrippedPartition p : partitions) {
            sortedPartitions.addAll(p.getValues());
        }
        Iterator<IntList> it = sortedPartitions.iterator();
        Int2ObjectMap<Set<IntList>> maxSets = new Int2ObjectOpenHashMap<Set<IntList>>();
        long remainingPartitions = sortedPartitions.size();
        if (this.debugSysout) {
            System.out.println("\tNumber of Partitions: " + remainingPartitions);
        }
        if (it.hasNext()) {
            IntList actuelList = it.next();
            int minSize = actuelList.size();
            Set<IntList> set = new HashSet<IntList>();
            set.add(actuelList);
            while ((actuelList = it.next()) != null && (actuelList.size() == minSize)) {
                if (this.debugSysout) {
                    System.out.println("\tremaining: " + --remainingPartitions);
                }
                set.add(actuelList);
            }
            maxSets.put(minSize, set);
            if (actuelList != null) {
                maxSets.put(actuelList.size(), new HashSet<IntList>());
                if (this.debugSysout) {
                    System.out.println("\tremaining: " + --remainingPartitions);
                }
                this.handleList(actuelList, maxSets, true);
                while (it.hasNext()) {
                    actuelList = it.next();
                    if (this.debugSysout) {
                        System.out.println("\tremaining: " + --remainingPartitions);
                    }
                    if (!maxSets.containsKey(actuelList.size()))
                        maxSets.put(actuelList.size(), new HashSet<IntList>());
                    this.handleList(actuelList, maxSets, true);
                }
            }
        }

        long end = System.currentTimeMillis();
        if (this.debugSysout)
            System.out.println("\tTime needed: " + (end - start));

        Set<IntList> max = this.mergeResult(maxSets);
        maxSets.clear();
        sortedPartitions.clear();

        return max;
    }
 
Example 20
Source File: MeanSquaredError.java    From samantha with MIT License 4 votes vote down vote up
public void add(IntList idxList, List<double[]> resps) {
    for (int i=0; i<idxList.size(); i++) {
        add(idxList.getInt(i), resps);
    }
}