org.apache.mahout.cf.taste.model.DataModel Java Examples

The following examples show how to use org.apache.mahout.cf.taste.model.DataModel. 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: 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 #3
Source File: MovieUserRecommender.java    From hiped2 with Apache License 2.0 6 votes vote down vote up
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 #4
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 #5
Source File: UserbasedRecommender.java    From Building-Recommendation-Engines with MIT License 5 votes vote down vote up
public static void main( String[] args ) throws IOException, TasteException
{
	//user based recommender model
	DataModel model = new FileDataModel(new File("data/dataset.csv"));    	
	UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
	UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
	UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
	List<RecommendedItem> recommendations = recommender.recommend(2, 3);
	for (RecommendedItem recommendation : recommendations) {
	  System.out.println(recommendation);
	}
}
 
Example #6
Source File: MahoutRecommenderRunner.java    From rival with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the recommender using the provided datamodels.
 *
 * @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 see
 * {@link #runMahoutRecommender(net.recommenders.rival.recommend.frameworks.AbstractRunner.RUN_OPTIONS, org.apache.mahout.cf.taste.model.TemporalDataModelIF, org.apache.mahout.cf.taste.model.TemporalDataModelIF)}
 * @throws RecommenderException see null     {@link #runMahoutRecommender(net.recommenders.rival.recommend.frameworks.AbstractRunner.RUN_OPTIONS,
 * org.apache.mahout.cf.taste.model.DataModel, org.apache.mahout.cf.taste.model.DataModel)}
 * @throws TasteException see null     {@link #runMahoutRecommender(net.recommenders.rival.recommend.frameworks.AbstractRunner.RUN_OPTIONS,
 * org.apache.mahout.cf.taste.model.DataModel, org.apache.mahout.cf.taste.model.DataModel)}
 */
@Override
public TemporalDataModelIF<Long, Long> run(final RUN_OPTIONS opts,
        final net.recommenders.rival.core.TemporalDataModelIF<Long, Long> trainingModel,
        final net.recommenders.rival.core.TemporalDataModelIF<Long, Long> testModel)
        throws RecommenderException, TasteException {
    if (isAlreadyRecommended()) {
        return null;
    }
    // transform from core's DataModels to Mahout's DataModels
    DataModel trainingModelMahout = new DataModelWrapper(trainingModel);
    DataModel testModelMahout = new DataModelWrapper(testModel);

    return runMahoutRecommender(opts, trainingModelMahout, testModelMahout);
}
 
Example #7
Source File: MovieUserEvaluator.java    From hiped2 with Apache License 2.0 5 votes vote down vote up
@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 #8
Source File: MovieUserEvaluator.java    From hiped2 with Apache License 2.0 5 votes vote down vote up
public static void evaluate(String ratingsFile)
    throws TasteException, IOException {
  DataModel model = new FileDataModel(new File(ratingsFile));
  RecommenderEvaluator evaluator =
      new AverageAbsoluteDifferenceRecommenderEvaluator();
  RecommenderBuilder recommenderBuilder = new MyRecommendBuilder();
  evaluator.evaluate(
      recommenderBuilder,
      null,
      model,
      0.95,
      0.05
  );
}
 
Example #9
Source File: BookRecommender.java    From Machine-Learning-in-Java with MIT License 5 votes vote down vote up
public Recommender buildRecommender(DataModel arg0) {
	try {
		return BookRecommender.itemBased();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example #10
Source File: BookRecommender.java    From Machine-Learning-in-Java with MIT License 5 votes vote down vote up
public static DataModel loadFromDB() throws Exception {
	
	// Database-based DataModel - MySQLJDBCDataModel
	/*
	 * A JDBCDataModel backed by a PostgreSQL database and accessed via
	 * JDBC. It may work with other JDBC databases. By default, this class
	 * assumes that there is a DataSource available under the JNDI name
	 * "jdbc/taste", which gives access to a database with a
	 * "taste_preferences" table with the following schema: CREATE TABLE
	 * taste_preferences ( user_id BIGINT NOT NULL, item_id BIGINT NOT NULL,
	 * preference REAL NOT NULL, PRIMARY KEY (user_id, item_id) ) CREATE
	 * INDEX taste_preferences_user_id_index ON taste_preferences (user_id);
	 * CREATE INDEX taste_preferences_item_id_index ON taste_preferences
	 * (item_id);
	 */
	MysqlDataSource dbsource = new MysqlDataSource();
	dbsource.setUser("user");
	dbsource.setPassword("pass");
	dbsource.setServerName("localhost");
	dbsource.setDatabaseName("my_db");

	DataModel dataModelDB = new MySQLJDBCDataModel(dbsource,
			"taste_preferences", "user_id", "item_id", "preference",
			"timestamp");
	
	return dataModelDB;
}
 
Example #11
Source File: BookRecommender.java    From Machine-Learning-in-Java with MIT License 5 votes vote down vote up
public static DataModel loadFromFile(String filePath) throws IOException{
	// File-based DataModel - FileDataModel
	DataModel dataModel = new FileDataModel(new File("preferences.csv"));
	return dataModel;

}
 
Example #12
Source File: ItembasedRecommender.java    From Building-Recommendation-Engines with MIT License 5 votes vote down vote up
public static void main(String[] args) throws TasteException, IOException {
	DataModel model = new FileDataModel(new File("data/dataset.csv"));
   	ItemSimilarity similarity = new LogLikelihoodSimilarity(model);
   	//UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
   	GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(model, similarity);
   	List<RecommendedItem> recommendations = recommender.mostSimilarItems(18, 3);
   	for (RecommendedItem recommendation : recommendations) {
   	  System.out.println(recommendation);
   	}

}
 
Example #13
Source File: UserBasedSVDRecommender.java    From Building-Recommendation-Engines with MIT License 5 votes vote down vote up
public static void main(String[] args) throws TasteException, IOException {
	//MF recommender model
   	DataModel model = new FileDataModel(new File("data/recodataset.csv"));   
   	//ALSWRFactorizer factorizer = new ALSWRFactorizer(model, 50, 0.065, 15);
   	ParallelSGDFactorizer factorizer = new ParallelSGDFactorizer(model,10,0.1,1);
   	SVDRecommender recommender = new SVDRecommender(model, factorizer);    	
   	
   	List<RecommendedItem> recommendations = recommender.recommend(2, 3);
   	for (RecommendedItem recommendation : recommendations) {
   	  System.out.println(recommendation);
   	}

}
 
Example #14
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;
}
 
Example #15
Source File: ServerRecommender.java    From myrrix-recommender with Apache License 2.0 4 votes vote down vote up
/**
 * @throws UnsupportedOperationException
 * @deprecated do not call
 */
@Deprecated
@Override
public DataModel getDataModel() {
  throw new UnsupportedOperationException();
}
 
Example #16
Source File: SlopeOneRecommender.java    From Building-Recommendation-Engines with MIT License 4 votes vote down vote up
public static void main(String[] args) throws IOException {
	DataModel model = new FileDataModel(new File("data/dataset.csv"));  
	Recommender recommender = (Recommender) new SlopeOneRecommender();

}
 
Example #17
Source File: MahoutRecommenderRunner.java    From rival with Apache License 2.0 3 votes vote down vote up
/**
 * Runs the recommender using models from file.
 *
 * @param opts see
 * {@link net.recommenders.rival.recommend.frameworks.AbstractRunner.RUN_OPTIONS}
 * @return see
 * {@link #runMahoutRecommender(net.recommenders.rival.recommend.frameworks.AbstractRunner.RUN_OPTIONS, org.apache.mahout.cf.taste.model.DataModel, org.apache.mahout.cf.taste.model.DataModel)}
 *
 * @throws RecommenderException when the recommender is instantiated
 * incorrectly.
 * @throws IOException when paths in property object are incorrect..
 * @throws TasteException when the recommender is instantiated incorrectly
 * or breaks otherwise.
 */
@Override
public TemporalDataModelIF<Long, Long> run(final RUN_OPTIONS opts) throws RecommenderException, TasteException, IOException {
    if (isAlreadyRecommended()) {
        return null;
    }
    DataModel trainingModel = new FileDataModel(new File(getProperties().getProperty(RecommendationRunner.TRAINING_SET)));
    DataModel testModel = new FileDataModel(new File(getProperties().getProperty(RecommendationRunner.TEST_SET)));
    return runMahoutRecommender(opts, trainingModel, testModel);
}
 
Example #18
Source File: OnlineRecommendation.java    From Machine-Learning-in-Java with MIT License 3 votes vote down vote up
public OnlineRecommendation() throws IOException {

		DataModel model = new StringItemIdFileDataModel(new File("data/chap6/BX-Book-Ratings.csv"), ";");
		PlusAnonymousConcurrentUserDataModel plusModel = new PlusAnonymousConcurrentUserDataModel(model, concurrentUsers);
		// recommender = ...;

	}
 
Example #19
Source File: MyrrixRecommender.java    From myrrix-recommender with Apache License 2.0 2 votes vote down vote up
/**
 * @deprecated do not call; only present to satisfy Mahout API
 * @throws UnsupportedOperationException in general
 */
@Deprecated
@Override
DataModel getDataModel();
 
Example #20
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 2 votes vote down vote up
/**
 * Not available. The client does not directly use any {@link DataModel}.
 *
 * @throws UnsupportedOperationException
 * @deprecated do not call
 */
@Deprecated
@Override
public DataModel getDataModel() {
  throw new UnsupportedOperationException();
}
 
Example #21
Source File: GenericRecommenderBuilder.java    From rival with Apache License 2.0 2 votes vote down vote up
/**
 * 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 #22
Source File: GenericRecommenderBuilder.java    From rival with Apache License 2.0 2 votes vote down vote up
/**
 * 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 #23
Source File: GenericRecommenderBuilder.java    From rival with Apache License 2.0 2 votes vote down vote up
/**
 * 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 #24
Source File: GenericRecommenderBuilder.java    From rival with Apache License 2.0 2 votes vote down vote up
/**
 * 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 #25
Source File: GenericRecommenderBuilder.java    From rival with Apache License 2.0 2 votes vote down vote up
/**
 * 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 #26
Source File: PopularityBasedRecommender.java    From rival with Apache License 2.0 2 votes vote down vote up
/**
 * Default constructor.
 *
 * @param dataModel the data model.
 */
public PopularityBasedRecommender(final DataModel dataModel) {
    super(dataModel);
}
 
Example #27
Source File: PopularityBasedRecommender.java    From rival with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor when a canidate item strategy is to be used.
 *
 * @param dataModel the data model
 * @param candidateItemsStrategy the strategy
 */
public PopularityBasedRecommender(final DataModel dataModel, final CandidateItemsStrategy candidateItemsStrategy) {
    super(dataModel, candidateItemsStrategy);
}