Recently, due to the competition, we need to learn the relevant knowledge of recommendation system, and librec is an open-source recommendation algorithm library based on Java, which is written by Guo Guibing team of Northeast University. There are 2.9k star s on GitHub.
Most steps in this article refer to LibRec learning notes (I): how to import a third-party package into your project: LibRec?
1, Download librec
github address: GitHub project address
Students who can't access the Internet scientifically can download it through my baidu online disk address Baidu online disk
After downloading, open the directory. It looks like this
2, Create maven project
Please refer to this blog for specific steps IDEA 2020 creates Maven project
3, Import librec into your maven project as a jar package
Copy the librec-core-3.0.0 jar file under librec - > lib to the bin folder in your maven folder
Then open the terminal under this folder and enter the instruction
mvn install:install-file -Dfile=librec-core-3.0.0.jar -DgroupId=net.librec -DartifactId=librec-core -Dversion=3.0.0 -Dpackaging=jar
Finally, in POM Adding dependencies to XML
<dependency> <groupId>net.librec</groupId> <artifactId>librec-core</artifactId> <version>3.0.0</version> </dependency>
3, Testing
Create test under the java package, and the code in the Java class is:
public static void main(String[] args) throws Exception { // build data model Configuration conf = new Configuration(); conf.set("dfs.data.dir", "path to the data dir"); TextDataModel dataModel = new TextDataModel(conf); dataModel.buildDataModel(); // build recommender context RecommenderContext context = new RecommenderContext(conf, dataModel); // build similarity conf.set("rec.recommender.similarity.key" ,"item"); conf.setBoolean("rec.recommender.isranking", true); conf.setInt("rec.similarity.shrinkage", 10); RecommenderSimilarity similarity = new CosineSimilarity(); similarity.buildSimilarityMatrix(dataModel); context.setSimilarity(similarity); // build recommender conf.set("rec.neighbors.knn.number", "200"); Recommender recommender = new ItemKNNRecommender(); recommender.setContext(context); // run recommender algorithm recommender.train(context); // evaluate the recommended result EvalContext evalContext = new EvalContext(conf, recommender, dataModel.getTestDataSet(), context.getSimilarity().getSimilarityMatrix(), context.getSimilarities()); RecommenderEvaluator ndcgEvaluator = new NormalizedDCGEvaluator(); ndcgEvaluator.setTopN(10); double ndcgValue = ndcgEvaluator.evaluate(evalContext); System.out.println("ndcg:" + ndcgValue); }
Right click Run to run
This is the result of successful operation
4, A missing step in blogging
How can the reality be satisfactory? The results I run keep reporting errors. After continuous search by major search engines, I finally found a key step not mentioned in this blog, that is, the other jar packages under librec - > lib are also placed in Maven - > lib folder and added dependency.
The following are all the jar packages that need to be imported.
Add dependent code to maven warehouse Copy in.
For example: commons-logging-1.2 Jar search in maven repository
Since the version number is 1.2, click the 1.2 button.
Copy maven dependency code to POM In XML,
Finally, update the project.
My external library ended up like this:
Summary: the learning of recommendation algorithm is doomed to be difficult. Many students are stuck in the import step. So, the students who see this, as long as you successfully run the test code, you will take a big step ahead of others. Do you feel very excited!!!