ndcg算法的实现
算法参考wiki百科 Discounted cumulative gain
首先是学长之前提供的代码
环境python2.7
这里的 from resource import log 一直通不过
查阅了资料现在是用 import logging
__author__ = 'chen'
import math
import pandas as pd
from resource import log
class NDCG(object):
"""
normalized discount cumulative gain
"""
logger = log.Logger().get_logger()
def __init__(self, topk=20):
self.topk = topk
def error(self, comp_matrix, spar_matrix, pred_matrix):
"""
here pred_matrix represnt the predict rating or ranking
"""
self.logger.info(str(self.__class__.__name__) + "\t error")
self.comp_matrix = comp_matrix.copy()
self.pred_matrix = pred_matrix.copy()
sel