Example usage for org.apache.mahout.cf.taste.impl.model GenericDataModel GenericDataModel

List of usage examples for org.apache.mahout.cf.taste.impl.model GenericDataModel GenericDataModel

Introduction

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

Prototype

public GenericDataModel(FastByIDMap<PreferenceArray> userData, FastByIDMap<FastByIDMap<Long>> timestamps) 

Source Link

Document

Creates a new GenericDataModel from the given users (and their preferences).

Usage

From source file:net.recommenders.rival.recommend.frameworks.mahout.DataModelWrapper.java

License:Apache License

/**
 * Constructs the wrapper using the provided model.
 *
 * @param model the model to be used to create the wrapped model
 *///from w  ww . j  ava2  s. c o  m
public DataModelWrapper(final net.recommenders.rival.core.TemporalDataModelIF<Long, Long> model) {
    FastByIDMap<Collection<Preference>> data = new FastByIDMap<Collection<Preference>>();
    FastByIDMap<FastByIDMap<Long>> timestampData = new FastByIDMap<FastByIDMap<Long>>();
    for (Long u : model.getUserItemPreferences().keySet()) {
        List<Preference> prefs = new ArrayList<Preference>();
        FastByIDMap<Long> userTimestamps = new FastByIDMap<Long>();
        timestampData.put(u, userTimestamps);
        for (Long i : model.getUserItemPreferences().get(u).keySet()) {
            Set<Long> timestamps = model.getUserItemTimestamps().get(u).get(i);
            long t = -1;
            if (timestamps != null) {
                for (Long tt : timestamps) {
                    t = tt;
                    break;
                }
            }
            userTimestamps.put(i, t);
            prefs.add(new GenericPreference(u, i, model.getUserItemPreferences().get(u).get(i).floatValue()));
        }
        data.put(u, prefs);
    }

    FastByIDMap<PreferenceArray> userData = GenericDataModel.toDataMap(data, true);
    wrapper = new GenericDataModel(userData, timestampData);
}

From source file:org.easyrec.mahout.model.EasyrecInMemoryDataModel.java

License:Open Source License

private void intializeDelegate() {
    try {/*from  ww  w.j  ava 2  s. co m*/
        //iterate over all user ids
        LongPrimitiveIterator it = easyrecDataModelDelegate.getUserIDs();
        FastByIDMap<PreferenceArray> preferences = new FastByIDMap<PreferenceArray>(
                easyrecDataModelDelegate.getNumUsers());
        FastByIDMap<FastByIDMap<Long>> timestamps = new FastByIDMap<FastByIDMap<Long>>(
                easyrecDataModelDelegate.getNumUsers());
        while (it.hasNext()) {
            Long userId = it.next();
            //get preferences for each user
            PreferenceArray prefs = easyrecDataModelDelegate.getPreferencesFromUser(userId);
            preferences.put(userId, prefs);
            //get preference times for each user
            FastByIDMap<Long> timestampsForUser = new FastByIDMap<Long>(prefs.getIDs().length);
            for (Long itemId : prefs.getIDs()) {
                timestampsForUser.put(itemId, easyrecDataModelDelegate.getPreferenceTime(userId, itemId));
            }
            timestamps.put(userId, timestampsForUser);
        }
        //generate GenericDataModel
        setMaxPreference(easyrecDataModelDelegate.getMaxPreference());
        setMinPreference(easyrecDataModelDelegate.getMinPreference());
        this.inMemoryDelegate = new GenericDataModel(preferences, timestamps);
    } catch (TasteException e) {
        logger.warn("caught exception while reading preference data", e);
    }
}