短縮版:
StackOverflowと同様の設定があります。ユーザーは実績を取得します。私はSOよりもはるかに多くの実績を持っています。たとえば10kのオーダーで、各ユーザーは数百の実績を持っています。さて、ユーザーが試すべき次の成果をどのように推奨しますか(推奨しますか)?
ロングバージョン:
オブジェクトはdjangoでこのようにモデル化されています(重要な部分のみを表示しています):
class User(models.Model):
alias = models.ForeignKey(Alias)
class Alias(models.Model):
achievements = models.ManyToManyField('Achievement', through='Achiever')
class Achievement(models.Model):
points = models.IntegerField()
class Achiever(models.Model):
achievement = models.ForeignKey(Achievement)
alias = models.ForeignKey(Alias)
count = models.IntegerField(default=1)
私のアルゴリズムは、ログインしたユーザーと実績を共有している他のすべてのユーザーを見つけて、すべての実績を調べ、発生回数で並べ替えるだけです。
def recommended(request) :
user = request.user.get_profile()
// The final response
r = {}
// Get all the achievements the user's aliases have received
// in a set so they aren't double counted
achievements = set()
for alias in user.alias_set.select_related('achievements').all() :
achievements.update(alias.achievements.all())
// Find all other aliases that have gotten at least one of the same
// same achievements as the user
otherAliases = set()
for ach in achievements :
otherAliases.update(ach.alias_set.all())
// Find other achievements the other users have gotten in addition to
// the shared ones.
// And count the number of times each achievement appears
for otherAlias in otherAliases :
for otherAch in otherAlias.achievements.all() :
r[otherAch] = r.get(otherAch, 0) + 1
// Remove all the achievements that the user has already gotten
for ach in achievements :
r.pop(ach)
// Sort by number of times the achievements have been received
r = sorted(r.items(), lambda x, y: cmp(x[1], y[1]), reverse=True)
// Put in the template for showing on the screen
template_values = {}
template_values['achievements'] = r
ただし、実行にはFOREVERが必要であり、常にリスト全体が返されますが、これは不要です。ユーザーは、上位のいくつかの実績のみを追跡する必要があります。
したがって、他のアルゴリズムやコードの改善に関する推奨事項を歓迎します。レコメンデーションアルゴリズムを考え出すための私のシステムでの成果をお伝えします:)