Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import numpy as np
  2. import math
  3.  
  4. history_data_file = open("history.txt", "r")
  5. queries_data_file = open("queries.txt", "r")
  6.  
  7. history_data = [[int(x) for x in line.split()] for line in history_data_file]
  8. queries_data = [[int(x) for x in line.split()] for line in queries_data_file]
  9. #use mode => "r" which is read mode
  10. #extract data from history and form an array
  11. #extract data from queries
  12. #calculate the angles for the history data
  13. custNum = history_data[0][0]
  14. itemNum = history_data[0][1]
  15. transactionNum = history_data[0][2]
  16. vector_list = [[0]*int(custNum) for x in [0]*int(itemNum)]
  17.  
  18.  
  19. def calc_angle(x, y):
  20. norm_x = np.linalg.norm(x)
  21. norm_y = np.linalg.norm(y)
  22. cos_theta = np.dot(x, y) / (norm_x * norm_y)
  23. theta = math.degrees(math.acos(cos_theta))
  24. return theta
  25.  
  26.  
  27. def calc_vectors(data):
  28. #each vector will be custNum long
  29. #the list will be transactionNum long
  30. vectors = vector_list
  31.  
  32. for x in data[1:]:
  33. vector_list[x[1]-1][x[0]-1] = 1
  34.  
  35. return vector_list
  36.  
  37.  
  38. vector_data = (calc_vectors(history_data))
  39. #angle = calc_angle(vector_data[1], vector_data[2])
  40.  
  41.  
  42. #create a list for vector_list
  43. angles = vector_list
  44.  
  45.  
  46. #calculate all angles and add to a list
  47. def something():
  48. for x in range(itemNum):
  49. for y in range(custNum):
  50. if x != y:
  51. angles[x][y] = calc_angle(vector_data[x], vector_data[y])
  52. else:
  53. angles[x][y] = 0
  54. return angles
  55.  
  56.  
  57. print(vector_data)
  58. print(vector_list)
  59. print(len(angles))
  60. print(something())
  61.  
  62.  
  63. print(np.mean(something()))
  64.  
  65.  
  66. #calculate the vector angle but only if it hasnt been calculate before
  67. #added calculated angles to a list and before running the angle - check to
  68. #see if this angle is present
  69. #format to get it to two decimal places
  70.  
  71. #if min_angle is less than 90*,
  72. #print item: item_id; match: match_id; angle: min_angle
  73. #otherwise no match is required -> output. item: item_id no match
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement