Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

100% complete p successful for user: solkc24@berkeley.edu ing for software updat

ID: 3916004 • Letter: 1

Question

100% complete p successful for user: solkc24@berkeley.edu ing for software updates... sup to date s-MacBook-Air:maps ryo$ python3 ok -q 84-u gnment: Project 2: Yelp Maps version v1.13.11 ocking tests each7 , type what you would expect the output to be. pe exit) to quit roblem 4 Suite 1Case 1 cases remaining: 13) - Atready unlocked Problem 4> Suite 1> Case 2 (cases remaining: 12) -Already unlocked- Problem 4> Suite 2> Case 1 (cases remaining: 11) > import tests.test functions as test >>> from recommend import * >>r1 -make restaurant ( Stiver, [4, 01, [ Pizza], 2, [1) >>> r2= make-restaurant ('La Burrita', t-1, 51, [.Mexican.1, 1, t1) >r3 make_ restaurant( Thai Basil', [-1, 31, [ Thai , 2, [1) >> centroids [13, 01, [e, 411 >>groups group_by centroid((ri, r2, r31, centroids) >> Len(groups) >>> [restaurant-name (r) I'Sliver'1 for r in groups[e]] # cluster containing Sliver >>> [restaurant-name (r) for r in groups lill # second cluster

Explanation / Answer

A Yelp-powered Restaurant Recommendation Program

from abstractions import *
from data import ALL_RESTAURANTS, CATEGORIES, USER_FILES, load_user_file
from ucb import main, trace, interact
from utils import distance, mean, zip, enumerate, sample
from visualize import draw_map

Phase 2: Unsupervised Learning
------------------------------


def find_closest(location, centroids):
"""Return the centroid in centroids that is closest to location.
If multiple centroids are equally close, return the first one.

>>> find_closest([3.0, 4.0], [[0.0, 0.0], [2.0, 3.0], [4.0, 3.0], [5.0, 5.0]])
[2.0, 3.0]
"""
# BEGIN Question 3
return min(centroids, key=lambda x: distance(location,x))
# END Question 3


def group_by_first(pairs):
"""Return a list of pairs that relates each unique key in the [key, value]
pairs to a list of all values that appear paired with that key.

Arguments:
pairs -- a sequence of pairs

>>> example = [ [1, 2], [3, 2], [2, 4], [1, 3], [3, 1], [1, 2] ]
>>> group_by_first(example)
[[2, 3, 2], [2, 1], [4]]
"""
keys = []
for key, _ in pairs:
if key not in keys:
keys.append(key)
return [[y for x, y in pairs if x == key] for key in keys]


def group_by_centroid(restaurants, centroids):
"""Return a list of clusters, where each cluster contains all restaurants
nearest to a corresponding centroid in centroids. Each item in
restaurants should appear once in the result, along with the other
restaurants closest to the same centroid.
"""
# BEGIN Question 4
new_pairs=[]
for res in restaurants:
new_pairs.append([find_closest(restaurant_location(res), centroids),res])
return group_by_first(new_pairs)
# END Question 4


def find_centroid(cluster):
"""Return the centroid of the locations of the restaurants in cluster."""
# BEGIN Question 5
longitudes = [restaurant_location(i)[0] for i in cluster]
latitudes = [restaurant_location(i)[1] for i in cluster]
return [mean(longitudes),mean(latitudes)]
# END Question 5


def k_means(restaurants, k, max_updates=100):
"""Use k-means to group restaurants by location into k clusters."""
assert len(restaurants) >= k, 'Not enough restaurants to cluster'
old_centroids, n = [], 0
# Select initial centroids randomly by choosing k different restaurants
centroids = [restaurant_location(r) for r in sample(restaurants, k)]

while old_centroids != centroids and n < max_updates:
old_centroids = centroids
# BEGIN Question 6
centroids = [find_centroid(i) for i in group_by_centroid(restaurants, old_centroids)]
# END Question 6
n += 1
return centroids

Phase 3: Supervised Learning
--------------------------------

def find_predictor(user, restaurants, feature_fn):
"""Return a rating predictor (a function from restaurants to ratings),
for a user by performing least-squares linear regression using feature_fn
on the items in restaurants. Also, return the R^2 value of this model.

Arguments:
user -- A user
restaurants -- A sequence of restaurants
feature_fn -- A function that takes a restaurant and returns a number
"""
reviews_by_user = {review_restaurant_name(review): review_rating(review)
for review in user_reviews(user).values()}

xs = [feature_fn(r) for r in restaurants]
ys = [reviews_by_user[restaurant_name(r)] for r in restaurants]

# BEGIN Question 7
mean_xs = mean(xs)
mean_ys = mean(ys)
sxx = sum([(i - mean_xs)**2 for i in xs])
syy = sum([(i - mean_ys)**2 for i in ys])
sxy = sum([(i[0]-mean_xs)*(i[1]-mean_ys) for i in zip(xs,ys)])
b = sxy/sxx
a = mean(ys) - b*mean(xs)
r_squared = sxy**2/(sxx*syy)
# END Question 7

def predictor(restaurant):
return b * feature_fn(restaurant) + a

return predictor, r_squared


def best_predictor(user, restaurants, feature_fns):
"""Find the feature within feature_fns that gives the highest R^2 value
for predicting ratings by the user; return a predictor using that feature.

Arguments:
user -- A user
restaurants -- A list of restaurants
feature_fns -- A sequence of functions that each takes a restaurant
"""
reviewed = user_reviewed_restaurants(user, restaurants)
# BEGIN Question 8
all_predictors = [find_predictor(user,reviewed,f) for f in feature_fns]
return max(all_predictors,key = lambda i: i[1])[0]
# END Question 8


def rate_all(user, restaurants, feature_fns):
"""Return the predicted ratings of restaurants by user using the best
predictor based on a function from feature_fns.

Arguments:
user -- A user
restaurants -- A list of restaurants
feature_fns -- A sequence of feature functions
"""
predictor = best_predictor(user, ALL_RESTAURANTS, feature_fns)
reviewed = user_reviewed_restaurants(user, restaurants)
# BEGIN Question 9
"*** YOUR CODE HERE ***"
return {restaurant_name(i): user_rating(user,restaurant_name(i)) if i in reviewed else predictor(i) for i in restaurants}
#from above, predictor is the return value of best_predictor...which is itself a function. and it needs an input. we want the highest R^2 for a given restaurant so do predictor(i)
# END Question 9


def search(query, restaurants):
"""Return each restaurant in restaurants that has query as a category.

Arguments:
query -- A string
restaurants -- A sequence of restaurants
"""
# BEGIN Question 10
return [i for i in restaurants if query in restaurant_categories(i)]
# END Question 10


def feature_set():
"""Return a sequence of feature functions."""
return [lambda r: mean(restaurant_ratings(r)),
restaurant_price,
lambda r: len(restaurant_ratings(r)),
lambda r: restaurant_location(r)[0],
lambda r: restaurant_location(r)[1]]


@main
def main(*args):
import argparse
parser = argparse.ArgumentParser(
description='Run Recommendations',
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument('-u', '--user', type=str, choices=USER_FILES,
default='test_user',
metavar='USER',
help='user file, e.g. ' +
'{{{}}}'.format(','.join(sample(USER_FILES, 3))))
parser.add_argument('-k', '--k', type=int, help='for k-means')
parser.add_argument('-q', '--query', choices=CATEGORIES,
metavar='QUERY',
help='search for restaurants by category e.g. '
'{{{}}}'.format(','.join(sample(CATEGORIES, 3))))
parser.add_argument('-p', '--predict', action='store_true',
help='predict ratings for all restaurants')
parser.add_argument('-r', '--restaurants', action='store_true',
help='outputs a list of restaurant names')
args = parser.parse_args()

# Output a list of restaurant names
if args.restaurants:
print('Restaurant names:')
for restaurant in sorted(ALL_RESTAURANTS, key=restaurant_name):
print(repr(restaurant_name(restaurant)))
exit(0)

# Select restaurants using a category query
if args.query:
restaurants = search(args.query, ALL_RESTAURANTS)
else:
restaurants = ALL_RESTAURANTS

# Load a user
assert args.user, 'A --user is required to draw a map'
user = load_user_file('{}.dat'.format(args.user))

# Collect ratings
if args.predict:
ratings = rate_all(user, restaurants, feature_set())
else:
restaurants = user_reviewed_restaurants(user, restaurants)
names = [restaurant_name(r) for r in restaurants]
ratings = {name: user_rating(user, name) for name in names}

# Draw the visualization
if args.k:
centroids = k_means(restaurants, min(args.k, len(restaurants)))
else:
centroids = [restaurant_location(r) for r in restaurants]
draw_map(centroids, restaurants, ratings)
  
  
"""Utilities for Maps"""

from math import sqrt
from random import sample

# Rename the built-in zip (http://docs.python.org/3/library/functions.html#zip)
_zip = zip

def map_and_filter(s, map_fn, filter_fn):
"""Returns a new list containing the results of calling map_fn on each
element of sequence s for which filter_fn returns a true value.

>>> square = lambda x: x * x
>>> is_odd = lambda x: x % 2 == 1
>>> map_and_filter([1, 2, 3, 4, 5], square, is_odd)
[1, 9, 25]
"""
# BEGIN Question 0
return [map_fn(i) for i in s if filter_fn(i)]
# END Question 0

def key_of_min_value(d):
"""Returns the key in a dict d that corresponds to the minimum value of d.

>>> letters = {'a': 6, 'b': 5, 'c': 4, 'd': 5}
>>> min(letters)
'a'
>>> key_of_min_value(letters)
'c'
"""
# BEGIN Question 0
return min(d,key=d.get)
# END Question 0

def zip(*sequences):
"""Returns a list of lists, where the i-th list contains the i-th
element from each of the argument sequences.

>>> zip(range(0, 3), range(3, 6))
[[0, 3], [1, 4], [2, 5]]
>>> for a, b in zip([1, 2, 3], [4, 5, 6]):
... print(a, b)
1 4
2 5
3 6
>>> for triple in zip(['a', 'b', 'c'], [1, 2, 3], ['do', 're', 'mi']):
... print(triple)
['a', 1, 'do']
['b', 2, 're']
['c', 3, 'mi']
"""
return list(map(list, _zip(*sequences)))

def enumerate(s, start=0):
"""Returns a list of lists, where the i-th list contains i+start and
the i-th element of s.

>>> enumerate([6, 1, 'a'])
[[0, 6], [1, 1], [2, 'a']]
>>> enumerate('five', 5)
[[5, 'f'], [6, 'i'], [7, 'v'], [8, 'e']]
"""
# BEGIN Question 0
return zip([start+i for i in range(len(s))],s)
# END Question 0

def distance(pos1, pos2):
"""Returns the Euclidean distance between pos1 and pos2, which are pairs.

>>> distance([1, 2], [4, 6])
5.0
"""
return sqrt((pos1[0] - pos2[0]) ** 2 + (pos1[1] - pos2[1]) ** 2)

def mean(s):
"""Returns the arithmetic mean of a sequence of numbers s.

>>> mean([-1, 3])
1.0
>>> mean([0, -3, 2, -1])
-0.5
"""
# BEGIN Question 1
assert len(s)>0, "Empty sequences are not allowed"
return sum(s)/len(s)
# END Question 1
  
"""Data Abstractions"""

from utils import mean


Phase 1: Data Abstraction
-----------------------------


# Reviews

def make_review(restaurant_name, rating):
"""Return a review data abstraction."""
return [restaurant_name, rating]

def review_restaurant_name(review):
"""Return the restaurant name of the review, which is a string."""
return review[0]

def review_rating(review):
"""Return the number of stars given by the review, which is a
floating point number between 1 and 5."""
return review[1]


# Users

def make_user(name, reviews):
"""Return a user data abstraction."""
return [name, {review_restaurant_name(r): r for r in reviews}]

def user_name(user):
"""Return the name of the user, which is a string."""
return user[0]

def user_reviews(user):
"""Return a dictionary from restaurant names to reviews by the user."""
return user[1]


### === +++ USER ABSTRACTION BARRIER +++ === ###

def user_reviewed_restaurants(user, restaurants):
"""Return the subset of restaurants reviewed by user.

Arguments:
user -- a user
restaurants -- a list of restaurant data abstractions
"""
names = list(user_reviews(user))
return [r for r in restaurants if restaurant_name(r) in names]

def user_rating(user, restaurant_name):
"""Return the rating given for restaurant_name by user."""
reviewed_by_user = user_reviews(user)
user_review = reviewed_by_user[restaurant_name]
return review_rating(user_review)


# Restaurants

def make_restaurant(name, location, categories, price, reviews):
"""Return a restaurant data abstraction containing the name, location,
categories, price, and reviews for that restaurant."""
# BEGIN Question 2
return [name, location, categories, price, reviews]
# END Question 2

def restaurant_name(restaurant):
"""Return the name of the restaurant, which is a string."""
# BEGIN Question 2
return str(restaurant[0])
# END Question 2

def restaurant_location(restaurant):
"""Return the location of the restaurant, which is a list containing
latitude and longitude."""
# BEGIN Question 2
return restaurant[1]
# END Question 2

def restaurant_categories(restaurant):
"""Return the categories of the restaurant, which is a list of strings."""
# BEGIN Question 2
return restaurant[2]
# END Question 2

def restaurant_price(restaurant):
"""Return the price of the restaurant, which is a number."""
# BEGIN Question 2
return restaurant[3]
# END Question 2

def restaurant_ratings(restaurant):
"""Return a list of ratings, which are numbers from 1 to 5, of the
restaurant based on the reviews of the restaurant."""
# BEGIN Question 2
return [review_rating(i) for i in restaurant[4]]
# END Question 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote