org.apache.mahout.cf.taste.recommender.Recommender Java Examples
The following examples show how to use
org.apache.mahout.cf.taste.recommender.Recommender.
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 |
@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: MovieUserRecommender.java From hiped2 with Apache License 2.0 | 6 votes |
private static void recommend(String ratingsFile, int ... userIds) throws TasteException, IOException { DataModel model = new FileDataModel(new File(ratingsFile)); UserSimilarity similarity = new PearsonCorrelationSimilarity(model); UserNeighborhood neighborhood = new NearestNUserNeighborhood( 100, similarity, model); Recommender recommender = new GenericUserBasedRecommender( model, neighborhood, similarity); Recommender cachingRecommender = new CachingRecommender(recommender); for(int userId: userIds) { System.out.println("UserID " + userId); List<RecommendedItem> recommendations = cachingRecommender.recommend(userId, 2); for(RecommendedItem item: recommendations) { System.out.println(" item " + item.getItemID() + " score " + item.getValue()); } } }
Example #3
Source File: GenericRecommenderBuilderTest.java From rival with Apache License 2.0 | 6 votes |
@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 #4
Source File: BookRecommender.java From Machine-Learning-in-Java with MIT License | 5 votes |
public Recommender buildRecommender(DataModel arg0) { try { return BookRecommender.itemBased(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
Example #5
Source File: MovieUserEvaluator.java From hiped2 with Apache License 2.0 | 5 votes |
@Override public Recommender buildRecommender(DataModel model) throws TasteException { UserSimilarity similarity = new PearsonCorrelationSimilarity(model); UserNeighborhood neighborhood = new NearestNUserNeighborhood( 100, similarity, model); return new GenericUserBasedRecommender( model, neighborhood, similarity); }
Example #6
Source File: SlopeOneRecommender.java From Building-Recommendation-Engines with MIT License | 4 votes |
public static void main(String[] args) throws IOException { DataModel model = new FileDataModel(new File("data/dataset.csv")); Recommender recommender = (Recommender) new SlopeOneRecommender(); }
Example #7
Source File: MahoutRecommenderRunner.java From rival with Apache License 2.0 | 4 votes |
/** * 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; }
Example #8
Source File: GenericRecommenderBuilder.java From rival with Apache License 2.0 | 2 votes |
/** * Builds a random recommender which will recommend items from the data * model passed as a parameter. * * @param dataModel the data model * @return the recommender * @throws TasteException when the recommender is instantiated incorrectly. */ @Override public Recommender buildRecommender(final DataModel dataModel) throws TasteException { return new RandomRecommender(dataModel); }
Example #9
Source File: GenericRecommenderBuilder.java From rival with Apache License 2.0 | 2 votes |
/** * CF recommender with default parameters. * * @param dataModel the data model * @param recType the recommender type (as Mahout class) * @return the recommender * @throws RecommenderException see {@link #buildRecommender(org.apache.mahout.cf.taste.model.DataModel, java.lang.String, java.lang.String, int, int, int, java.lang.String)} */ public Recommender buildRecommender(final DataModel dataModel, final String recType) throws RecommenderException { return buildRecommender(dataModel, recType, null, DEFAULT_N, NOFACTORS, NOITER, null); }
Example #10
Source File: GenericRecommenderBuilder.java From rival with Apache License 2.0 | 2 votes |
/** * Recommender based on given recType and simType (with default parameters). * * @param dataModel the data model * @param recType the recommender type (as Mahout class) * @param simType the similarity type (as Mahout class) * @return the recommender * @throws RecommenderException see {@link #buildRecommender(org.apache.mahout.cf.taste.model.DataModel, java.lang.String, java.lang.String, int, int, int, java.lang.String)} */ public Recommender buildRecommender(final DataModel dataModel, final String recType, final String simType) throws RecommenderException { return buildRecommender(dataModel, recType, simType, DEFAULT_N, NOFACTORS, NOITER, null); }
Example #11
Source File: GenericRecommenderBuilder.java From rival with Apache License 2.0 | 2 votes |
/** * Recommender based on given recType, simType and neighborhood type. * * @param dataModel the data model * @param recType the recommender type (as Mahout class) * @param simType the similarity type (as Mahout class) * @param nbSize the neighborhood size * @return the recommender * @throws RecommenderException see {@link #buildRecommender(org.apache.mahout.cf.taste.model.DataModel, java.lang.String, java.lang.String, int, int, int, java.lang.String)} */ public Recommender buildRecommender(final DataModel dataModel, final String recType, final String simType, final int nbSize) throws RecommenderException { return buildRecommender(dataModel, recType, simType, nbSize, NOFACTORS, NOITER, null); }
Example #12
Source File: GenericRecommenderBuilder.java From rival with Apache License 2.0 | 2 votes |
/** * SVD. * * @param dataModel the data model * @param recType the recommender type (as Mahout class) * @param facType the factorizer (as Mahout class) * @param iterations number of iterations * @param factors number of factors * @return the recommender * @throws RecommenderException see * {@link #buildRecommender(org.apache.mahout.cf.taste.model.DataModel, java.lang.String, java.lang.String, int, int, int, java.lang.String)} */ public Recommender buildRecommender(final DataModel dataModel, final String recType, final String facType, final int iterations, final int factors) throws RecommenderException { return buildRecommender(dataModel, recType, null, NO_N, factors, iterations, facType); }
Example #13
Source File: CrossValidationRecSysEvaluator.java From rival with Apache License 2.0 | votes |
protected abstract Recommender buildRecommender(org.apache.mahout.cf.taste.model.DataModel model) throws TasteException;