Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import math
- history_data_file = open("history.txt", "r")
- queries_data_file = open("queries.txt", "r")
- history_data = [[int(x) for x in line.split()] for line in history_data_file]
- queries_data = [[int(x) for x in line.split()] for line in queries_data_file]
- #use mode => "r" which is read mode
- #extract data from history and form an array
- #extract data from queries
- #calculate the angles for the history data
- custNum = history_data[0][0]
- itemNum = history_data[0][1]
- transactionNum = history_data[0][2]
- vector_list = [[0]*int(custNum) for x in [0]*int(itemNum)]
- def calc_angle(x, y):
- norm_x = np.linalg.norm(x)
- norm_y = np.linalg.norm(y)
- cos_theta = np.dot(x, y) / (norm_x * norm_y)
- theta = math.degrees(math.acos(cos_theta))
- return theta
- def calc_vectors(data):
- #each vector will be custNum long
- #the list will be transactionNum long
- vectors = vector_list
- for x in data[1:]:
- vector_list[x[1]-1][x[0]-1] = 1
- return vector_list
- vector_data = (calc_vectors(history_data))
- #angle = calc_angle(vector_data[1], vector_data[2])
- #create a list for vector_list
- angles = vector_list
- #calculate all angles and add to a list
- def something():
- for x in range(itemNum):
- for y in range(custNum):
- if x != y:
- angles[x][y] = calc_angle(vector_data[x], vector_data[y])
- else:
- angles[x][y] = 0
- return angles
- print(vector_data)
- print(vector_list)
- print(len(angles))
- print(something())
- print(np.mean(something()))
- #calculate the vector angle but only if it hasnt been calculate before
- #added calculated angles to a list and before running the angle - check to
- #see if this angle is present
- #format to get it to two decimal places
- #if min_angle is less than 90*,
- #print item: item_id; match: match_id; angle: min_angle
- #otherwise no match is required -> output. item: item_id no match
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement