recommenderlab

repository·master·Indexed 42 days ago

https://github.com/mhahsler/recommenderlab

An R framework for the development and evaluation of collaborative filtering recommender algorithms. It provides specialized data structures like 'realRatingMatrix' and supports various algorithms including UBCF, IBCF, SVD, SVDF, ALS, and LIBMF. The framework includes tools for cross-validation, repeated bootstrap sampling, and top-N recommendation testing using measures such as MSE, RMSE, MAE, precision, and recall.

Tokens
2.1K
Snippets
7
Records
12
Agent score
53%

What's inside recommenderlab

  1. Install recommenderlab

    master

    You can install the stable version of recommenderlab from CRAN, or the current development version from r-universe.

    # Stable CRAN version
    install.packages("recommenderlab")
    
    # Current development version
    install.packages("recommenderlab",
        repos = c("https://mhahsler.r-universe.dev",
                  "https://cloud.r-project.org/"))
  2. Review MovieLens data usage license

    master

    The MovieLens dataset is provided for research purposes under specific conditions:

    1. No Endorsement: You may not imply endorsement from the University of Minnesota or the GroupLens Research Group.
    2. Acknowledgment: You must acknowledge the use of the dataset in any resulting publications and provide a copy of those publications to the researchers.
    3. No Redistribution: You may not redistribute the data without separate permission.
    4. Non-Commercial: You may not use this information for commercial or revenue-bearing purposes without prior permission from a faculty member of the GroupLens Research Project at the University of Minnesota.
  3. Introduction to recommenderlab

    master

    recommenderlab is an R package that provides research infrastructure for developing and evaluating collaborative filtering recommender algorithms. It supports both rating data (e.g., 1-5 stars) and unary (0-1) datasets. Key features include:

    • Sparse representation for user-item matrices.
    • A variety of popular recommendation algorithms.
    • Support for top-N recommendations.
    • Robust cross-validation and evaluation frameworks.
  4. Train and use a Recommender

    master

    To use the package, load the library and use the Recommender() function to train a model on a dataset (such as a realRatingMatrix). You can then use predict() to generate recommendations for new users.

    Note: When predicting, you can specify n to get the top-N recommendations.

    library("recommenderlab")
    data("MovieLense")
    
    # Prepare data (e.g., users with > 100 ratings)
    MovieLense100 <- MovieLense[rowCounts(MovieLense) > 100, ]
    
    # Train a user-based collaborative filtering (UBCF) model
    train <- MovieLense100[1:300]
    rec <- Recommender(train, method = "UBCF")
    
    # Predict top-5 recommendations for users 301 and 302
    pre <- predict(rec, MovieLense100[301:302], n = 5)
    
    # Convert predictions to a list for easier viewing
    as(pre, "list")
  5. Understand the MovieLens users.dat format

    master

    The users.dat file contains demographic information for users. Each line follows this format:

    UserID::Gender::Age::Occupation::Zip-code

    • UserID: Integer.
    • Gender: M for male, F for female.
    • Age: Represented by integer ranges:
      • 1: Under 18
      • 18: 18-24
      • 25: 25-34
      • 35: 35-44
      • 45: 45-49
      • 50: 50-55
      • 56: 56+
    • Occupation: Integer mapping (e.g., 1 for academic/educator, 12 for programmer, 20 for writer).
    • Zip-code: User-provided postal code.
    UserID::Gender::Age::Occupation::Zip-code
  6. Understand the MovieLens ratings.dat format

    master

    The ratings.dat file contains user ratings for movies. Each line follows this format:

    UserID::MovieID::Rating::Timestamp

    • UserID: Integer between 1 and 6040.
    • MovieID: Integer between 0 and 3592.
    • Rating: A whole-star rating on a 5-star scale.
    • Timestamp: Seconds since the epoch (as returned by time(2)).
    UserID::MovieID::Rating::Timestamp
  7. Supported Recommender Evaluation Protocols and Measures

    master

    The framework provides infrastructure for evaluating algorithms using several protocols and measures.

    Evaluation Protocols

    • Train/test split
    • Cross-validation
    • Repeated bootstrap sampling
    • Given-n and all-but-x protocols

    Evaluation Measures

    • Rating errors: MSE, RMSE, MAE
    • Top-N recommendations: TPR/FPR (ROC), precision, and recall
  8. Understand the MovieLens movies.dat format

    master

    The movies.dat file contains movie metadata. Each line follows this format:

    MovieID::Title::Genres

    • MovieID: Integer.
    • Title: Movie title (includes release year, matching IMDB).
    • Genres: Pipe-separated (|) list of genres (e.g., Action|Adventure).

    Supported genres include: Action, Adventure, Animation, Children's, Comedy, Crime, Documentary, Drama, Fantasy, Film-Noir, Horror, Musical, Mystery, Romance, Sci-Fi, Thriller, War, and Western.

    MovieID::Title::Genres
  9. Supported Recommender Algorithms

    master

    The recommenderlab framework supports a wide variety of collaborative filtering and baseline algorithms:

    • UBCF: User-based collaborative filtering
    • IBCF: Item-based collaborative filtering
    • SVD: SVD with column-mean imputation
    • SVDF: Funk SVD
    • ALS: Alternating Least Squares
    • LIBMF: Matrix factorization with LIBMF
    • AR: Association rule-based recommender
    • POPULAR: Popular items
    • RANDOM: Randomly chosen items for comparison
    • RERECOMMEND: Re-recommend liked items
    • HybridRecommender: Hybrid recommendations
  10. Train and use a Recommender algorithm

    master

    To use a recommender, load the package, prepare your data (often as a realRatingMatrix), use the Recommender() function to train a model, and predict() to generate recommendations.

    library("recommenderlab")
    
    # Prepare data (example using MovieLense)
    data("MovieLense")
    train <- MovieLense[1:300, ]
    
    # Train a user-based collaborative filtering recommender
    rec <- Recommender(train, method = "UBCF")
    
    # Create top-N recommendations for new users (e.g., users 301 and 302)
    pre <- predict(rec, MovieLense[301:302, ], n = 5)
    
    # Convert predictions to a list format
    as(pre, "list")
    library("recommenderlab")
    data("MovieLense")
    
    # Filter users with more than 100 ratings
    MovieLense100 <- MovieLense[rowCounts(MovieLense) > 100, ]
    
    # Train a user-based collaborative filtering recommender
    train <- MovieLense100[1:300, ]
    rec <- Recommender(train, method = "UBCF")
    
    # Create top-N recommendations for new users (users 301 and 302)
    pre <- predict(rec, MovieLense100[301:302, ], n = 5)
    
    # View as list
    as(pre, "list")
  11. Evaluate algorithms using cross-validation

    master

    You can compare multiple algorithms using an evaluationScheme. This involves defining a scheme (e.g., 10-fold cross-validation), specifying the algorithms and their parameters, and then calling evaluate().

    # Define the evaluation scheme
    scheme <- evaluationScheme(MovieLense100, method = "cross-validation", k = 10, 
      given = -5, goodRating = 4)
    
    # Define algorithms to compare
    algorithms <- list(
      "random items" = list(name = "RANDOM", param = NULL),
      "popular items" = list(name = "POPULAR", param = NULL),
      "user-based CF" = list(name = "UBCF", param = list(nn = 3)),
      "item-based CF" = list(name = "IBCF", param = list(k = 100))
    )
    
    # Run evaluation for top-N lists of different lengths
    results <- evaluate(scheme, algorithms, type = "topNList",
      n = c(1, 3, 5, 10), progress = FALSE)
    
    # Plot the results
    plot(results, annotate = 2, legend = "topleft")
    scheme <- evaluationScheme(MovieLense100, method = "cross-validation", k = 10, 
      given = -5, goodRating = 4)
    
    algorithms <- list(
      "random items" = list(name = "RANDOM", param = NULL),
      "popular items" = list(name = "POPULAR", param = NULL),
      "user-based CF" = list(name = "UBCF", param = list(nn = 3)),
      "item-based CF" = list(name = "IBCF", param = list(k = 100))
    )
    
    results <- evaluate(scheme, algorithms, type = "topNList",
      n=c(1, 3, 5, 10), progress = FALSE)
    
    plot(results, annotate = 2, legend = "topleft")
  12. Recommender Evaluation Protocols and Measures

    master

    The package provides a framework for evaluating algorithms using different protocols and metrics.

    Evaluation Protocols

    • Train/test split
    • Cross-validation
    • Repeated bootstrap sampling

    Protocols can be configured using given-n and all-but-x methods.

    Evaluation Measures

    • Rating errors: MSE, RMSE, MAE
    • Top-N recommendations: TPR/FPR (ROC), precision, and recall