Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ratings.train = read.csv("/Users/fay_kong/Desktop/SML_SPRING/FILM/movies_matchup.csv", head = TRUE)
- ratings.test = read.csv("/Users/fay_kong/Desktop/SML_SPRING/FILM/test_matchup.csv", head = TRUE)
- user.ids = sort(unique(ratings.train$userId)) #count user NO.
- movie.ids = sort(unique(ratings.train$movieId)) #count movie NO.
- totalmean = mean(ratings.train[,5])
- # # # user.bia---------------------------------------------
- user.bias = matrix(NA,671,2)
- user.bias[,1] = c(1:671)
- for(i in 1:671){
- users = ratings.train[which(ratings.train[,1]==i),]
- user.mean = mean(users[,5])
- user.bias[i,2] = user.mean - totalmean
- }
- # # # movie.bias-------------------------------------------
- movie.bias = matrix(NA,8090,3)
- movie.bias[,1] = c(1:8090)
- movie.bias[,2] = unique(ratings.train$movieId)
- for(i in 1:8090){
- movies = ratings.train[which(ratings.train[,2]==movie.bias[i,2]),]
- movie.mean = mean(movies[,5])
- movie.bias[i,3] = movie.mean - totalmean
- }
- # # # predictions------------------------------------------
- ratings.test$prediction = 0
- for(i in 1:24720){
- userid = ratings.test[i,1]
- movieid = ratings.test[i,2]
- ubias = user.bias[userid,2]
- movie.row = movie.bias[which(movie.bias[,2]==movieid)]
- if(length(movie.row) == 0){
- mbias = 0
- }else{
- mbias = movie.bias[movie.row,3]
- }
- ratings.test[i,5] = totalmean + ubias +mbias
- }
- RoundToZeroFive = function(x){
- decimal = x - trunc(x)
- roundx = matrix(NA,length(x),1)
- for(i in 1:length(x)){
- if(decimal[i] < 0.25){
- roundx[i] = trunc(x[i])
- }
- else{
- if(decimal[i] > 0.75){
- roundx[i] = trunc(x[i])+1
- }
- else{
- roundx[i] = trunc(x[i])+0.5
- }
- }
- }
- roundx
- }
- predictions = RoundToZeroFive(ratings.test[,5])
- predictions[which(predictions==0)] = 0.5
- predictions[which(predictions > 5)] = 5
- write.csv(predictions,file = "/Users/fay_kong/Desktop/SML_SPRING/FILM/Baselinepredictions.csv",row.names = FALSE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement