Apache Mahout is an open source project that is primarily used in producing scalable machine learning algorithms. This tutorial will show you how to install Apache Mahout in Eclipse.
Requirement:
Eclipse IDE
Maven - http://download.eclipse.org/technology/m2e/releases/
Apache Mahout
Steps for installing Eclipse:
1. Installing Maven in Eclipse:
Recommendation Java Code
Creating Dataset for project
Run the project
IF you getting warn for log4j then
EvaluationRecommender.java
References: https://mahout.apache.org/users/recommender/userbased-5-minutes.html
https://www.youtube.com/watch?v=63k560Livmg
Requirement:
Eclipse IDE
Maven - http://download.eclipse.org/technology/m2e/releases/
Apache Mahout
Steps for installing Eclipse:
1. Installing Maven in Eclipse:
- Open Eclipse -> Help -> Install new software
- In text box paste this URL - http://download.eclipse.org/technology/m2e/releases/
- Click add -> type the name [Example: m2eclipse] -> OK
- Click on check box of Maven Integration for Eclipse -> Next -> Agree the Licenses -> Finish
- Restart the Eclipse
- File -> New -> Project -> Select Maven -> Maven Project -> Next -> Next
- In Group ID -> com.predictionmarketing [Any name]
- Artifact ID -> Name of your project [RecommenderApp]
- Finish
- Right Click on Project
- Properties
- Java Build Path -> Libraries -> Add Library
- Select JRE System Library -> Next -> Workspace Default Library -> Finish
- Delete JRE System Library [J2SE 1.5]
- OK
- Click project and you will see pom.xml -> Open it
- Beside Overview tab there will be pom.xml -> click it
- Add the Dependency inside <dependencies> </dependencies>
<dependency> <groupId>org.apache.mahout</groupId> <artifactId>mahout-math</artifactId> <version>${mahout.version}</version> </dependency>
- Change ${mahout.version} with your mahout version [Example: 0.13.0]
- Save and close the file
Recommendation Java Code
Creating Dataset for project
- Copy the Dataset from https://mahout.apache.org/users/recommender/userbased-5-minutes.html
- Project -> Right Click -> New -> Folder -> Folder Name "data" -> Finish
- Right click on "data" Folder -> New -> File
- File name as "dataset.csv" -> paste the Dataset you have copied from the above website and save
- Extract the Apache Mahout Files -> Inside Mahout Folder -> Copy All Jar files
- Right Click on Project -> And paste all jar files
- One by one right click jar file -> Build Path -> Add to Build Path
package com.predictionmarketing.RecommenderApp;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.log4j.PropertyConfigurator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
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.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws Exception
{
String log4jConfPath = "LOG4J/log4j.properties";
PropertyConfigurator.configure(log4jConfPath);
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);
}
}
}
Run the project
IF you getting warn for log4j then
- Create a folder in project as LOG4J
- Create a file in folder LOG4J as "log4j.properties" and paste the code
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
- And add the below code
String log4jConfPath = "/path/to/log4j.properties"; PropertyConfigurator.configure(log4jConfPath);
EvaluationRecommender.java
package com.predictionmarketing.RecommenderApp;
import java.io.File;
import java.io.IOException;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
import org.apache.mahout.cf.taste.eval.RecommenderEvaluator;
import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
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;
public class EvaluateRecommender {
public static void main(String[] args) throws Exception {
DataModel model = new FileDataModel(new File("data/dataset.csv"));
RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator();
RecommenderBuilder builder = new MyRecommenderBuilder();
double result = evaluator.evaluate(builder, null, model, 0.9, 1.0);
System.out.println(result);
}
}
class MyRecommenderBuilder implements RecommenderBuilder {
public Recommender buildRecommender(DataModel dataModel) throws TasteException{
UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, dataModel);
return new GenericUserBasedRecommender(dataModel, neighborhood, similarity);
}
}
References: https://mahout.apache.org/users/recommender/userbased-5-minutes.html
https://www.youtube.com/watch?v=63k560Livmg
It very good tuturial
ReplyDelete