7
다른 사용자로부터 항목 등급을 받아야한다고 생각한 Weighted slope one algorithm (이상 공식적으로는 here (PDF))에 대해 읽었으며 적어도 1 개의 평가와 1 개의 누락 값을 포함하는 사용자 벡터가있는 경우 누락 된 등급.가중 기울기 알고리즘 하나? (Python에서 R로 이식)
나는 Python implementation of the algorithm을 찾았지만 R (나는 더 편안함)으로 이식하는 데 어려움을 겪고 있습니다. 아래는 나의 시도입니다. 어떻게 작동 시키는가에 대한 제안?
미리 감사드립니다.
# take a 'training' set, tr.set and a vector with some missing ratings, d
pred=function(tr.set,d) {
tr.set=rbind(tr.set,d)
n.items=ncol(tr.set)
# tally frequencies to use as weights
freqs=sapply(1:n.items, function(i) {
unlist(lapply(1:n.items, function(j) {
sum(!(i==j)&!is.na(tr.set[,i])&!is.na(tr.set[,j])) })) })
# estimate product-by-product mean differences in ratings
diffs=array(NA, dim=c(n.items,n.items))
diffs=sapply(1:n.items, function(i) {
unlist(lapply(1:n.items, function(j) {
diffs[j,i]=mean(tr.set[,i]-tr.set[,j],na.rm=T) })) })
# create an output vector with NAs for all the items the user has already rated
pred.out=as.numeric(is.na(d))
pred.out[!is.na(d)]=NA
a=which(!is.na(pred.out))
b=which(is.na(pred.out))
# calculated the weighted slope one estimate
pred.out[a]=sapply(a, function(i) {
sum(unlist(lapply(b,function (j) {
sum((d[j]+diffs[j,i])*freqs[j,i])/rowSums(freqs)[i] }))) })
names(pred.out)=colnames(tr.set)
return(pred.out) }
# end function
# test, using example from [3]
alice=c(squid=1.0, octopus=0.2, cuttlefish=0.5, nautilus=NA)
bob=c(squid=1.0, octopus=0.5, cuttlefish=NA, nautilus=0.2)
carole=c(squid=0.2, octopus=1.0, cuttlefish=0.4, nautilus=0.4)
dave=c(squid=NA, octopus=0.4, cuttlefish=0.9, nautilus=0.5)
tr.set2=rbind(alice,bob,carole,dave)
lucy2=c(squid=0.4, octopus=NA, cuttlefish=NA, nautilus=NA)
pred(tr.set2,lucy2)
# not correct
# correct(?): {'nautilus': 0.10, 'octopus': 0.23, 'cuttlefish': 0.25}
나는 더 읽기 쉽도록 코드를 포맷하려고했지만 R은 나에게 익숙하지 않다. 좋은 스타일이 아니라면 미안 해요. – ephemient