def predictLikes(userLikes): postIds = pickle.load(open ("postIds","rb")) indexes = pickle.load(open ("indexes","rb")) numberOfPosts = len(postIds) indexesHashMap = pickle.load(open ("indexesHashMap","rb")) numberOfNeighbors = len(indexesHashMap) usersVectorsArr,usersArr = createArr(indexesHashMap,numberOfNeighbors) nearestNumber = 7 neighbors = findNearestNeighbors(userLikes, numberOfNeighbors,nearestNumber,usersVectorsArr,usersArr) fullUsersHashMap = pickle.load(open ("fullUsersHash","rb")) averages = [None]*numberOfPosts for j in range(numberOfPosts): average = 0 weight = nearestNumber for i in range(nearestNumber): neighbor = neighbors[i] average += fullUsersHashMap[str(neighbor[1])][j]*weight weight -= 1 averages[j] = (average/28) #averages[j] = (average/nearestNumber) print(averages) testLikes = [None]*numberOfPosts counter = 0 for i in indexes: testLikes[i] = userLikes[counter] counter += 1 newIndexes =[] lastIndexes=[] filterNum = 0.0 while (len(newIndexes)<6): filterNum += 0.1 newIndexes = [] for num in range(numberOfPosts): if num not in indexes: if (averages[num]>= (1-filterNum)): newIndexes.append(num) #testLikes[num] = 1 #else: #testLikes[num] = 0 if len(newIndexes)>20: newIndexes = newIndexes[:20] print (filterNum) return newIndexes
PostLikeUsers = pickle.load(open ("PostLikeUsers","rb")) arr = pickle.load(open ("arr","rb")) indexes = pickle.load(open ("indexes","rb")) print(indexes) #randomIndex = list(random.sample(range(0,59),25)) usersHashMap = createHashMap(arr,sizeOfTrainingSet,indexes) print(len(usersHashMap)) #usersHashMap = pickle.load(open ("usersHash","rb")) #postIds = pickle.load(open ("postIds","rb")) #PostLikeUsers = pickle.load(open ("PostLikeUsers","rb")) numberOfPosts = len(postIds) numberOfNeighbors = len(usersHashMap) nearestNumber = 7 totalDiff = 0 usersVectorsArr,usersArr = createArr(usersHashMap,numberOfNeighbors) tmpUsersVectorsArr = deepcopy(usersVectorsArr) #the full data on users tmpUsersArr = deepcopy(usersArr) #only the likes array #indexes = list(range(sizeOfTrainingSet)) fullUsersHashmap = pickle.load(open ("fullUsersHash","rb")) print(len(fullUsersHashmap)) sample = list(random.sample(range(1,18315),5)) for numberOfTests in sample: tmpFullUsersHashmap = deepcopy(fullUsersHashmap) #print(numberOfTests) #test user userLikes = tmpUsersVectorsArr[numberOfTests] #print(userLikes) fullTestUser = tmpUsersArr[numberOfTests] #create temp arr without the test user