Mahout Recommended engine usage

Source: Internet
Author: User
Tags file info stub

1 user-based recommendation engine

Datamodel: Provides storage and access to user, item, and preference data for computing

Usersimilarity: Calculating the similarity between users

Userneighborhood: Compute the user's neighbor

Recommender: Organize the above components together to provide the user with item recommendation

package Com.taobao.afan;

import java.io.File;

import java.util.List;

import Org.apache.mahout.cf.taste.impl.model.file.FileDataModel;

import Org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;

import Org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;

import Org.apache.mahout.cf.taste.model.DataModel;

import Org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;

import Org.apache.mahout.cf.taste.recommender.RecommendedItem;

import Org.apache.mahout.cf.taste.recommender.Recommender;

import org.apache.mahout.cf.taste.similarity.UserSimilarity;

Public class Recommenderintro {

Public Static void Main (string[] args) throws exception{

TODO auto-generated Method stub

Datamodel model = new filedatamodel (new File ("./intro.csv")); Loading data files

Usersimilarity similarity = new pearsoncorrelationsimilarity (model); Constructing the method of similarity calculation

Userneighborhood neighborhood =

New Nearestnuserneighborhood (2, similarity, model);

Recommender recommender = new genericuserbasedrecommender (

Model, neighborhood, similarity); Create a recommendation engine

List<recommendeditem> recommendations =

Recommender.recommend (1, 1); Recommend an item to user 1

for (Recommendeditem recommendation:recommendations) {

SYSTEM.OUT.PRINTLN (recommendation);

}

}

}

Output:

2011-2-8 12:42:46 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Creating Filedatamodel for file./intro.csv

2011-2-8 12:42:48 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Reading File info ...

2011-2-8 12:42:48 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Read lines:21

2011-2-8 12:42:48 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Processed 5 users

recommendeditem[item:104, value:4.253491]

Recommendation Engine Evaluation

Package Com.taobao.afan;

Import Java.io.File;

Import org.apache.mahout.cf.taste.common.TasteException;

Import Org.apache.mahout.cf.taste.eval.RecommenderBuilder;

Import Org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;

Import Org.apache.mahout.cf.taste.model.DataModel;

Import Org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;

Import Org.apache.mahout.cf.taste.recommender.Recommender;

Import org.apache.mahout.cf.taste.similarity.UserSimilarity;

Import Org.apache.mahout.common.RandomUtils;

Import Org.apache.mahout.cf.taste.impl.model.file.FileDataModel;

Import Org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;

Import Org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;

Import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;

Import Org.apache.mahout.cf.taste.eval.RecommenderEvaluator;

public class Evaluatorinfo {

public static void Main (string[] args) throws exception{

TODO auto-generated Method Stub

Randomutils.usetestseed ();

Datamodel model = new Filedatamodel (New File ("./intro.csv"));

Recommenderevaluator evaluator = new Averageabsolutedifferencerecommenderevaluator ();

Recommenderbuilder builder = new Recommenderbuilder () {

@Override

Public Recommender Buildrecommender (Datamodel model)

Throws Tasteexception {

Usersimilarity similarity = new pearsoncorrelationsimilarity (model);

Userneighborhood neighborhood =

New Nearestnuserneighborhood (2, similarity, model);

Return

New Genericuserbasedrecommender (model, neighborhood, similarity);

}

};

Double score = evaluator.evaluate (builder, NULL, model, 0.7, 1.0);

SYSTEM.OUT.PRINTLN (score);

}

}

Output:

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Creating Filedatamodel for file./intro.csv

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Beginning evaluation using 0.7 of Filedatamodel[datafile:/root/workspace/recommenderintro/./intro.csv]

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Reading File info ...

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Read lines:21

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Processed 5 users

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Processed 5 users

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Information: Beginning evaluation of 3 users

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Starting timing of 3 tasks in 1 threads

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Average time per recommendation:11ms

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Approximate memory used:1mb/7mb

2011-2-8 13:35:31 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Evaluation result:0.5

0.5

2 evaluation accuracy and return rate

Package Com.taobao.afan;

Import org.apache.mahout.cf.taste.common.TasteException;

Import Org.apache.mahout.cf.taste.eval.IRStatistics;

Import Org.apache.mahout.cf.taste.eval.RecommenderBuilder;

Import Org.apache.mahout.cf.taste.eval.RecommenderIRStatsEvaluator;

Import Org.apache.mahout.cf.taste.impl.eval.GenericRecommenderIRStatsEvaluator;

Import Org.apache.mahout.cf.taste.impl.model.file.FileDataModel;

Import Org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;

Import Org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;

Import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;

Import Org.apache.mahout.cf.taste.model.DataModel;

Import Org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;

Import Org.apache.mahout.cf.taste.recommender.Recommender;

Import org.apache.mahout.cf.taste.similarity.UserSimilarity;

Import Java.io.File;

Class Irevaluatorintro {

public static void Main (string[] args) throws Exception {

Datamodel model = new Filedatamodel (New File ("./intro.csv"));

Recommenderirstatsevaluator evaluator =

New Genericrecommenderirstatsevaluator ();

Create a recommendation engine

Recommenderbuilder Recommenderbuilder = new Recommenderbuilder () {

@Override

Public Recommender Buildrecommender (Datamodel model) throws Tasteexception {

Usersimilarity similarity = new pearsoncorrelationsimilarity (model);

Userneighborhood neighborhood =

New Nearestnuserneighborhood (2, similarity, model);

return new Genericuserbasedrecommender (model, neighborhood, similarity);

}

};

Evaluate accuracy and return rate "at 2":

Irstatistics stats = evaluator.evaluate (Recommenderbuilder,

NULL, model, NULL, 2,

Genericrecommenderirstatsevaluator.choose_threshold,

1.0);

System.out.println (Stats.getprecision ());

System.out.println (Stats.getrecall ());

}

}

Output:

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Creating Filedatamodel for file./intro.csv

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Reading File info ...

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Read lines:21

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Processed 5 users

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Processed 5 users

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: evaluated with user 2 in 29ms

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: precision/recall/fall-out:0.0/0.0/0.3333333333333333

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: Processed 5 users

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: evaluated with user 4 in 0ms

2011-2-8 13:44:52 Org.slf4j.impl.JCLLoggerAdapter Info

Info: precision/recall/fall-out:0.25/0.5/0.25

0.25

0.5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.