Converting the DataFrame

We are going to use functions from an R library called recommenderlab to build recommendation engine projects in this chapter. Irrespective of the category of recommendation system we implement, there are some prerequisites that the dataset needs to satisfy to be able to apply the recommenderlab functions. The prebuilt recommenderlab functions for collaborative filtering expects realRatingMatrix to be supplied as input. In our case, the Jester5k dataset is already in this format, therefore, we could directly use this matrix to apply the recommenderlab functions.

In case, we were to have our data as a R DataFrame and if we intend to convert into realRatingMatrix, the following steps may be performed:

  1. Convert the DataFrame into an R matrix as follows:
# convert the df dataframe to a matrix
r_mat <- as.matrix(df)
  1. Convert the resultant matrix into realRatingMatrix with the help of the as() function as follows:
# convert r_mat matrix to a recommenderlab realRatingMatrix
r_real_mat <- as(r_mat,"realRatingMatrix")
Here, we assume that the name of the DataFrame is df, the code will convert it into a realRatingMatrix that can be used as input to the recommenderlab functions.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset