The Algorithm

For every feature we have top 20 selected Ids corresponding each Id.
Let's introduce one more column called rank in our data frame, this will be used in the algorithm.

Remember our data frame k,
rank has the ranks of the selected Ids for every group of 20. 

r <- ave(k$X,k$id1,FUN=function(x)rank(x,ties.method="first"))
k$rank <- r

rank is now the 14th column, rank of any Id, thus can be easily fetched using
k[id,14]

Here's where the algorithm starts, 
we will calculate the match_score of each Id in every column, where
feature_match_score <-  (21-rank(id))*feature_imp_value

Create a different data frame called match with same structure as k,
match <-k
match[,-1]<-0        #fill all columns with 0 excluding Id column.

The feature_match_score will be filled into match.
Now decide for the feature_imp_value. I made it on the scale of 1-9 according to their importance in selecting the best match.

con <- paste0(names(match[,-1]),"_imp") 

q_following_imp <-4                            
topic_followed_imp <-4                         
topic_answered_imp <-5                         
nfollowers_imp <-9                              
nfollowing_imp <-9                              
N.ans.answered_imp<-7                          
Nupvotes.on.top.5.ans..Answered.addition_imp <-8
X30.days.views_imp <-3                          
All.time.views_imp <-6                         
N.ans.upv.by.A..April.month_imp <-2             
min_prevotes_imp <-1 

Now get the feature_match_score, eg.

match$topic_followed 
<-(21-k[row.names(k),14])*topic_followed_imp

Now its time to get the best match, for this I need to define some functions:

matcher <- function(x,y){ return(na.omit(m[which(d[,y]==x),y]))  }

where x=Id, y=feature_column, the function gives the direct match score of an Id in particular column.

max_matcher <- function(x) { return(sum(matcher(x,3:13)))    }
For one Id appearing in top 20 of different columns will have a final match score as their sum.

 And finally, the best match,

result <- foreach(1:1000, function(x) {if(max(max_matcher(1:1000))==max_matcher(x))return(x)})

From 1:1000 Ids, it'll check for the one which has max match score and return the Id as the best match i.e our Quoran Twin.

We need to apply these functions groupwise, where each group is grouped according to id1 column.
Here how it is done :


m %>%
group_by(id1) %>% 
do(data.frame(result))

Find the entire code here! 





No comments:

Post a Comment