LensKit 开源推荐引擎使用教程

简介

LensKit是一个开源的引荐体系东西包,它供给了一组算法和东西来构建和评价引荐体系。LensKit支撑根据物品的协同过滤、根据用户的协同过滤、根据模型的引荐和混合引荐等多种引荐算法。

LensKit的首要特点是:

  1. 灵活性:LensKit支撑多种引荐算法,用户能够根据自己的需求挑选适宜的算法。
  2. 易用性:LensKit供给了简略易用的API和指令行东西,用户能够方便地构建和评价引荐体系。
  3. 可扩展性:LensKit采用模块化的规划,用户能够自定义算法和评价目标。
  4. 高性能:LensKit采用并行计算和内存映射等技术,能够高效地处理大规模数据集。

LensKit现已被广泛应用于学术界和工业界,例如在Netflix Prize比赛中就有参赛选手运用了LensKit构建引荐体系。

官网地址: lenskit.org/

目前 LensKit现已弃用java代码,改为python完成。想看java教程的清检查java.lenskit.org/

文档网址:mooc.lenskit.org/

GroupLens网址 :组镜头 (grouplens.org)

开始

从Maven中心存储库获取LensKit。

<dependency>
  <groupId>org.grouplens.lenskit</groupId>
  <artifactId>lenskit-all</artifactId>
  <version>3.0-M2</version>
</dependency>

LensKit 3.0-M2是LensKit的一个版别,它是LensKit 3.0版别的第二个里程碑版别。以下是一些LensKit 3.0-M2的新特性和改进:

  1. 改进了根据模型的引荐算法:LensKit 3.0-M2新增了一些根据模型的引荐算法,包括主题模型、隐式反馈模型和矩阵分化等。这些算法能够提高引荐体系的准确性和功率
  2. 改进了根据物品的协同过滤算法:LensKit 3.0-M2对根据物品的协同过滤算法进行了优化,提高了引荐体系的准确性和功率。
  3. 改进了评价目标:LensKit 3.0-M2新增了一些评价目标,包括覆盖率、多样性和新颖性等,能够更全面地评价引荐体系的性能。
  4. 改进了API和文档:LensKit 3.0-M2改进了API和文档,使得用户能够更方便地运用LensKit构建和评价引荐体系。

装备引荐器

方法一:

为了运用LensKit,您首先需求装备要运用的LensKit算法。这首要包括挑选所需的组建完成并运用

LenskitConfiguration装备它们。例如,要运用基线装备基本项目-项目 kNN 引荐器,请运用以下装备(将其保存在文件中,例如item-item.groovy)

// Use item-item CF to score items
bind ItemScorer to ItemItemScorer
// let's use personalized mean rating as the baseline/fallback predictor.
// 2-step process:
// First, use the user-item bias to compute item scores
bind (BaselineScorer, ItemScorer) to BiasItemScorer
// Second, use user-item biases
bind BiasModel to LiveUserItemBiasModel
// and normalize ratings by baseline prior to computing similarities
bind (UserVectorNormalizer) to BiasUserVectorNormalizer
// little speed tweek
within (UserVectorNormalizer) {
    bind Biasmodel to UserItemBiasModel
}

然后,您能够在 Java 程序中加载该装备:

LenskitConfiguration config = ConfigHelpers.load(new File("item-item.groovy"))

方法二:

运用java代码装备LenskitConfiguration,代码如下

// 装备Lenskit
LenskitConfiguration config = new LenskitConfiguration();
config.bind(ItemScorer.class).to(ItemItemScorer.class);
config.set(MinNeighbors.class).to(2);
config.set(ModelSize.class).to(1000);
config.bind(BaselineScorer.class,ItemScorer.class).to(UserMeanItemScorer.class);
config.bind(UserMeanBaseline.class,ItemScorer.class).to(ItemMeanRatingItemScorer.class);
config.bind(UserVectorNormalizer.class).to(BaselineSubtractingUserVectorNormalizer.class);

个人主张,尽量运用方法一,能够更好的讲算法装备和项目分离,使得项目愈加灵活。

连接数据源

LensKit还需求数据源。我们能够运用其中一个MovieLens数据集。从那里下载最新-小(或最新)文件。您还需求一个数据清单来告诉 LensKit 如何运用它;下载这个并将其保存在MovieLens数据目录下.csv文件里。

然后,您能够加载数据源:

StaticDataSource source = StaticDataSource.load("ml-latest-small/movielens.yml");
DataAccessObject dao = data.get();

创立引荐器

然后,您需求创立一个引荐器才干实践引荐:

LenskitRecommender rec = LenskitRecommender.build(config, dao);

当您完成一个LenskitRecommender时,请运用rec.close()进行封闭。运用try-with-resources块能够很好地完成这一点。

try (LenskitRecommender rec = LenskitRecommender.build(config)) {
    rec.close();
    /* do things */
}

生成主张

引荐器对象供给对组件的拜访,例如能够履行实践主张。例如,要为用户 1 生成 10条主张:ItemRecommender

ItemRecommender irec = rec.getItemRecommender();
ResultList recs = irec.recommendWithDetails(1, 10, null, null);

因为我们在装备LensKit时没有装备ItemRecommender,因此它运用默认设置:TopNItemRecommender,该引荐器运用装备的ItemScorer对项目进行评分,并回来N个得分最高的项目。因为我们运用的是物品-物品协同过滤,因此这些分数是物品-物品协同过滤预测评分的原始值。

您还能够运用以下指令预测评级:RatingPredictor

RatingPredictor pred = rec.getRatingPredictor();
Result score = pred.predict(1, 17);

输出为,用户1 对产品17的预测评分成果。

源代码

gitee.com/taisan/reco…

这里是我根据LensKit 写的一些算法完成demo。包括本文教程的中完整代码。感兴趣的小伙伴,欢迎检查,如果觉得不错,记得给个星星啊!