def learningAddIndicateFunctionlfm(n, alpha, lamd): global fileNameAllPuiArray p, q = initTrainMatrix() puiArray = paper.load(fileNameAllPuiArray) #0-1 puiArray = (puiArray - 44.034)/ 107.439 import random for step in range(0, n): number = 0 mae = 0 userList = range(userNum) while len(userList) > 0: u = random.choice(userList) del userList[userList.index(u)] itemList= range(itemNum) while len(itemList) > 0: i = random.choice(itemList) del itemList[itemList.index(i)] if puiArray[u, i] != 111: number += 1 #number 记录训练集中的评分总数 rui = puiArray[u, i] pui = predict(u, i, p, q) eui = rui - pui mae += abs(eui) #修正 p\q temp = p[u,] + alpha * (q[i, ] * eui - lamd * p[u,]) q[i,] += alpha * (p[u,] * eui - lamd * q[i,]) p[u,] = temp else: p[u,] -= alpha * lamd * p[u,] q[i,] -= alpha * lamd * q[i,] nowMae = mae / number print 'step: %d mae: %f' % ((step + 1), nowMae) alpha *= 0.9 return p, q
def loadPQ(*fileName): import paper p = paper.load(fileName[0]).A q = paper.load(fileName[1]).A return p, q
import sys import copy if __name__ == '__main__': simCalMethod = paper.simMinkowskiDist sparseness = 'sparseness5' fileNumbers = 1 K = 2 MAE = 0.0 RMSE = 0.0 #参数调整 0.519026063656 1.64540380436 #文件对象 for i in range(1, fileNumbers+1): #文件对象 trainFileName = 'dataSet/%s/training%d.txt' % (sparseness,i) testFileName = 'dataSet/%s/test%d.txt' % (sparseness,i) euiFileName = 'result/eui/euiNR-%s-test%d.txt' % (sparseness,i) pf = open(euiFileName, 'w') #load data testDict = paper.loadTestDataForDict(testFileName) trainArrayObj = paper.createArrayObj(trainFileName) mean = paper.calMean(trainArrayObj) wsAverageVector = paper.columnAverageArray(trainArrayObj, mean) #相似度矩阵数据 userSimFileName = 'result/simArraySlopeoneUser-%s-test%d.txt' % (sparseness,i) # userSimArrayObj = paper.createSimArray(trainArrayNorm, simCalMethod, 2.0) # paper.save(userSimArrayObj, userSimFileName) userSimArrayObj = paper.load(userSimFileName) #计算预测准确 print calMaeAndRmse() pf.close() print 'ok'
def loadPQTp(sparseness=5, number=1): pFile = r'throught/p-%d-%d.txt' % (sparseness, number) qFile = r'throught/q-%d-%d.txt' % (sparseness, number) p = paper.load(pFile) q = paper.load(qFile) return p, q