Advertisement
arindamnayak

Untitled

Feb 28th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. # Map 1-based optional input ports to variables
  2. data.songs <- maml.mapInputPort(1) # class: data.frame
  3.  
  4. data.toUse <- (data.songs[,!(names(data.songs) %in% c("user"))])
  5.  
  6. getCosine <- function(x,y)
  7. {
  8. this.cosine <- sum(x*y) / (sqrt(sum(x*x)) * sqrt(sum(y*y)))
  9. return(this.cosine)
  10. }
  11.  
  12. data.similarity <- matrix(NA, nrow=ncol(data.toUse),
  13. ncol=ncol(data.toUse),
  14. dimnames=list(colnames(data.toUse),
  15. colnames(data.toUse)))
  16.  
  17.  
  18. for(i in 1:ncol(data.toUse)) {
  19. # Loop through the columns for each column
  20. for(j in 1:ncol(data.toUse)) {
  21. # Fill in placeholder with cosine similarities
  22. data.similarity[i,j] <- getCosine(as.matrix(data.toUse[i]),
  23. as.matrix(data.toUse[j]))
  24. }
  25. }
  26.  
  27. data.neighbours <- matrix(NA, nrow=ncol(data.similarity),
  28. ncol=11,dimnames=list(colnames(data.similarity)))
  29.  
  30. for(i in 1:ncol(data.toUse))
  31. {
  32. data.neighbours[i,] <-
  33. t(head(n=11,
  34. rownames(as.matrix((data.similarity[order(data.similarity[,i],
  35. decreasing = TRUE),][,i])))))
  36. }
  37.  
  38.  
  39. data.neighbours = as.data.frame(data.neighbours)
  40. # Select data.frame to be sent to the output Dataset port
  41. maml.mapOutputPort("data.neighbours");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement