def maeAndRmseTp(p, q, testArrayObj, pfEui): euiList = [] for index in range(len(testArrayObj)): u = int(testArrayObj[index, 0]) i = int(testArrayObj[index, 1]) tui = testArrayObj[index, 2] pui = (p[u] * q[i].T)[0, 0] * 107.439 + 44.034 euiTemp = tui - pui euiList.append(euiTemp) pfEui.write(str(euiTemp) + '\n') paper.maeAndRmse(euiList)
def predWithPQTp(testDataArray, pf, p, q): euiList = [] for index in range(len(testDataArray)): u = int(testDataArray[index, 0]) i = int(testDataArray[index, 1]) tui = testDataArray[index, 2] pui = (p[u] * q[i].T)[0, 0] * 107.439 + 44.034 euiTemp = tui - pui pf.write(str(euiTemp) + '\n') euiList.append(euiTemp) paper.maeAndRmse(euiList)
def test1(self): import numpy as np fileIndex = 1 for i in xrange(1): # userEui = np.loadtxt(ROOT_DIRECTORY + "/userEui" + str(fileIndex) + ".txt") userEui = np.loadtxt(ROOT_DIRECTORY + "/userEui.txt") # wsEui = np.loadtxt(ROOT_DIRECTORY + "/wsEui" + str(fileIndex) + ".txt") wsEui = np.loadtxt(ROOT_DIRECTORY + "/wsEui.txt") fileIndex = fileIndex + 1 alpha = 0.4 count = 0 totalQos = 0 testArr = paper.loadTest(ROOT_DIRECTORY + '/test1.txt') minMae = 1000 minAlpha = 10 for i in xrange(11): eui = [] alpha = i / 10.0 totalQos = 0 for index, value in enumerate(userEui): totalQos = totalQos + testArr[index][2] eui.append(testArr[index][2] - alpha * value - (1 - alpha) * wsEui[index]) eui = np.array(eui) mae, rmse = paper.maeAndRmse(eui) if mae < minMae: minMae = mae minAlpha = alpha print "alpha:" + str(alpha) + "\t" + "MAE:" + str( mae) + "\tRMSE:" + str(rmse) + "\tNMAE:" + str( totalQos / len(testArr)) print "最小mae:" + str(minMae) + "\t alpha=" + str(minAlpha)
def calAverageMaeAndRmse(start=1, end=10, sparseness=5): maeSums = rmseSums = 0.0 for i in range(start, end + 1): # userEuiFile = r'rt/eui/slopeone-%d-%d.txt' % (sparseness,i) userEuiFile = r'throught/eui/slopeone-%d-%d.txt' % (sparseness, i) result = np.loadtxt(userEuiFile) mae, rmse = paper.maeAndRmse(result) maeSums += mae rmseSums += rmse print maeSums / (end - start + 1), rmseSums / (end - start + 1)
def findBestMatch(a, b): lamda = 0.0 minMae = sys.maxint minRmse = sys.maxint minLamda = -1 while lamda <= 1.0: result = a * lamda + b * (1 - lamda) print lamda, tempMae, tempRmse = paper.maeAndRmse(result) if tempMae < minMae: minMae = tempMae minRmse = tempRmse minLamda = lamda lamda += 0.1 print minLamda, minMae, minRmse
def test1(self): import numpy as np trainingArr = createArrayObj(ROOT_DIRECTORY + '/training1.txt', 339, 5825) simUserArr = np.loadtxt(ROOT_DIRECTORY + "/simUserArr.txt") simWsArr = np.loadtxt(ROOT_DIRECTORY + "/simWsArr.txt") webServerMapping = np.loadtxt(ROOT_DIRECTORY + "/webServerMapping.txt", dtype=str, delimiter="\t") maxSimUser = np.argsort(simUserArr, axis=1)[:, 0 - 10:] testArr = paper.loadTest(ROOT_DIRECTORY + '/test1.txt') #标签 pui = np.loadtxt(ROOT_DIRECTORY + "/label.txt") #标签-概率 probability = np.loadtxt(ROOT_DIRECTORY + "/probability.txt") # 取出相似度最高的k个用户 k_user = 10 k_ws = 10 result = [] fileIndex = 1 for i in xrange(1): k_user = 10 + i * 10 for j in xrange(1): k_ws = 50 + j * 10 simUser = np.argsort(simUserArr, axis=1)[:, 0 - k_user:] simWs = np.argsort(simWsArr, axis=1)[:, 0 - k_ws:] eui = [] # 用户-服务 for index, value in enumerate(testArr): uId = value[0] wsId = value[1] label = pui[index] fenzi = 0 fenmu = 0 # 相似用户-相似度 for i, otherUId in enumerate(simUser[uId]): #pow(simUserArr[uId][otherUId], 3) #simUserArr[uId][otherUId] if trainingArr[otherUId][wsId] != NoneValue and int( trainingArr[otherUId][wsId]) in label: fenzi += simUserArr[uId][otherUId] * trainingArr[ otherUId][wsId] * probability[otherUId][ np.argwhere(label == int( trainingArr[otherUId][wsId]))[0, 0]] fenmu += simUserArr[uId][otherUId] * probability[ otherUId][np.argwhere(label == int( trainingArr[otherUId][wsId]))[0, 0]] if fenmu != 0: predictValue = fenzi / fenmu r = predictValue - value[2] if r < 0: r = 0 - r eui.append(r) else: total = 0 count = 0 for i, otherUId in enumerate(maxSimUser[uId]): for m, n in enumerate(simWs[wsId]): # if webServerMapping[wsId][2] != webServerMapping[n][2] and webServerMapping[wsId][4] != webServerMapping[n][4]: # continue; #maxSimUser[uId][0] if trainingArr[otherUId][n] != NoneValue: count = count + 1 total += trainingArr[otherUId][n] if count != 0: avg = total / count if avg < 0: avg = 0 - avg eui.append(avg) print len(eui) eui = np.array(eui) mae, rmse = paper.maeAndRmse(eui) result.append("相似用户数:" + str(k_user) + "\t相似服务数:" + str(k_ws) + "\t" + "MAE:" + str(mae) + "\tRMSE:" + str(rmse)) print "相似用户数:" + str(k_user) + "\t相似服务数:" + str( k_ws) + "\t" + "MAE:" + str(mae) + "\tRMSE:" + str(rmse) # np.savetxt(ROOT_DIRECTORY + "/result.txt", result, fmt='%s', delimiter="\t") np.savetxt(ROOT_DIRECTORY + "/userEui.txt", eui, fmt='%s', delimiter="\t") # np.savetxt(ROOT_DIRECTORY + "/userEui" + str(fileIndex) + ".txt", eui, fmt='%s', delimiter="\t") fileIndex = fileIndex + 1
def getMaeAndRmse(a, b, lamda): result = a * lamda + b * (1 - lamda) mae, rmse = paper.maeAndRmse(result) return mae, rmse
def test1(self): import numpy as np trainingArr = createArrayObj(ROOT_DIRECTORY + '/training1.txt', 339, 5825) simArr = createSimArray(trainingArr, paper.simMinkowskiDist, 2) webServerMapping = np.loadtxt(ROOT_DIRECTORY + "/webServerMapping.txt", dtype=str, delimiter="\t") # 取出相似度最高的k个用户 k = 10 simUser = np.argsort(simArr, axis=1)[:, 0 - k:] testArr = paper.loadTest(ROOT_DIRECTORY + '/test1.txt') pui = np.loadtxt(ROOT_DIRECTORY + "/label.txt") probability = np.loadtxt(ROOT_DIRECTORY + "/probability.txt") eui = [] # 用户-服务 for index, value in enumerate(testArr): uId = value[0] wsId = value[1] label = pui[index] fenzi = 0 fenmu = 0 # 相似用户-相似度 for i, otherUId in enumerate(simUser[uId]): if trainingArr[otherUId][wsId] != NoneValue and int( trainingArr[otherUId][wsId]) in label: fenzi += simArr[uId][otherUId] * trainingArr[otherUId][ wsId] * probability[otherUId][np.argwhere( label == int(trainingArr[otherUId][wsId]))[0, 0]] fenmu += simArr[uId][otherUId] * probability[otherUId][ np.argwhere( label == int(trainingArr[otherUId][wsId]))[0, 0]] if fenmu != 0: predictValue = fenzi / fenmu eui.append(predictValue - value[2]) # else: # if webServerMapping[wsId - 1][2] != 'not found': # ii = 2 # else: # ii = 4 # score = [ ] # for g, h in enumerate(webServerMapping): # if h[ii] == webServerMapping[wsId - 1][ii]: # # 取出和i同个as的其他服务 # score.append(int(h[0])) # total = 0 # count = 0 # for i, otherUId in enumerate(simUser[uId]): # for m, n in enumerate(score): # if trainingArr[otherUId][n] != NoneValue: # count = count + 1 # total += trainingArr[otherUId][n] # if count != 0: # avg = total / count; # eui.append(avg) print len(eui) eui = np.array(eui) mae, rmse = paper.maeAndRmse(eui) print "MAE:" + str(mae) + "\tRMSE:" + str(rmse)
if not (int(info[-5]), int(info[-4])) in temp: result.append(info) result = sorted(result, key=lambda x: float(x[-1])) with open('euiAnalysis/euiFilter1.txt','w') as pf: for line in result: for elem in line: pf.write(str(elem) + '\t') pf.write('\n') def filters(): eui_c1 = r'euiAnalysis/euiWith-1-c1.txt' eui_NotGet = r'euiAnalysis/euiNotGet.txt' result = [] with open(eui_c1) as pf: for line in pf: info = line.strip().split() u = int(info[-5]) i = int(info[-4]) result.append((u, i)) with open(eui_NotGet) as pf: for line in pf: info = line.strip().split() u = int(info[-5]) i = int(info[-4]) result.append((u, i)) euiFilter(result) if __name__ == '__main__': paper.maeAndRmse([-1, 1, 2])