def js_wp_xsd(xlz, yh_xsd_dd=False): dy_rq, dy_jx = js_dy_rq(xlz) print('-' * 80) print('Generate: items co-rated similarity matrix') dy_xsd_sz = {} dydyh_sj = cssj(dayinbushu=1000) for yh, dys in xlz.items(): for dy in dys: dy_xsd_sz.setdefault(dy, defaultdict(int)) for dy2 in dys: if dy == dy2: continue if yh_xsd_dd: dy_xsd_sz[dy][dy2] += 1 / math.log(1 + len(dys)) else: dy_xsd_sz[dy][dy2] += 1 dydyh_sj.js_sj() print('Generate:items co-rated similarity matrix.(Done)') print('-' * 80) dydyh_sj.js() print('-' * 80) print('Calculate: item-item similarity matrix') dy_xs_sz_sj = cssj(dayinbushu=1000) for dy, zj_wp in dy_xsd_sz.items(): dy1_cd = dy_rq[dy] for dy2, jx in zj_wp.items(): yh2_cd = dy_rq[dy2] dy_xsd_sz[dy][dy2] = jx / math.sqrt(dy1_cd * yh2_cd) dy_xs_sz_sj.js_sj() print('Calculate:item-item similarity matrix(Done)') print('-' * 80) dy_xs_sz_sj.js() return dy_xsd_sz, dy_rq, dy_jx
def jx_yh_xsd(xlsjb, yh_csb_xsd=False): print('-' * 80) print('Build: movie-users inverse table') dydyh = collections.defaultdict(set) dyymd = defaultdict(int) for yh, dys in xlsjb.items(): for dy in dys: dydyh[dy].add(yh) dyymd[dy] += 1 print('Build: movie-users inverse table(Done)') print('-' * 80) dy_zh = len(dydyh) print('total movie number = %d' % dy_zh) print('-' * 80) print('Generate:user co-rated movies similarity matrix') yhxsd_sz = {} dydyh_sj = cssj(dayinbushu=1000) for dy, yhm in dydyh.items(): for yh1 in yhm: yhxsd_sz.setdefault(yh1, defaultdict(int)) for yh2 in yhm: if yh1 == yh2: continue if yh_csb_xsd: yhxsd_sz[yh1][yh2] += 1 / math.log(1 + len(yhm)) else: yhxsd_sz[yh1][yh2] += 1 dydyh_sj.js_sj() print('Generate: user co-rated movies similarity matrix(Done)') print('-' * 80) dydyh_sj.js() print('-' * 80) print('Calculate matrix: user-user similarity ') yh_xsd_sz = cssj(dayinbushu=1000) for yh1, zj_yh in yhxsd_sz.items(): yh1_cd = len(xlsjb[yh1]) for yh2, js in zj_yh.items(): yh2_cd = len(xlsjb[yh2]) yhxsd_sz[yh1][yh2] = js / math.sqrt(yh1_cd * yh2_cd) yh_xsd_sz.js_sj() print('Calculate: user-user similarity(Done)') print('-' * 80) yh_xsd_sz.js() return yhxsd_sz, dyymd, dy_zh
def yuce(self, csj): dy_tj = defaultdict(list) print('-' * 80) print('start to predict scores ') yc_sj = csh.cssj(dayinbushu=500) for gd, yh in enumerate(csj): jl_sj = self.tuijian(yh) dy_tj[yh].append(jl_sj) yc_sj.js_sj() print('Predict scores(Done)') print('-' * 80) yc_sj.js() return dy_tj
def yuce(self, csj): dytuijian = defaultdict(list) print('-' * 80) print('start to predict scores ') ybyshijian = cssj(dayinbushu=500) for ni, yh in enumerate(csj): jl_dy = self.tuijian(yh) dytuijian[yh].append(jl_dy) ybyshijian.js_sj() print('Predict scores(Done)') print('-' * 80) ybyshijian.js() return dytuijian
def cs(self, csj): if not self.n_jl_dy or not self.xlj or not self.dy_rqz or not self.dy_hj: raise ValueError('Method has not called yet.') self.csj = csj print('-' * 80) print('Start to test recommendation system ') weizhi = self.n_jl_dy jida = 0 jl_hj = 0 cs_hj = 0 sy_jl_dy = set() rqzs = 0 cs_sj = cssj(dayinbushu=1000) for w, yh in enumerate(self.xlj): cs_dy = self.csj.get(yh, {}) jl_dy = self.tuijian(yh) for dy in jl_dy: if dy in cs_dy: jida += 1 sy_jl_dy.add(dy) rqzs += math.log(1 + self.dy_rqz[dy]) jl_hj += weizhi cs_hj += len(cs_dy) cs_sj.js_sj() qxd = jida / (1.0 * jl_hj) hs = jida / (1.0 * cs_hj) fg = len(sy_jl_dy) / (1.0 * self.dy_hj) zmd = rqzs / (1.0 * jl_hj) f_s=(qxd*hs*2.0)/(qxd+hs) print('Run test recommendation system(Done)') print('-' * 80) cs_sj.js() print('-' * 90) print('precision=%.4f\trecall=%.4f\tF-Measure1=%.4f\tcoverage=%.4f\tpopularity=%.4f\n' % (qxd, hs,f_s, fg, zmd)) print('-' * 90)
def cs(self, csj): if not self.n_jilu_dy or not self.xlji or not self.dy_zmd or not self.dy_jx: raise ValueError('Method has not called yet.') self.ceshiji = csj print('-' * 80) print('Start:Test recommendation system ') ni = self.n_jilu_dy jida = 0 jilu_jihe = 0 ceshi_jihe = 0 sy_jl_dy = set() rq_hj = 0 cs_sj = csh.cssj(dayinbushu=1000) for gd, yh in enumerate(self.xlji): cs_dy = self.ceshiji.get(yh, {}) jl_dy = self.tuijian(yh) for dy in jl_dy: if dy in cs_dy: jida += 1 sy_jl_dy.add(dy) rq_hj += math.log(1 + self.dy_zmd[dy]) jilu_jihe += ni ceshi_jihe += len(cs_dy) cs_sj.js_sj() xmd = jida / (1.0 * jilu_jihe) zhaohui = jida / (1.0 * ceshi_jihe) zy = len(sy_jl_dy) / (1.0 * self.dy_jx) rqz = rq_hj / (1.0 * jilu_jihe) f_s = (xmd * zhaohui * 2.0) / (xmd + zhaohui) print('Run test recommendation system(Done)') cs_sj.js() print('-' * 90) print( 'precision=%.4f\trecall=%.4f\tF-Measure1=%.4f\tcoverage=%.4f\tpopularity=%.4f\n' % (xmd, zhaohui, f_s, zy, rqz)) print('-' * 90)
def cs(self, csj): if not self.n_jl_dy or not self.xlj or not self.dy_zmd or not self.dy_hj: raise ValueError('Method has not called yet.') self.csj = csj print('-' * 80) print('Start to test recommendation system ') weizhi = self.n_jl_dy ddjd = 0 jl_zs = 0 cs_zs = 0 sy_ji_zs = set() rq_hj = 0 ceshishijian = cssj(dayinbushu=1000) for ni, yh in enumerate(self.xlj): ceshidy = self.csj.get(yh, {}) jl_dy = self.tuijian(yh) for dy in jl_dy: if dy in ceshidy: ddjd += 1 sy_ji_zs.add(dy) rq_hj += math.log(1 + self.dy_zmd[dy]) jl_zs += weizhi cs_zs += len(ceshidy) ceshishijian.js_sj() jqz = ddjd / (1.0 * jl_zs) sh = ddjd / (1.0 * cs_zs) fg = len(sy_ji_zs) / (1.0 * self.dy_hj) zmrq = rq_hj / (1.0 * jl_zs) f_s = (jqz * sh * 2.0) / (jqz + sh) print('Test recommendation system(Done)') ceshishijian.js() print('-' * 90) print( 'precision=%.4f\trecall=%.4f\tF-Measure1=%.4f\tcoverage=%.4f\tpopularity=%.4f\n' % (jqz, sh, f_s, fg, zmrq)) print('-' * 90)
print(str(zhi) +":" + dy_ditu[xiangmu]) def jianli_dy_dt(wenjian, shuju): jieuo = {} with open(wenjian, encoding='unicode_escape') as sjl: hangs = sjl.readlines() for hang in hangs: hdz = hang.strip().split(shuju) jieuo[hdz[0]] = hdz[1] return jieuo if __name__ == '__main__': xt_sj = cssj(danzi="Main Function") print('-' * 80) print('\t GU JIAKAI 20161795\tWEN JIALI 20174950\t LIU JIAQI 20173122') print('-' * 80 + '\n') print('\t\tRecommend System for Movielens ') print('-' * 70) print('\t 1:ml-100k\t2:ml-1m\t3:ml-25m') print('-' * 70 + '\n') a = int(input("Select Data set:")) sjj_mc = None mx_mc = None if a == 1: sjj_mc = 'ml-100k' elif a == 2: sjj_mc = 'ml-1m'