Java Code Examples for org.apache.mahout.cf.taste.common.TasteException#printStackTrace()

The following examples show how to use org.apache.mahout.cf.taste.common.TasteException#printStackTrace() . 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: StringItemIdFileDataModel.java    From Machine-Learning-in-Java with MIT License 6 votes vote down vote up
@Override
protected long readItemIDFromString(String value) {

	if (memIdMigtr == null) {
		memIdMigtr = new ItemMemIDMigrator();
	}

	long retValue = memIdMigtr.toLongID(value);
	if (null == memIdMigtr.toStringID(retValue)) {
		try {
			memIdMigtr.singleInit(value);
		} catch (TasteException e) {
			e.printStackTrace();
		}
	}
	return retValue;
}
 
Example 3
Source File: MahoutDataModel.java    From rival with Apache License 2.0 5 votes vote down vote up
@Override
public Double getUserItemPreference(Long u, Long i) {
    if (model == null) {
        generateDatamodel();
    }
    try {
        return model.getPreferenceValue(u, i) * 1.0;
    } catch (TasteException e) {
        e.printStackTrace();
    }
    return Double.NaN;
}
 
Example 4
Source File: MahoutDataModel.java    From rival with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Long> getUserItemTimestamps(Long u, Long i) {
    if (model == null) {
        generateDatamodel();
    }
    List<Long> t = new ArrayList<>();
    try {
        t.add(model.getPreferenceTime(u, i));
    } catch (TasteException e) {
        e.printStackTrace();
    }
    return t;
}
 
Example 5
Source File: MahoutDataModel.java    From rival with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Long> getUserItems(Long u) {
    if (model == null) {
        generateDatamodel();
    }
    try {
        return model.getItemIDsFromUser(u);
    } catch (TasteException e) {
        e.printStackTrace();
    }
    return Collections.emptySet();
}
 
Example 6
Source File: MahoutRecommenderRunner.java    From rival with Apache License 2.0 4 votes vote down vote up
/**
 * Runs a Mahout recommender using the provided datamodels and the
 * previously provided properties.
 *
 * @param opts see
 * {@link net.recommenders.rival.recommend.frameworks.AbstractRunner.RUN_OPTIONS}
 * @param trainingModel model to be used to train the recommender.
 * @param testModel model to be used to test the recommender.
 * @return nothing when opts is
 * {@link net.recommenders.rival.recommend.frameworks.AbstractRunner.RUN_OPTIONS#OUTPUT_RECS},
 * otherwise, when opts is
 * {@link net.recommenders.rival.recommend.frameworks.AbstractRunner.RUN_OPTIONS#RETURN_RECS}
 * or
 * {@link net.recommenders.rival.recommend.frameworks.AbstractRunner.RUN_OPTIONS#RETURN_AND_OUTPUT_RECS}
 * it returns the predictions
 * @throws TasteException when there is a problem with the Mahout
 * recommender
 * @throws RecommenderException when recommender cannot be instantiated
 * properly
 */
public TemporalDataModelIF<Long, Long> runMahoutRecommender(final RUN_OPTIONS opts, final DataModel trainingModel, final DataModel testModel)
        throws RecommenderException, TasteException {
    if (isAlreadyRecommended()) {
        return null;
    }

    GenericRecommenderBuilder grb = new GenericRecommenderBuilder();

    if (getProperties().containsKey(RecommendationRunner.NEIGHBORHOOD) && getProperties().getProperty(RecommendationRunner.NEIGHBORHOOD).equals("-1")) {
        getProperties().setProperty(RecommendationRunner.NEIGHBORHOOD, Math.round(Math.sqrt(trainingModel.getNumItems())) + "");
    }
    if (getProperties().containsKey(RecommendationRunner.FACTORS) && getProperties().getProperty(RecommendationRunner.FACTORS).equals("-1")) {
        getProperties().setProperty(RecommendationRunner.FACTORS, Math.round(Math.sqrt(trainingModel.getNumItems())) + "");
    }

    Recommender recommender = null;
    if (getProperties().getProperty(RecommendationRunner.FACTORS) == null) {
        recommender = grb.buildRecommender(
                trainingModel,
                getProperties().getProperty(RecommendationRunner.RECOMMENDER),
                getProperties().getProperty(RecommendationRunner.SIMILARITY),
                Integer.parseInt(getProperties().getProperty(RecommendationRunner.NEIGHBORHOOD)));
    }
    if (getProperties().getProperty(RecommendationRunner.FACTORS) != null) {
        recommender = grb.buildRecommender(
                trainingModel,
                getProperties().getProperty(RecommendationRunner.RECOMMENDER),
                getProperties().getProperty(RecommendationRunner.FACTORIZER),
                DEFAULT_ITERATIONS,
                Integer.parseInt(getProperties().getProperty(RecommendationRunner.FACTORS)));
    }

    LongPrimitiveIterator users = testModel.getUserIDs();

    TemporalDataModelIF<Long, Long> model = null;
    switch (opts) {
        case RETURN_AND_OUTPUT_RECS:
        case RETURN_RECS:
            model = new TemporalDataModel<>();
            break;
        default:
            model = null;
    }
    String name = null;
    switch (opts) {
        case RETURN_AND_OUTPUT_RECS:
        case OUTPUT_RECS:
            name = getFileName();
            break;
        default:
            name = null;
    }
    boolean createFile = true;
    while (users.hasNext()) {
        long u = users.nextLong();
        try {
            List<RecommendedItem> items = recommender.recommend(u, trainingModel.getNumItems());
            //
            List<RecommenderIO.Preference<Long, Long>> prefs = new ArrayList<>();
            for (RecommendedItem i : items) {
                prefs.add(new RecommenderIO.Preference<>(u, i.getItemID(), i.getValue()));
            }
            //
            RecommenderIO.writeData(u, prefs, getPath(), name, !createFile, model);
            createFile = false;
        } catch (TasteException e) {
            e.printStackTrace();
        }
    }
    return model;
}