proffreda

recomend.py

Sep 25th, 2016
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. """A Yelp-powered Restaurant Recommendation Program"""
  2.  
  3. from abstractions import *
  4. #from data import ALL_RESTAURANTS, CATEGORIES, USER_FILES, load_user_file
  5. from utils import distance, mean, zip, enumerate, sample
  6.  
  7.  
  8. ##################################
  9. # Phase 2: Unsupervised Learning #
  10. ##################################
  11.  
  12.  
  13. def find_closest(location, centroids):
  14. """Return the centroid in centroids that is closest to location. If
  15. multiple centroids are equally close, return the first one.
  16.  
  17. >>> find_closest([3.0, 4.0], [[0.0, 0.0], [2.0, 3.0], [4.0, 3.0], [5.0, 5.0]])
  18. [2.0, 3.0]
  19. """
  20. # BEGIN Question 3
  21. "*** REPLACE THIS LINE ***"
  22. # END Question 3
  23.  
  24.  
  25. def group_by_first(pairs):
  26. """Return a list of pairs that relates each unique key in the [key, value]
  27. pairs to a list of all values that appear paired with that key.
  28.  
  29. Arguments:
  30. pairs -- a sequence of pairs
  31.  
  32. >>> example = [ [1, 2], [3, 2], [2, 4], [1, 3], [3, 1], [1, 2] ]
  33. >>> group_by_first(example)
  34. [[2, 3, 2], [2, 1], [4]]
  35. """
  36. keys = []
  37. for key, _ in pairs:
  38. if key not in keys:
  39. keys.append(key)
  40. return [[y for x, y in pairs if x == key] for key in keys]
  41.  
  42.  
  43. def group_by_centroid(restaurants, centroids):
  44. """Return a list of clusters, where each cluster contains all restaurants
  45. nearest to a corresponding centroid in centroids. Each item in
  46. restaurants should appear once in the result, along with the other
  47. restaurants closest to the same centroid.
  48. """
  49. # BEGIN Question 4
  50. "*** REPLACE THIS LINE ***"
  51. # END Question 4
  52.  
  53.  
  54. def find_centroid(cluster):
  55. """Return the centroid of the locations of the restaurants in cluster."""
  56. # BEGIN Question 5
  57. "*** REPLACE THIS LINE ***"
  58. # END Question 5
  59.  
  60.  
  61. def k_means(restaurants, k, max_updates=100):
  62. """Use k-means to group restaurants by location into k clusters."""
  63. assert len(restaurants) >= k, 'Not enough restaurants to cluster'
  64. old_centroids, n = [], 0
  65. # Select initial centroids randomly by choosing k different restaurants
  66. centroids = [restaurant_location(r) for r in sample(restaurants, k)]
  67.  
  68. while old_centroids != centroids and n < max_updates:
  69. old_centroids = centroids
  70. # BEGIN Question 6
  71. "*** REPLACE THIS LINE ***"
  72. # END Question 6
  73. n += 1
  74. return centroids
  75.  
  76.  
  77. ################################
  78. # Phase 3: Supervised Learning #
  79. ################################
  80.  
  81.  
  82. def find_predictor(user, restaurants, feature_fn):
  83. """Return a rating predictor (a function from restaurants to ratings),
  84. for a user by performing least-squares linear regression using feature_fn
  85. on the items in restaurants. Also, return the R^2 value of this model.
  86.  
  87. Arguments:
  88. user -- A user
  89. restaurants -- A sequence of restaurants
  90. feature_fn -- A function that takes a restaurant and returns a number
  91. """
  92. reviews_by_user = {review_restaurant_name(review): review_rating(review)
  93. for review in user_reviews(user).values()}
  94.  
  95. xs = [feature_fn(r) for r in restaurants]
  96. ys = [reviews_by_user[restaurant_name(r)] for r in restaurants]
  97.  
  98. # BEGIN Question 7
  99. "*** REPLACE THIS LINE ***"
  100. b, a, r_squared = 0, 0, 0 # REPLACE THIS LINE WITH YOUR SOLUTION
  101. # END Question 7
  102.  
  103. def predictor(restaurant):
  104. return b * feature_fn(restaurant) + a
  105.  
  106. return predictor, r_squared
  107.  
  108.  
  109. def best_predictor(user, restaurants, feature_fns):
  110. """Find the feature within feature_fns that gives the highest R^2 value
  111. for predicting ratings by the user; return a predictor using that feature.
  112.  
  113. Arguments:
  114. user -- A user
  115. restaurants -- A list of restaurants
  116. feature_fns -- A sequence of functions that each takes a restaurant
  117. """
  118. reviewed = user_reviewed_restaurants(user, restaurants)
  119. # BEGIN Question 8
  120. "*** REPLACE THIS LINE ***"
  121. # END Question 8
  122.  
  123.  
  124. def rate_all(user, restaurants, feature_fns):
  125. """Return the predicted ratings of restaurants by user using the best
  126. predictor based a function from feature_fns.
  127.  
  128. Arguments:
  129. user -- A user
  130. restaurants -- A list of restaurants
  131. feature_fns -- A sequence of feature functions
  132. """
  133. predictor = best_predictor(user, ALL_RESTAURANTS, feature_fns)
  134. reviewed = user_reviewed_restaurants(user, restaurants)
  135. # BEGIN Question 9
  136. "*** REPLACE THIS LINE ***"
  137. # END Question 9
  138.  
  139.  
  140. def search(query, restaurants):
  141. """Return each restaurant in restaurants that has query as a category.
  142.  
  143. Arguments:
  144. query -- A string
  145. restaurants -- A sequence of restaurants
  146. """
  147. # BEGIN Question 10
  148. "*** REPLACE THIS LINE ***"
  149. # END Question 10
  150.  
  151.  
  152. def feature_set():
  153. """Return a sequence of feature functions."""
  154. return [restaurant_mean_rating,
  155. restaurant_price,
  156. restaurant_num_ratings,
  157. lambda r: restaurant_location(r)[0],
  158. lambda r: restaurant_location(r)[1]]
Add Comment
Please, Sign In to add comment