Last active
June 28, 2018 12:15
-
-
Save decisionmechanics/8ad6560f58f33689a766629e40d76abd to your computer and use it in GitHub Desktop.
Create item-based recommendations using a co-occurrence matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_recommendation_ratings <- function(rating_file_path) { | |
# Read user ID, item ID, user preference CSV data | |
ratings <- read.csv(file = rating_file_path, header = FALSE, col.names = c('user', 'item', 'preference')) | |
# Create item co-occurrence matrix | |
co_occurrence_matrix <- crossprod(table(ratings[, c('user', 'item')])) | |
# Convert long format to wide format and replace NAs with 0s | |
user_ratings <- tidyr::spread(ratings, user, preference, fill = 0) | |
# Remove item ID column and convert data frame to matrix | |
user_rating_matrix <- data.matrix(user_ratings[-1]) | |
# Create ratings | |
recommendation_rating_matrix <- co_occurrence_matrix %*% user_rating_matrix | |
# Remove ratings for items that user has already rated | |
unrated_item_recommendation_rating_matrix <- recommendation_rating_matrix * ifelse(user_rating_matrix > 0, NA, 1) | |
data.frame(unrated_item_recommendation_rating_matrix, check.names = FALSE) | |
} | |
get_recommendation_ratings('small.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment