org.apache.mahout.cf.taste.impl.common.FastByIDMap Java Examples

The following examples show how to use org.apache.mahout.cf.taste.impl.common.FastByIDMap. 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: GenericRecommenderBuilderTest.java    From rival with Apache License 2.0 7 votes vote down vote up
@Test
public void testBuildDefaultRecommender() {

    RecommenderBuilder rb = new GenericRecommenderBuilder();
    FastByIDMap<PreferenceArray> userData = new FastByIDMap<PreferenceArray>();
    userData.put(1, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(1, 1, 1),
            new GenericPreference(1, 2, 1), new GenericPreference(1, 3, 1))));
    userData.put(2, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(2, 1, 1),
            new GenericPreference(2, 2, 1), new GenericPreference(2, 4, 1))));
    DataModel dm = new GenericDataModel(userData);

    Recommender rec = null;
    try {
        rec = rb.buildRecommender(dm);
    } catch (TasteException e) {
        e.printStackTrace();
    }
    assertTrue(rec instanceof RandomRecommender);
}
 
Example #2
Source File: BookRecommender.java    From Machine-Learning-in-Java with MIT License 6 votes vote down vote up
public DataModel loadInMemory() {
	// In-memory DataModel - GenericDataModels
	FastByIDMap<PreferenceArray> preferences = new FastByIDMap<PreferenceArray>();

	PreferenceArray prefsForUser1 = new GenericUserPreferenceArray(10);
	prefsForUser1.setUserID(0, 1L);
	prefsForUser1.setItemID(0, 101L);
	prefsForUser1.setValue(0, 3.0f);
	prefsForUser1.setItemID(1, 102L);
	prefsForUser1.setValue(1, 4.5F);
	preferences.put(1L, prefsForUser1); // use userID as the key
	
	//TODO: add others users

	
	// Return preferences as new data model
	DataModel dataModel = new GenericDataModel(preferences);
	
	return dataModel;

}
 
Example #3
Source File: DataModelWrapper.java    From rival with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs the wrapper using the provided model.
 *
 * @param model the model to be used to create the wrapped model
 */
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.getUsers()) {
        List<Preference> prefs = new ArrayList<Preference>();
        FastByIDMap<Long> userTimestamps = new FastByIDMap<Long>();
        timestampData.put(u, userTimestamps);
        for (Long i : model.getUserItems(u)) {
            Iterable<Long> timestamps = model.getUserItemTimestamps(u, 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.getUserItemPreference(u, i).floatValue()));
        }
        data.put(u, prefs);
    }

    FastByIDMap<PreferenceArray> userData = GenericDataModel.toDataMap(data, true);
    wrapper = new GenericDataModel(userData, timestampData);
}
 
Example #4
Source File: GenericRecommenderBuilderTest.java    From rival with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildKNNRecommender() {
    GenericRecommenderBuilder rb = new GenericRecommenderBuilder();
    FastByIDMap<PreferenceArray> userData = new FastByIDMap<PreferenceArray>();
    userData.put(1, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(1, 1, 1),
            new GenericPreference(1, 2, 1), new GenericPreference(1, 3, 1))));
    userData.put(2, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(2, 1, 1),
            new GenericPreference(2, 2, 1), new GenericPreference(2, 4, 1))));
    DataModel dm = new GenericDataModel(userData);

    Recommender rec = null;
    String recommenderType = "org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender";
    String similarityType = "org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity";
    try {
        rec = rb.buildRecommender(dm, recommenderType, similarityType);
    } catch (RecommenderException e) {
        e.printStackTrace();
    }
    assertTrue(rec instanceof GenericUserBasedRecommender);
}
 
Example #5
Source File: MemoryUserClusterStore.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public MemoryUserClusterStore(String client,int entries)
{
	logger.info("MemoryUserClusterStore for "+client+" of size "+entries);
	this.store = new FastByIDMap<>(entries);
	this.client = client;
	this.clusterGroups = new ConcurrentHashMap<>();
	this.transientClusters = new ConcurrentHashMap<>();
}
 
Example #6
Source File: MahoutDataModel.java    From rival with Apache License 2.0 5 votes vote down vote up
@Override
public void addTimestamp(Long u, Long i, Long t) {
    if (model != null) {
        throw new IllegalArgumentException("DataModel already generated. It is not possible to add more information.");
    }
    FastByIDMap<Long> prefs = null;
    if (!timestampData.containsKey(u)) {
        prefs = new FastByIDMap<Long>();
        timestampData.put(u, prefs);
    } else {
        prefs = timestampData.get(u);
    }
    prefs.put(i, t);
}
 
Example #7
Source File: ItemMemIDMigrator.java    From Machine-Learning-in-Java with MIT License 4 votes vote down vote up
public ItemMemIDMigrator() {
	this.longToString = new FastByIDMap<String>(10000);
}
 
Example #8
Source File: MahoutDataModel.java    From rival with Apache License 2.0 4 votes vote down vote up
private void generateDatamodel() {
    FastByIDMap<PreferenceArray> userData = GenericDataModel.toDataMap(data, true);
    model = new GenericDataModel(userData, timestampData);
    data = null;
    timestampData = null;
}
 
Example #9
Source File: MahoutDataModel.java    From rival with Apache License 2.0 4 votes vote down vote up
@Override
public void clear() {
    model = null;
    data = new FastByIDMap<Collection<Preference>>();
    timestampData = new FastByIDMap<FastByIDMap<Long>>();
}