com.jstarcraft.ai.data.DataInstance Java Examples

The following examples show how to use com.jstarcraft.ai.data.DataInstance. 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: LDCCModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = 0F;
    for (Entry<Float, Integer> term : scoreIndexes.entrySet()) {
        float score = term.getKey();
        int scoreIndex = term.getValue();
        float probability = 0F; // P(r|u,v)=\sum_{i,j} P(r|i,j)P(i|u)P(j|v)
        for (int userTopic = 0; userTopic < numberOfUserTopics; userTopic++) {
            for (int itemTopic = 0; itemTopic < numberOfItemTopics; itemTopic++) {
                probability += rateTopicProbabilities[userTopic][itemTopic][scoreIndex] * userTopicProbabilities.getValue(userIndex, userTopic) * itemTopicProbabilities.getValue(itemIndex, itemTopic);
            }
        }
        value += score * probability;
    }
    instance.setQuantityMark(value);
}
 
Example #2
Source File: FISMAUCModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    float bias = itemBiases.getValue(itemIndex);
    float sum = 0F;
    int count = 0;
    for (VectorScalar term : scoreMatrix.getRowVector(userIndex)) {
        int compareIndex = term.getIndex();
        // for test, i and j will be always unequal as j is unrated
        if (compareIndex != itemIndex) {
            DenseVector compareVector = userFactors.getRowVector(compareIndex);
            DenseVector itemVector = itemFactors.getRowVector(itemIndex);
            sum += scalar.dotProduct(compareVector, itemVector).getValue();
            count++;
        }
    }
    sum *= (float) (count > 0 ? Math.pow(count, -alpha) : 0F);
    instance.setQuantityMark(bias + sum);
}
 
Example #3
Source File: BHFreeRankingModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = 0F;
    for (Entry<Float, Integer> entry : scoreIndexes.entrySet()) {
        float score = entry.getKey();
        float probability = 0F;
        for (int userTopic = 0; userTopic < userTopicSize; userTopic++) {
            for (int itemTopic = 0; itemTopic < itemTopicSize; itemTopic++) {
                probability += user2TopicProbabilities.getValue(userIndex, userTopic) * userTopic2ItemTopicProbabilities.getValue(userTopic, itemTopic) * userTopic2ItemTopicItemSums[userTopic][itemTopic][itemIndex] * userTopic2ItemTopicScoreProbabilities[userTopic][itemTopic][entry.getValue()];
            }
        }
        value += score * probability;
    }
    instance.setQuantityMark(value);
}
 
Example #4
Source File: SocialModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    super.prepare(configuration, model, space);

    socialRegularization = configuration.getFloat("recommender.social.regularization", 0.01f);
    // social path for the socialMatrix
    // TODO 此处是不是应该使用context.getSimilarity().getSimilarityMatrix();代替?
    DataModule socialModel = space.getModule("social");
    // TODO 此处需要重构,trusterDimension与trusteeDimension要配置
    coefficientField = configuration.getString("data.model.fields.coefficient");
    trusterDimension = socialModel.getQualityInner(userField) + 0;
    trusteeDimension = socialModel.getQualityInner(userField) + 1;
    coefficientDimension = socialModel.getQuantityInner(coefficientField);
    HashMatrix matrix = new HashMatrix(true, userSize, userSize, new Long2FloatRBTreeMap());
    for (DataInstance instance : socialModel) {
        matrix.setValue(instance.getQualityFeature(trusterDimension), instance.getQualityFeature(trusteeDimension), instance.getQuantityFeature(coefficientDimension));
    }
    socialMatrix = SparseMatrix.valueOf(userSize, userSize, matrix);
}
 
Example #5
Source File: DataSorterTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void testSort() {
    DataModule module = getDataModule();
    DataSorter sorter = new DataSorter() {

        @Override
        public int sort(DataInstance left, DataInstance right) {
            return right.getQualityFeature(0) - left.getQualityFeature(0);
        }

    };
    module = sorter.sort(module);
    for (int index = 0; index < instanceCapacity; index++) {
        Assert.assertEquals(instanceCapacity - 1 - index, module.getInstance(index).getQualityFeature(0));
    }
}
 
Example #6
Source File: DataSplitter.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
/**
 * 划分
 * 
 * @param module
 * @param number
 * @return
 */
default ReferenceModule[] split(DataModule module, int number) {
    int size = module.getSize();
    int maximum = size;
    int minimum = 1000;
    if (maximum < minimum) {
        maximum = minimum;
    }
    IntegerArray[] references = new IntegerArray[number];
    for (int index = 0; index < number; index++) {
        references[index] = new IntegerArray(minimum, maximum);
    }
    int cursor = 0;
    DataInstance instance = module.getInstance(cursor);
    while (cursor < size) {
        instance.setCursor(cursor);
        references[split(instance) % number].associateData(cursor);
        cursor++;
    }
    ReferenceModule[] modules = new ReferenceModule[number];
    for (int index = 0; index < number; index++) {
        modules[index] = new ReferenceModule(references[index], module);
    }
    return modules;
}
 
Example #7
Source File: AspectModelRatingModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = 0F;
    float denominator = 0F;
    for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
        float weight = topicProbabilities.getValue(topicIndex) * userProbabilities.getValue(topicIndex, userIndex) * itemProbabilities.getValue(topicIndex, itemIndex);
        denominator += weight;
        value += weight * meanProbabilities.getValue(topicIndex);
    }
    value = value / denominator;
    if (Float.isNaN(value)) {
        value = meanScore;
    }
    instance.setQuantityMark(value);
}
 
Example #8
Source File: DataSplitterTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void testSplit() {
    DataModule module = getDataModule();
    DataSplitter splitter = new DataSplitter() {

        @Override
        public int split(DataInstance instance) {
            return instance.getQualityFeature(0);
        }

    };
    int number = 5;
    DataModule[] modules = splitter.split(module, number);
    for (int index = 0; index < number; index++) {
        module = modules[index];
        Assert.assertEquals(2, module.getSize());
        Assert.assertEquals(index, module.getInstance(0).getQualityFeature(0));
        Assert.assertEquals(index + number, module.getInstance(1).getQualityFeature(0));
    }

}
 
Example #9
Source File: ItemClusterModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = 0F;
    for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
        float topicProbability = itemTopicProbabilities.getValue(itemIndex, topicIndex); // probability
        float topicValue = 0F;
        for (Entry<Float, Integer> entry : scoreIndexes.entrySet()) {
            float score = entry.getKey();
            float probability = topicScoreMatrix.getValue(topicIndex, entry.getValue());
            topicValue += score * probability;
        }
        value += topicProbability * topicValue;
    }
    instance.setQuantityMark(value);
}
 
Example #10
Source File: DataSelector.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
/**
 * 选择
 * 
 * @param module
 * @return
 */
default ReferenceModule select(DataModule module) {
    int size = module.getSize();
    if (size == 0) {
        return new ReferenceModule(module);
    }
    int maximum = size;
    int minimum = 1000;
    if (maximum < minimum) {
        maximum = minimum;
    }
    IntegerArray reference = new IntegerArray(minimum, maximum);
    int cursor = 0;
    DataInstance instance = module.getInstance(cursor);
    while (cursor < size) {
        instance.setCursor(cursor);
        if (select(instance)) {
            reference.associateData(cursor);
        }
        cursor++;
    }
    return new ReferenceModule(reference, module);
}
 
Example #11
Source File: BHFreeRatingModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = 0F, probabilities = 0F;
    for (Entry<Float, Integer> entry : scoreIndexes.entrySet()) {
        float score = entry.getKey();
        float probability = 0F;
        for (int userTopic = 0; userTopic < userTopicSize; userTopic++) {
            for (int itemTopic = 0; itemTopic < itemTopicSize; itemTopic++) {
                probability += user2TopicProbabilities.getValue(userIndex, userTopic) * userTopic2ItemTopicProbabilities.getValue(userTopic, itemTopic) * userTopic2ItemTopicScoreProbabilities[userTopic][itemTopic][entry.getValue()];
            }
        }
        value += score * probability;
        probabilities += probability;
    }
    instance.setQuantityMark(value / probabilities);
}
 
Example #12
Source File: FMALSModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    super.prepare(configuration, model, space);
    // init Q
    // TODO 此处为rateFactors
    actionFactors = DenseMatrix.valueOf(actionSize, factorSize);

    // construct training appender matrix
    HashMatrix table = new HashMatrix(true, actionSize, featureSize, new Long2FloatRBTreeMap());
    int index = 0;
    int order = marker.getQualityOrder();
    for (DataInstance sample : model) {
        int count = 0;
        for (int orderIndex = 0; orderIndex < order; orderIndex++) {
            table.setValue(index, count + sample.getQualityFeature(orderIndex), 1F);
            count += dimensionSizes[orderIndex];
        }
        index++;
    }
    // TODO 考虑重构(.此处似乎就是FactorizationMachineRecommender.getFeatureVector);
    featureMatrix = SparseMatrix.valueOf(actionSize, featureSize, table);
}
 
Example #13
Source File: FISMRMSEModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    float bias = userBiases.getValue(userIndex) + itemBiases.getValue(itemIndex);
    float sum = 0F;
    int count = 0;
    for (VectorScalar term : scoreMatrix.getRowVector(userIndex)) {
        int index = term.getIndex();
        // for test, i and j will be always unequal as j is unrated
        if (index != itemIndex) {
            DenseVector userVector = userFactors.getRowVector(index);
            DenseVector itemVector = itemFactors.getRowVector(itemIndex);
            sum += scalar.dotProduct(userVector, itemVector).getValue();
            count++;
        }
    }
    sum *= (float) (count > 0 ? Math.pow(count, -alpha) : 0F);
    instance.setQuantityMark(bias + sum);
}
 
Example #14
Source File: ArrayInstance.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
public void copyInstance(DataInstance instance) {
    for (int index = 0; index < qualityOrder; index++) {
        this.qualityFeatures[index] = DataInstance.defaultInteger;
    }
    for (int index = 0; index < quantityOrder; index++) {
        this.quantityFeatures[index] = DataInstance.defaultFloat;
    }
    instance.iterateQualityFeatures((index, value) -> {
        this.qualityFeatures[index] = value;
    });
    instance.iterateQuantityFeatures((index, value) -> {
        this.quantityFeatures[index] = value;
    });
    this.qualityMark = instance.getQualityMark();
    this.quantityMark = instance.getQuantityMark();
    this.weight = instance.getWeight();
}
 
Example #15
Source File: RatingTask.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
protected FloatList recommend(Model recommender, int userIndex) {
    ReferenceModule testModule = testModules[userIndex];
    ArrayInstance copy = new ArrayInstance(testMarker.getQualityOrder(), testMarker.getQuantityOrder());
    List<Integer2FloatKeyValue> rateList = new ArrayList<>(testModule.getSize());
    for (DataInstance instance : testModule) {
        copy.copyInstance(instance);
        recommender.predict(copy);
        rateList.add(new Integer2FloatKeyValue(copy.getQualityFeature(itemDimension), copy.getQuantityMark()));
    }

    FloatList recommendList = new FloatArrayList(rateList.size());
    for (Integer2FloatKeyValue keyValue : rateList) {
        recommendList.add(keyValue.getValue());
    }
    return recommendList;
}
 
Example #16
Source File: CCDModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    float score = scalar.dotProduct(userFactors.getRowVector(userIndex), itemFactors.getRowVector(itemIndex)).getValue();
    if (score == 0F) {
        score = meanScore;
    }
    instance.setQuantityMark(score);
}
 
Example #17
Source File: ItemBigramModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    List<Integer> items = userItemMap.get(userIndex);
    int scoreIndex = items == null ? itemSize : items.get(items.size() - 1); // last
    // rated
    // item
    float value = 0F;
    for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
        value += userTopicProbabilities.getValue(userIndex, topicIndex) * topicItemBigramProbabilities[topicIndex][scoreIndex][itemIndex];
    }

    instance.setQuantityMark(value);
}
 
Example #18
Source File: FactorizationMachineModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    DefaultScalar scalar = DefaultScalar.getInstance();
    // TODO 暂时不支持连续特征,考虑将连续特征离散化.
    MathVector featureVector = getFeatureVector(instance);
    instance.setQuantityMark(predict(scalar, featureVector));
}
 
Example #19
Source File: GPLSAModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float sum = 0F;
    for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
        sum += userTopicProbabilities.getValue(userIndex, topicIndex) * itemMus.getValue(itemIndex, topicIndex);
    }
    instance.setQuantityMark(userMus.getValue(userIndex) + userSigmas.getValue(userIndex) * sum);
}
 
Example #20
Source File: BPMFModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    float value = 0F;
    for (int iterationStep = 0; iterationStep < epocheSize - 1; iterationStep++) {
        DenseVector userVector = userMatrixes[iterationStep].getRowVector(userIndex);
        DenseVector itemVector = itemMatrixes[iterationStep].getRowVector(itemIndex);
        value = (value * (iterationStep) + meanScore + scalar.dotProduct(userVector, itemVector).getValue()) / (iterationStep + 1);
    }
    instance.setQuantityMark(value);
}
 
Example #21
Source File: RankingTask.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
protected IntSet check(int userIndex) {
    ReferenceModule testModule = testModules[userIndex];
    IntSet itemSet = new IntOpenHashSet();
    for (DataInstance instance : testModule) {
        itemSet.add(instance.getQualityFeature(itemDimension));
    }
    return itemSet;
}
 
Example #22
Source File: IRRGModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float score = super.predict(userIndex, itemIndex);
    score = LogisticUtility.getValue(score);
    score = minimumScore + score * (maximumScore - minimumScore);
    instance.setQuantityMark(score);
}
 
Example #23
Source File: DeepCrossModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = outputData.getValue(userIndex, itemIndex);
    instance.setQuantityMark(value);
}
 
Example #24
Source File: RankCDModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    instance.setQuantityMark(scalar.dotProduct(userFactors.getRowVector(userIndex), itemFactors.getRowVector(itemIndex)).getValue());
}
 
Example #25
Source File: PLSAModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    DenseVector userVector = userTopicProbabilities.getRowVector(userIndex);
    DenseVector itemVector = topicItemProbabilities.getColumnVector(itemIndex);
    instance.setQuantityMark(scalar.dotProduct(userVector, itemVector).getValue());
}
 
Example #26
Source File: AspectModelRankingModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = 0F;
    for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
        value += userProbabilities.getValue(topicIndex, userIndex) * itemProbabilities.getValue(topicIndex, itemIndex) * topicProbabilities.getValue(topicIndex);
    }
    instance.setQuantityMark(value);
}
 
Example #27
Source File: ArrayInstance.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
public ArrayInstance(int qualityOrder, int quantityOrder) {
    this.qualityOrder = qualityOrder;
    this.quantityOrder = quantityOrder;
    this.qualityFeatures = new int[qualityOrder];
    this.quantityFeatures = new float[quantityOrder];
    for (int dimension = 0; dimension < qualityOrder; dimension++) {
        this.qualityFeatures[dimension] = DataInstance.defaultInteger;
    }
    for (int dimension = 0; dimension < quantityOrder; dimension++) {
        this.quantityFeatures[dimension] = DataInstance.defaultFloat;
    }
    this.qualityMark = defaultInteger;
    this.quantityMark = defaultFloat;
    this.weight = DataInstance.defaultWeight;
}
 
Example #28
Source File: HMMModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float score = 0F;
    for (int state = 0; state < numberOfStates; state++) {
        score += Math.exp(norms.getValue(userIndex, state) - viewProbabilities.getValue(state, itemIndex));
    }
    score = 1F - score;
    instance.setQuantityMark(score);
}
 
Example #29
Source File: SLIMModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
/**
 * predict a specific ranking score for user userIdx on item itemIdx.
 *
 * @param userIndex user index
 * @param itemIndex item index
 * @return predictive ranking score for user userIdx on item itemIdx
 * @throws ModelException if error occurs
 */
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    int[] neighbors = itemNeighbors[itemIndex];
    if (neighbors == null) {
        instance.setQuantityMark(0F);
        return;
    }
    instance.setQuantityMark(predict(userIndex, itemIndex, neighbors, -1));
}
 
Example #30
Source File: HashInstance.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
public HashInstance(int qualityOrder, int quantityOrder) {
    this.qualityOrder = qualityOrder;
    this.quantityOrder = quantityOrder;
    this.qualityFeatures = new Int2IntRBTreeMap();
    this.qualityFeatures.defaultReturnValue(DataInstance.defaultInteger);
    this.quantityFeatures = new Int2FloatRBTreeMap();
    this.quantityFeatures.defaultReturnValue(DataInstance.defaultFloat);
    this.qualityMark = defaultInteger;
    this.quantityMark = defaultFloat;
    this.weight = DataInstance.defaultWeight;
}