Example usage for org.apache.mahout.cf.taste.impl.neighborhood ThresholdUserNeighborhood ThresholdUserNeighborhood

List of usage examples for org.apache.mahout.cf.taste.impl.neighborhood ThresholdUserNeighborhood ThresholdUserNeighborhood

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.impl.neighborhood ThresholdUserNeighborhood ThresholdUserNeighborhood.

Prototype

public ThresholdUserNeighborhood(double threshold, UserSimilarity userSimilarity, DataModel dataModel,
        double samplingRate) 

Source Link

Usage

From source file:org.easyrec.plugin.mahout.MahoutBooleanGenerator.java

License:Open Source License

@Override
protected void doExecute(ExecutionControl executionControl, MahoutBooleanGeneratorStats stats)
        throws Exception {
    // when doExecute() is called, the generator has been initialized with the configuration we should use

    Date execution = new Date();

    MahoutBooleanGeneratorConfig config = getConfiguration();

    TypeMappingService typeMappingService = (TypeMappingService) super.getTypeMappingService();
    ItemAssocService itemAssocService = getItemAssocService();

    executionControl.updateProgress("initialize DataModel");
    DataModel easyrecDataModel = new EasyrecDataModel(config.getTenantId(),
            typeMappingService.getIdOfActionType(config.getTenantId(), config.getActionType()), false,
            mahoutDataModelMappingDAO);/*from ww w  .j a  va 2s.  co  m*/

    if (config.getCacheDataInMemory() == 1) {
        executionControl.updateProgress("initialize EasyrecInMemoryDataModel");
        easyrecDataModel = new EasyrecInMemoryDataModel(easyrecDataModel);
    }

    /*TanimotoCoefficientSimilarity is intended for "binary" data sets  where a user either expresses a generic "yes" preference for an item or has no preference.*/
    UserSimilarity userSimilarity = null;

    switch (config.getUserSimilarityMethod()) {
    case 1:
        executionControl.updateProgress("using LogLikelihoodSimilarity as UserSimilarity");
        userSimilarity = new LogLikelihoodSimilarity(easyrecDataModel);
        break;
    case 2:
        executionControl.updateProgress("using TanimotoCoefficientSimilarity as UserSimilarity");
        userSimilarity = new TanimotoCoefficientSimilarity(easyrecDataModel);
        break;
    case 3:
        executionControl.updateProgress("using SpearmanCorrelationSimilarity as UserSimilarity");
        userSimilarity = new SpearmanCorrelationSimilarity(easyrecDataModel);
        break;
    case 4:
        executionControl.updateProgress("using CityBlockSimilarity as UserSimilarity");
        userSimilarity = new CityBlockSimilarity(easyrecDataModel);
        break;
    }

    /*ThresholdUserNeighborhood is preferred in situations where we go in for a  similarity measure between neighbors and not any number*/
    UserNeighborhood neighborhood = null;
    Double userNeighborhoodSamplingRate = config.getUserNeighborhoodSamplingRate();
    Double neighborhoodThreshold = config.getUserNeighborhoodThreshold();
    int neighborhoodSize = config.getUserNeighborhoodSize();
    double userNeighborhoodMinSimilarity = config.getUserNeighborhoodMinSimilarity();

    switch (config.getUserNeighborhoodMethod()) {
    case 1:
        executionControl.updateProgress("using ThresholdUserNeighborhood as UserNeighborhood");
        neighborhood = new ThresholdUserNeighborhood(neighborhoodThreshold, userSimilarity, easyrecDataModel,
                userNeighborhoodSamplingRate);
        break;
    case 2:
        executionControl.updateProgress("using NearestNUserNeighborhood as UserNeighborhood");
        neighborhood = new NearestNUserNeighborhood(neighborhoodSize, userNeighborhoodMinSimilarity,
                userSimilarity, easyrecDataModel, userNeighborhoodSamplingRate);
        break;
    }
    /*GenericBooleanPrefUserBasedRecommender is appropriate for use when no notion of preference value exists in the data. */
    executionControl.updateProgress("using GenericBooleanPrefUserBasedRecommender as Recommender");
    Recommender recommender = new GenericBooleanPrefUserBasedRecommender(easyrecDataModel, neighborhood,
            userSimilarity);

    itemTypeDAO.insertOrUpdate(config.getTenantId(), "USER", true);

    Integer assocType = typeMappingService.getIdOfAssocType(config.getTenantId(), config.getAssociationType());
    Integer userType = typeMappingService.getIdOfItemType(config.getTenantId(), "USER");
    Integer sourceType = typeMappingService.getIdOfSourceType(config.getTenantId(), getId().toString());
    Integer viewType = typeMappingService.getIdOfViewType(config.getTenantId(), config.getViewType());

    stats.setNumberOfItems(easyrecDataModel.getNumItems());

    int totalSteps = easyrecDataModel.getNumUsers();
    int currentStep = 1;
    for (LongPrimitiveIterator it = easyrecDataModel.getUserIDs(); it.hasNext()
            && !executionControl.isAbortRequested();) {
        executionControl.updateProgress(currentStep++, totalSteps, "Saving Recommendations...");
        long userId = it.nextLong();
        List<RecommendedItem> recommendations = recommender.recommend(userId, config.getNumberOfRecs());

        if (recommendations.isEmpty()) {
            logger.debug("User " + userId + " : no recommendations");
        }

        // print the list of recommendations for each
        for (RecommendedItem recommendedItem : recommendations) {
            logger.debug("User " + userId + " : " + recommendedItem);

            Integer itemToId = (int) recommendedItem.getItemID();
            Integer itemToType = itemDAO.getItemTypeIdOfItem(config.getTenantId(), itemToId);

            ItemVO<Integer, Integer> fromItem = new ItemVO<Integer, Integer>(config.getTenantId(), (int) userId,
                    userType);
            Double recommendationStrength = (double) recommendedItem.getValue();
            ItemVO<Integer, Integer> toItem = new ItemVO<Integer, Integer>(config.getTenantId(), itemToId,
                    itemToType);

            ItemAssocVO<Integer, Integer> itemAssoc = new ItemAssocVO<Integer, Integer>(config.getTenantId(),
                    fromItem, assocType, recommendationStrength, toItem, sourceType, "Mahout Boolean Generator",
                    viewType, null, execution);

            itemAssocService.insertOrUpdateItemAssoc(itemAssoc);
            stats.incNumberOfRulesCreated();
        }
    }

}

From source file:org.zaizi.mahout.config.ClassNameNeighborHoodConfiguration.java

License:Open Source License

public UserNeighborhood getNeighborhood(DataModel dataModel, UserSimilarity userSimilarity)
        throws TasteException {

    UserNeighborhood neighborhood = null;
    if (ThresholdUserNeighborhood.class.getName().equals(neighborHoodClassName)) {
        if (samplingRate > 0d) {
            neighborhood = new ThresholdUserNeighborhood(threshold, userSimilarity, dataModel, samplingRate);
        } else {/*from   w ww .j  a v a  2  s.  c  o m*/
            neighborhood = new ThresholdUserNeighborhood(threshold, userSimilarity, dataModel);
        }
    } else {
        if (samplingRate > 0d) {
            neighborhood = new NearestNUserNeighborhood(neighborhoodSize, minSimilarity, userSimilarity,
                    dataModel, samplingRate);
        } else {
            neighborhood = new NearestNUserNeighborhood(neighborhoodSize, minSimilarity, userSimilarity,
                    dataModel);
        }
    }
    return new CachingUserNeighborhood(neighborhood, dataModel);
}