-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRecommScript.R
More file actions
153 lines (139 loc) · 5.15 KB
/
RecommScript.R
File metadata and controls
153 lines (139 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Set data path as per your data file (for example: "c://abc//" )
setwd("C:/Users/Akshama PC")
# If not installed, first install following three packages in R
library(recommenderlab)
library(reshape2)
library(ggplot2)
# Read training file along with header
tr<-read.csv("train_v2.csv",header=TRUE)
# Look at first few lines of this file
head(tr)
# Remove 'id' column. We do not need it
tr<-tr[,-c(1)]
# Check, if removed
tr[tr$user==1,]
# Using acast to convert above data as follows:
# m1 m2 m3 m4
# u1 3 4 2 5
# u2 1 6 5
# u3 4 4 2 5
g<-acast(tr, user ~ movie)
# Check the class of g
class(g)
# Convert it as a matrix
R<-as.matrix(g)
# Convert R into realRatingMatrix data structure
# realRatingMatrix is a recommenderlab sparse-matrix like data-structure
r <- as(R, "realRatingMatrix")
r
# view r in other possible ways
as(r, "list") # A list
as(r, "matrix") # A sparse matrix
#Turn it into data-frame
head(as(r, "data.frame"))
# normalize the rating matrix
r_m <- normalize(r)
r_m
as(r_m, "list")
# Draw an image plot of raw-ratings & normalized ratings
# A column represents one specific movie and ratings by users
# are shaded.
# Note that some items are always rated 'black' by most users
# while some items are not rated by many users
# On the other hand a few users always give high ratings
# as in some cases a series of black dots cut across items
image(r, main = "Raw Ratings")
# Can also turn the matrix into a 0-1 binary matrix
r_b <- binarize(r, minRating=1)
as(r_b, "matrix")
# Create a recommender object (model)
# Run anyone of the following four code lines.
# Do not run all four
# They pertain to four different algorithms.
# UBCF: User-based collaborative filtering
# IBCF: Item-based collaborative filtering
# Parameter 'method' decides similarity measure
# Cosine or Jaccard
rec=Recommender(r[1:nrow(r)],method="UBCF", param=list(normalize = "Z-score",method="Cosine",nn=5, minRating=1))
rec=Recommender(r[1:nrow(r)],method="UBCF", param=list(normalize = "Z-score",method="Jaccard",nn=5, minRating=1))
rec=Recommender(r[1:nrow(r)],method="IBCF", param=list(normalize = "Z-score",method="Jaccard",minRating=1))
rec=Recommender(r[1:nrow(r)],method="POPULAR")
# Depending upon your selection, examine what you got
print(rec)
names(getModel(rec))
getModel(rec)$nn
############Create predictions#############################
# This prediction does not predict movie ratings for test.
# But it fills up the user 'X' item matrix so that
# for any userid and movieid, I can find predicted rating
# dim(r) shows there are 6040 users (rows)
# 'type' parameter decides whether you want ratings or top-n items
# get top-10 recommendations for a user, as:
# predict(rec, r[1:nrow(r)], type="topNList", n=10)
recom <- predict(rec, r[1:nrow(r)], type="ratings")
recom
########## Examination of model & experimentation #############
# Convert prediction into list, user-wise
as(recom, "list")
# Study and Compare the following:
as(r, "matrix") # Has lots of NAs. 'r' is the original matrix
as(recom, "matrix") # Is full of ratings. NAs disappear
as(recom, "matrix")[,1:10] # Show ratings for all users for items 1 to 10
as(recom, "matrix")[5,3] # Rating for user 5 for item at index 3
as.integer(as(recom, "matrix")[5,3]) # Just get the integer value
as.integer(round(as(recom, "matrix")[6039,8])) # Just get the correct integer value
as.integer(round(as(recom, "matrix")[368,3717]))
# Convert all your recommendations to list structure
rec_list<-as(recom,"list")
head(summary(rec_list))
# Access this list. User 2, item at index 2
rec_list[[2]][2]
# Convert to data frame all recommendations for user 1
u1<-as.data.frame(rec_list[[1]])
attributes(u1)
class(u1)
# Create a column by name of id in data frame u1 and populate it with row names
u1$id<-row.names(u1)
# Check movie ratings are in column 1 of u1
u1
# Now access movie ratings in column 1 for u1
u1[u1$id==3952,1]
########## Create submission File from model #######################
# Read test file
test<-read.csv("test_v2.csv",header=TRUE)
head(test)
# Get ratings list
rec_list<-as(recom,"list")
head(summary(rec_list))
ratings<-NULL
# For all lines in test file, one by one
for ( u in 1:length(test[,2]))
{
# Read userid and movieid from columns 2 and 3 of test data
userid <- test[u,2]
movieid<-test[u,3]
# Get as list & then convert to data frame all recommendations for user: userid
u1<-as.data.frame(rec_list[[userid]])
# Create a (second column) column-id in the data-frame u1 and populate it with row-names
# Remember (or check) that rownames of u1 contain are by movie-ids
# We use row.names() function
u1$id<-row.names(u1)
# Now access movie ratings in column 1 of u1
x= u1[u1$id==movieid,1]
# print(u)
# print(length(x))
# If no ratings were found, assign 0. You could also
# assign user-average
if (length(x)==0)
{
ratings[u] <- 0
}
else
{
ratings[u] <-x
}
}
length(ratings)
tx<-cbind(test[,1],round(ratings))
# Write to a csv file: submitfile.csv in your folder
write.table(tx,file="submitfile.csv",row.names=FALSE,col.names=FALSE,sep=',')