Pritish Patil
Pritish Patil

Reputation: 21

Convert large matrix to binary rating matrix in R?

I have a matrix which has information about which item the user has viewed i.e 1= item viewed and 0= item not viewed. This matrix is a very large matrix with dimensions 9276 x 13068. But whenever I try converting it into binaryRatingMatrix using

binmatrix<- as(user_asset_matrix2,"binaryRatingMatrix")
binmatrix
2 x 2 rating matrix of class ‘binaryRatingMatrix’ with 3 ratings.

it forms a 2x2 matrix which is of no use. After converting binmatrix back to matrix it is converted to:

  0        1 
0 TRUE     TRUE
1 TRUE     FALSE

Can anyone give me a solution to solve this issue?

Upvotes: 2

Views: 1131

Answers (2)

Andrew Buchanan
Andrew Buchanan

Reputation: 81

I had a similar problem with my code and was getting the same output even though my initial data-set had many many ratings. I was reading in a csv file, it turns out you can't simply coerce a data.frame straight to a binaryRatingMatrix without converting it to a data matrix first:

library(recommenderlab)
csv<-read.csv("csv.csv",header=TRUE,row.names=1)
csv<-as.matrix(csv)
csv<-as(csv,"binaryRatingMatrix)
csv
139x867 rating matrix of class 'binaryRatingMatrix' with 3944 ratings.

When you print csv after converting it to "binaryRatingMatrix", it should now show the correct dimensions of the variable along with the number of ratings you are expecting.

Upvotes: 1

Stefano
Stefano

Reputation: 231

I have just managed to get over this problem, although I am almost not sure how... Let me try helping you.

First of all, are you sure the format of the large matrix is actually matrix and not data.frame or data.table? Somehow, this was my problem...

In either case, assuming that the matrix has been obtained starting from a transaction list trlist with many many rows and two columns such as the following

    > dim(trlist)
    [1] 100000 2
    > colnames(trlist)
    [1] "customer" "item"

we can obtain a hopefully good customer-item matrix using acast from reshape2 (and please forgive my OCD indentation choices):

    product_item <- 
        acast(
            data          = trlist, 
            formula       = customer ~ item,  
            value.var     = item,
            fun.aggregate = length
        )

Now we should be ready for:

    product_item    <- as(product_item, "realRatingMatrix")
    sp_product_item <- binarize(product_item, minRating = 1)

If the matrix you have doesn't come from a transaction list, you can use melt from the already loaded reshape2 to obtain one, and then follow the previous steps.

I cannot guarantee that this will help you too, but it worked for me :)

Upvotes: 1

Related Questions