以下是您可以使用的生成算法的示例。如果样本量非常大,您可能需要使用data.table
包和/或外部数据库对其进行优化。对于初学者来说,代码编写起来相对容易。
下面有12,000个用户和90个节目,以及5种不同类型的节目/用户。每个用户喜欢在他们的类别中的节目比在他们的类别之外的节目多7倍。结果数据框显示用户的估计聚类,用户聚类成员资格的概率以及特定节目与聚类关联的概率(由于列中的概率总计为1,因此需要对这些值进行归一化)。 This是这里使用的算法。
library(plyr)
#creates "true" values
trueclass = sample(5,12000,replace=TRUE)
sid.sample <-function(x){ sapply(x,function(x) sample(1:90,1,prob = rep(1,90)*1+((0:89)%%5 == (x-1))*6))}
df = data.frame(user = rep(1:12000,each = 4),sid = sid.sample(rep(trueclass,each=4)))
#create empty frames
k = 5
uids = unique(as.numeric(df$user))
sids = unique(df$sid)
#initialize probabilities
uclass = uprobs = rdply(function() {x=rep(0,k);x[sample(k,1)] = 1;return(x)},
.n = length(uids))[,-1]
sprobs = matrix(0,nrow = length(sids),ncol = k)
scounts = sprobs*0
row.to.max <- function(x) rep(1,length(x)) * (1:length(x) == which.max(x))
#priors for each group; initially make them unbiased
priors = rep(0.2,5)
#slow method that still works
#20 iterations
for (counter in 1:40){
print(counter)
#smoothing
scounts[,] = 1
#calculate show probabilities
for (i in 1:nrow(df)){
scounts[df[i,2],which.max(uclass[df[i,1],])]=scounts[df[i,2],which.max(uclass[df[i,1],])]+1
}
sprobs = apply(scounts,2,function(x) x/sum(x))
#to calculate user probabilities
uprobs[,] = 0
for (i in 1:nrow(df)){
uprobs[df[i,1],] = uprobs[df[i,1],] + log(sprobs[df[i,2],])
}
#convert from log to actual, and add prior
uprobs = t(apply(uprobs,1,function(x,priors,temperature){ x = x + log(priors);x=x-max(x);x=exp(x);x/sum(x)},priors = priors))
uclass = t(apply(uprobs,1,row.to.max))
priors = colSums(uclass)
#small bit of smoothing
priors = (priors+0.01)/sum(priors+0.01)
print(priors)
}
final.classes = apply(uclass,1,which.max)
table(trueclass,final.classes)
如果您访问亚马逊网站,您会看到推荐的书籍或其他内容。类似的事情可以做,并且k最近邻算法可以是一个。 –
目前还不清楚你为我们提供了什么,但我的直觉是数据很稀少。随机森林可能会奏效,但我怀疑你的大部分集群都是由更受欢迎的节目组成的(这对你的第一个请求来说可能是很好的)。对于第二个问题,例如购物篮分析,您不一定会划分数据,但形成可能的关系,例如{辛普森和Futurama} - > {Family Guy}可能会有用。 –
假设电视剧的数量比用户数少数量级,您可以使用遵循贝叶斯规则的生成模型。基本上是P(cluster_i)= prod(P(cluster_i(show_j))),并且您最初随机地将展示分配给不同数量的聚类。只要你有一个平滑的参数(例如,一个节目的集群成员的最小概率),你可能会好起来的。这样做有更好的方法,但这是最简单的方法之一。 –