Esempio n. 1
0
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
    "*** REPLACE THIS LINE ***"
    pred_rest_name = [
        restaurant_name(restaurant) for restaurant in restaurants
    ]
    pred_rest_review = [predictor(restaurant) for restaurant in restaurants]
    pred_rate = {
        name: rating
        for name, rating in zip(pred_rest_name, pred_rest_review)
    }

    rest_name = [restaurant_name(restaurant) for restaurant in reviewed]
    rest_review = [user_rating(user, restaurant) for restaurant in rest_name]
    rate_user = {name: rating for name, rating in zip(rest_name, rest_review)}

    pred_rate.update(rate_user)

    return pred_rate
Esempio n. 2
0
def rate_all(user, restaurants, feature_fns):
    """Return the predicted ratings of restaurants by user using the best
    predictor based 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
    "*** REPLACE THIS LINE ***"

    
    #restaurant_ratings={name:reviewed(user,name) for name in reviewed}
    #user_reviewed=restaurant_ratings.keys()
    #restaurant_ratings.update({x:predictor(restaurants[x]) for x in restaurants if x not in user_reviewed})
    #return restaurant_ratings

    reviewed_restaurants= [restaurant_name(restaurant) for restaurant in reviewed]
    user_rest_ratings= [user_rating(user, restaurant) for restaurant in reviewed_restaurants]
    user_reviewed= {name: rating for name, rating in zip(reviewed_restaurants, user_rest_ratings)}

    all_restaurants = [restaurant_name(restaurant) for restaurant in restaurants]
    pred_rest_ratings = [predictor(restaurant) for restaurant in restaurants] 
    all_ratings_dict= {name: rating for name, rating in zip(all_restaurants, pred_rest_ratings)}

    
    
    all_ratings_dict.update(user_reviewed)
    
    return all_ratings_dict
Esempio n. 3
0
def deploy(machine=None):
    print green("当前机器:" + env.host)
    #如果指定机器,那就只上着一个
    if machine and not machine == env.host:
        return
    #关闭slb
    #print env.host
    for slb in env.slbList:
        utils.setSlb(env.slbServer, slb, 0)
    utils.runCmd("sleep 5")
    #备份
    utils.zip(env.online)
    utils.download(env.online, env.localPath)
    #上传
    utils.upload(env.source, env.target)
    #杀掉进程
    utils.stopProcess(env.aport)
    utils.runCmd("sleep 10")
    #解压缩
    utils.unzip(env.online)
    #开始进程
    utils.runCmd(env.start_process)
    #sleep 多少秒
    utils.runCmd("sleep " + env.sleep_time)
    #监控进程
    run(env.monitor_url)
    #上线slb
    for slb in env.slbList:
        utils.setSlb(env.slbServer, slb, 100)
Esempio n. 4
0
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
    rest_names = [restaurant_name(x) for x in restaurants]
    pred_rest_ratings = [predictor(x) for x in restaurants]
    dict_all = {
        name: rating
        for name, rating in zip(rest_names, pred_rest_ratings)
    }

    reviewed_names = [restaurant_name(x) for x in reviewed]
    user_rest_ratings = [
        user_rating(user, restaurant) for restaurant in reviewed_names
    ]
    dict_user = {
        name: rating
        for name, rating in zip(reviewed_names, user_rest_ratings)
    }
    dict_all.update(dict_user)

    return dict_all
Esempio n. 5
0
def find_centroid(cluster):
    """Return the centroid of the locations of the restaurants in cluster."""
    # BEGIN Question 5 (dont avoid the abstraction barrier)
    return [
        mean(zip(*[restaurant_location(r) for r in cluster])[0]),
        mean(zip(*[restaurant_location(r) for r in cluster])[1])
    ]
Esempio n. 6
0
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

    reviewed_rest_names = [restaurant_name(r) for r in reviewed]
    reviewed_rating = [user_rating(user, restaurant_name(r)) for r in reviewed]
    reviewed_all = zip(reviewed_rest_names, reviewed_rating)

    predicted_rest_names = [
        restaurant_name(r) for r in restaurants if r not in reviewed
    ]
    predicted_rating = [predictor(r) for r in restaurants if r not in reviewed]
    predicted_all = zip(predicted_rest_names, predicted_rating)

    combine_pairs = predicted_all + reviewed_all
    return dict(combine_pairs)
Esempio n. 7
0
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
    sx = [i - mean(xs) for i in xs]
    sy = [i - mean(ys) for i in ys]
    sxx = sum([k[0] * k[1] for k in zip(sx, sx)])
    syy = sum([k[0] * k[1] for k in zip(sy, sy)])
    sxy = sum([k[0] * k[1] for k in zip(sx, sy)])
    b = sxy / sxx
    a = mean(ys) - b * mean(xs)
    r_squared = sxy * sxy / (sxx * syy)

    # END Question 7

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

    return predictor, r_squared
Esempio n. 8
0
def main():
    url = 'http://jandan.net/ooxx'
    curPage = getHost.getMainUrl(url)
    utils.logger.info('please input nums you want catch (0~' + curPage + ') :')
    maxCatchPage = int(raw_input(""))
    while maxCatchPage > int(curPage) or maxCatchPage < 0:
        utils.logger.info('please input nums you want catch (0~' + curPage +
                          ') :')
        maxCatchPage = int(raw_input(""))

    utils.logger.info(
        'please 10input thread nums you want lanuch (n*5&less20):')
    threadCount = int(raw_input(""))
    while threadCount > 20 or threadCount < 0:
        utils.logger.info(
            'please input thread nums you want lanuch (n*5&less20):')
        threadCount = int(raw_input(""))

    start = time.clock()
    if curPage:
        myThread.createThread(threadCount, maxCatchPage, curPage)
        utils.zip('Img')
    else:
        print 'main() get tilUrls failed.'
    end = time.clock()
    utils.logger.info("process used time: %f s" % (end - start))
Esempio n. 9
0
def zip(online=None, machine=None):
    if not machine or not online:
        abort("机器、远程路径不能为空")
    print green("当前机器:" + env.host)
    if machine and not machine == env.host:
        return
    utils.zip(online)
Esempio n. 10
0
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
    """
    xs = [feature_fn(r) for r in restaurants]
    ys = [user_rating(user, restaurant_name(r)) for r in restaurants]

    # BEGIN Question 7
    xys = zip(xs, ys)
    meanx = mean(xs)
    meany = mean(ys)
    sxx = reduce(lambda s, x: pow(x - meanx, 2) + s, xs, 0)
    syy = reduce(lambda s, y: pow(y - meany, 2) + s, ys, 0)
    sxy = reduce(lambda s, xy: (xy[0] - meanx) * (xy[1] - meany) + s, xys, 0)
    b = sxy / sxx
    a = meany - b * meanx
    r_squared = pow(sxy, 2) / (sxx * syy)

    # END Question 7

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

    return predictor, r_squared
Esempio n. 11
0
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
    Sxx = sum([(x - mean(xs))**2 for x in xs])
    Syy = sum([(y - mean(ys))**2 for y in ys])
    Sxy = sum([(x - mean(xs)) * (y - mean(ys)) for x, y 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
Esempio n. 12
0
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
    "*** REPLACE THIS LINE ***"
    # b, a, r_squared = 0, 0, 0  # REPLACE THIS LINE WITH YOUR SOLUTION
    mean_x = mean(xs)
    mean_y = mean(ys)
    xys = zip(xs, ys)
    sxx = sum([(x-mean_x) * (x-mean_x) for x in xs])
    syy = sum([(y-mean_y) * (y-mean_y) for y in ys])
    sxy = sum([(x-mean_x) * (y-mean_y) for x, y in xys])
    b = sxy / sxx
    a = mean_y - b * mean_x
    r_squared = sxy * sxy / (sxx * syy)
    # END Question 7

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

    return predictor, r_squared
Esempio n. 13
0
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
    xx, yy, sxy = [elem - mean(xs) for elem in xs], [elem - mean(ys) for elem in ys], 0
    # computes the standard deviations of x and y
    sxx, syy = sum(x**2 for x in xx), sum(y**2 for y in yy)
    for a, b in zip(xx, yy):
        sxy += a*b
    
    # finds a and b to solve for predictor function
    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
Esempio n. 14
0
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
    "*** REPLACE THIS LINE ***"
    mean_x, mean_y = mean(xs), mean(ys)
    xx_list = [x - mean_x for x in xs]
    yy_list = [y - mean_y for y in ys]
    xy_list = zip(xx_list, yy_list)
    S_xx = sum([xx ** 2 for xx in xx_list])
    S_yy = sum([yy ** 2 for yy in yy_list])
    S_xy = sum([xx * yy for xx, yy in xy_list])
    b = S_xy / S_xx
    a = mean_y - b * mean_x
    r_squared = S_xy ** 2 / (S_xx * S_yy)    # END Question 7

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

    return predictor, r_squared
Esempio n. 15
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for 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]

    "*** YOUR CODE HERE ***"
    b, a, r_squared = 0, 0, 0  # REPLACE THIS LINE WITH YOUR SOLUTION
    

    mean_x = mean(xs) 
    mean_y = mean(ys)
    s_xx = sum([pow(x-mean_x,2) for x in xs]) 
    s_yy = sum([pow(y-mean_y,2) for y in ys]) 
    xy_pair = zip(xs,ys)
    s_xy = sum([(pair[0]-mean_x)*(pair[1]-mean_y) for pair in xy_pair]) 
    b = s_xy/s_xx 
    a = mean_y-b*mean_x
    r_squared = pow(s_xy,2)/(s_xx*s_yy)

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

    return predictor, r_squared
Esempio n. 16
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for 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
    """
    ## dictionary of (name: rating) pairs for a SINGLE user
    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]

    Sxx_list = [r - mean(xs) for r in xs]
    Syy_list = [r - mean(ys) for r in ys]
    Sxy_list = zip(Sxx_list, Syy_list)

    Sxx = sum([pow(r, 2) for r in Sxx_list])
    Syy = sum([pow(r, 2) for r in Syy_list])
    Sxy = sum([r[0] * r[1] for r in Sxy_list])

    b = Sxy / Sxx
    a = mean(ys) - b * mean(xs)
    r_squared = pow(Sxy, 2) / (Sxx * Syy)

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

    return predictor, r_squared
Esempio n. 17
0
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
    sum_squares = lambda x, y: sum([p[0] * p[1] for p in zip(x, y)])
    x = [x - mean(xs) for x in xs]
    y = [y - mean(ys) for y in ys]
    s_xx = sum_squares(x, x)
    s_yy = sum_squares(y, y)
    s_xy = sum_squares(x, y)
    b, r_squared = s_xy / s_xx, s_xy**2 / (
        s_xx * s_yy)  # REPLACE THIS LINE WITH YOUR SOLUTION
    a = mean(ys) - b * mean(xs)

    # END Question 7

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

    return predictor, r_squared
Esempio n. 18
0
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]

    S_xx, S_yy, S_xy = 0, 0, 0
    mean_x, mean_y = mean(xs), mean(ys)
    for x in xs:
        S_xx += (x - mean_x)**2
    for y in ys:
        S_yy += (y - mean_y)**2
    xsys = zip(xs, ys)
    for x, y in xsys:
        S_xy += (x - mean_x) * (y - mean_y)
    b, a, r_squared = S_xy / S_xx, mean_y - (S_xy / S_xx) * mean_x, S_xy**2 / (S_xx * S_yy)  

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

    return predictor, r_squared
Esempio n. 19
0
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
    S_xx = sum([(x - mean(xs))**2 for x in xs])
    S_yy = sum([(y - mean(ys))**2 for y in ys])
    S_xy = sum([(x - mean(xs)) * (y - mean(ys)) for x, y in zip(xs, ys)])

    b, a, r_squared = S_xy / S_xx, mean(ys) - (S_xy / S_xx) * mean(
        xs), S_xy**2 / (S_xx * S_yy)  # REPLACE THIS LINE WITH YOUR SOLUTION

    # END Question 7

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

    return predictor, r_squared
Esempio n. 20
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for 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]
    mean_xs = mean(xs)
    mean_ys = mean(ys)

    s_xx = sum([pow(x - mean_xs, 2) for x in xs])
    s_yy = sum([pow(y - mean_ys, 2) for y in ys])
    s_xy = sum([mul(z[0], z[1]) for z in zip([x - mean_xs for x in xs], [y - mean_ys for y in ys])])

    b = s_xy / s_xx
    a = mean_ys - b * mean_xs
    r_squared = pow(s_xy, 2) / mul(s_xx, s_yy)

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

    return predictor, r_squared
Esempio n. 21
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for `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
    "*** REPLACE THIS LINE ***"
    x_mean = mean(xs)
    y_mean = mean(ys)
    s_xx = sum([pow(x-x_mean,2) for x in xs])
    s_yy = sum([pow(y-y_mean,2) for y in ys])
    s_xy = sum([(x-x_mean)*(y-y_mean) for x,y in zip(xs,ys)])
    b, a, r_squared = s_xy/s_xx, y_mean - (s_xy/s_xx)*x_mean, (pow(s_xy,2))/(s_xx*s_yy)  # REPLACE THIS LINE WITH YOUR SOLUTION
    # END Question 7

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

    return predictor, r_squared
Esempio n. 22
0
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()}
    #print (reviews_by_user)

    xs = [feature_fn(r) for r in restaurants]
    ys = [reviews_by_user[restaurant_name(r)] for r in restaurants]
    joined_list = zip(xs, ys)
    # BEGIN Question 7
    meanx = mean(xs)
    meany = mean(ys)
    sxx = sum([(x-meanx)**2 for x in xs])
    syy = sum([(y-meany)**2 for y in ys])
    sxy = sum([(xy[0]-meanx)*(xy[1]-meany) for xy in joined_list])

    b = sxy/ sxx
    a = meany - b * meanx
    r_squared = sxy**2 / (sxx * syy)
    # END Question 7

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

    return predictor, r_squared
Esempio n. 23
0
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]

    x_avg, y_avg = mean(xs), mean(ys)
    Sxx, Syy = sum([(x-x_avg)**2 for x in xs]), sum([(y-y_avg)**2 for y in ys])
    x_y_pairs = zip(xs,ys)
    Sxy = sum([(pair[0]-x_avg)*(pair[1]-y_avg) for pair in x_y_pairs])

    b, r_squared = Sxy/Sxx, Sxy**2/(Sxx*Syy) 
    a = y_avg - b*x_avg

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

    return predictor, r_squared
Esempio n. 24
0
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
    s_xx, x_mean, s_yy, y_mean, s_xy = 0, mean(xs), 0, mean(ys), 0
    for i in xs:
        s_xx += (i-x_mean)**2
    for j in ys:
        s_yy += (j-y_mean)**2
    xy_list = zip(xs, ys)
    for i, j in xy_list:
        s_xy += (i-x_mean)*(j-y_mean)
    b = s_xy/s_xx
    a = y_mean - b*x_mean
    r_squared = s_xy**2 / (s_xx*s_yy)
    # END Question 7

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

    return predictor, r_squared
Esempio n. 25
0
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
    mean1 = mean(xs)
    mean2 = mean(ys)
    sxx = sum([pow(x - mean1, 2) for x in xs])
    syy = sum([pow(y - mean2, 2) for y in ys])
    sxy = sum([(x - mean1) * (y - mean2) for x, y in zip(xs, ys)])

    b, a, r_squared = sxy / sxx, mean2 - sxy / sxx * mean1, pow(
        sxy, 2) / (sxx * syy)

    # END Question 7

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

    return predictor, r_squared
Esempio n. 26
0
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
    """
    xs = [feature_fn(r) for r in restaurants]
    ys = [user_rating(user, restaurant_name(r)) for r in restaurants]

    # BEGIN Question 7
    "*** YOUR CODE HERE ***"

    mean_x = mean(xs)
    mean_y = mean(ys)

    S_xx = sum((x_i - mean_x)**2 for x_i in xs)
    S_yy = sum((y_i - mean_y)**2 for y_i in ys)
    S_xy = sum((x_i - mean_x) * (y_i - mean_y) for x_i, y_i in zip(xs, ys))

    b = S_xy / S_xx
    r_squared = S_xy**2 / (S_xx * S_yy)
    a = mean_y - b * mean_x

    # END Question 7

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

    return predictor, r_squared
Esempio n. 27
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for 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]

    "*** YOUR CODE HERE ***"
    b, a, r_squared = 0, 0, 0  # REPLACE THIS LINE WITH YOUR SOLUTION
    xs_mean = mean(xs)
    xs_minus_mean = [x - xs_mean for x in xs]
    ys_mean = mean(ys)
    ys_minus_mean = [y - ys_mean for y in ys]
    Sxx = sum([pow(x, 2) for x in xs_minus_mean])
    Syy = sum([pow(y, 2) for y in ys_minus_mean])
    Sxy = sum([x * y for x, y in zip(xs_minus_mean, ys_minus_mean)])
    b = Sxy / Sxx
    a = ys_mean - b * xs_mean
    r_squared = pow(Sxy, 2) / (Sxx * Syy)

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

    return predictor, r_squared
Esempio n. 28
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for 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]

    "*** YOUR CODE HERE ***"
    data_points=zip(xs, ys)
    S_xx, S_yy, S_xy = 0, 0, 0
    for x in xs:
        S_xx += pow((x - mean(xs)), 2)
    for y in ys:
        S_yy += pow((y - mean(ys)), 2)
    for x, y in data_points:
        S_xy += (x - mean(xs))*(y - mean(ys))

    b = S_xy/S_xx
    a, r_squared =  mean(ys)-b*mean(xs), pow(S_xy, 2)/(S_xx*S_yy)  # REPLACE THIS LINE WITH YOUR SOLUTION

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

    return predictor, r_squared
Esempio n. 29
0
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
    Sxx, Syy, Sxy = 0, 0, 0

    for x in xs:
        Sxx += (x - mean(xs)) ** 2
    for y in ys:
        Syy += (y - mean(ys)) ** 2
    for x,y in zip(xs, ys):
        Sxy += (x - mean(xs)) * (y - mean(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
Esempio n. 30
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for `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]

    Sxx=sum([(x-mean(xs))**2 for x in xs])
    Syy=sum([(y-mean(ys))**2 for y in ys])
    xy=zip([(x-mean(xs)) for x in xs], [(y-mean(ys)) for y in ys])
    Sxy=sum(r[0]*r[1] for r in xy)

    b=Sxy/Sxx
    a=mean(ys)-b*mean(xs)
    r_squared=(Sxy**2)/(Sxx*Syy)

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

    return predictor, r_squared
Esempio n. 31
0
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
    "*** REPLACE THIS LINE ***"
    mean_xs = mean(xs)
    mean_ys = mean(ys)
    list_x = [x - mean_xs for x in xs]
    list_y = [y - mean_ys for y in ys]
    sxx = sum( map(lambda x: x * x, list_x) )
    syy = sum( map(lambda y: y * y, list_y) )
    sxy = sum([a * b for a,b in zip (list_x, list_y)])

    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
Esempio n. 32
0
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]
    # xs -- a sequence of feature[3,5,6,7]
    ys = [reviews_by_user[restaurant_name(r)] for r in restaurants]
    # ys -- a sequence of reviews [2,3,4,6]
    # list(zip(ys, xs))-- [[2,3],[3,5],[6,4],[7,6]]
    # BEGIN Question 7
    # b, a, r_squared = 0, 0, 0

    pairs = zip(xs, ys)
    sxx = sum([(x[0] - mean(xs))**2 for x in pairs])
    syy = sum([(x[1] - mean(ys))**2 for x in pairs])
    sxy = sum([(x[0] - mean(xs)) * (x[1] - mean(ys)) for x in pairs])
    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
Esempio n. 33
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for 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]

    "*** YOUR CODE HERE ***"
    xx = sum([pow(xi - mean(xs), 2) for xi in xs])
    yy = sum([pow(yi - mean(ys), 2) for yi in ys])
    xy = sum([(xi - mean(xs)) * (yi - mean(ys)) for xi, yi in zip(xs, ys)])

    b = xy / xx
    a, r_squared = mean(ys) - b * mean(xs), pow(xy, 2) / (
        xx * yy)  # REPLACE THIS LINE WITH YOUR SOLUTION

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

    return predictor, r_squared
Esempio n. 34
0
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
    """
    xs = [feature_fn(r) for r in restaurants]
    ys = [user_rating(user, restaurant_name(r)) for r in restaurants]

    # BEGIN Question 7
    mean_x = mean(xs)
    mean_y = mean(ys)
    combined = zip(xs,ys)
    Sxx,Syy,Sxy = 0,0,0
    for sx,sy in combined:
        Sxx += (sx - mean_x) ** 2
        Syy += (sy - mean_y) ** 2
        Sxy += (sx - mean_x) * (sy - mean_y)
    b = Sxy / Sxx
    a = mean_y - b * mean_x
    r_squared = Sxy ** 2 / (Sxx * Syy)

    # END Question 7

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

    return predictor, r_squared
Esempio n. 35
0
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
    """
    xs = [feature_fn(r) for r in restaurants]
    ys = [user_rating(user, restaurant_name(r)) for r in restaurants]

    # BEGIN Question 7
    "*** YOUR CODE HERE ***"
    xs_zip_ys = zip(xs, ys)
    S_xx, S_yy, S_xy, x_mean, y_mean = 0, 0, 0, mean(xs), mean(ys)
    S_xx = sum([pow(x - x_mean, 2) for x in xs])
    S_yy = sum([pow(y - y_mean, 2) for y in ys])
    S_xy = sum([(x - x_mean) * (y - y_mean) for x, y in xs_zip_ys])

    b = S_xy / S_xx
    a = y_mean - b * x_mean
    r_squared = pow(S_xy, 2) / (S_xx * S_yy)

    # END Question 7

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

    return predictor, r_squared
Esempio n. 36
0
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
    """
    xs = [feature_fn(r) for r in restaurants]
    ys = [user_rating(user, restaurant_name(r)) for r in restaurants]

    # BEGIN Question 7
    xsandys = zip(xs, ys)
    sxx = sum([(the_xes - mean(xs))**2 for the_xes in xs])
    syy = sum([(the_ys - mean(ys))**2 for the_ys in ys])
    sxy = sum([(x - mean(xs)) * (y - mean(ys)) for x, y in xsandys])
    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
Esempio n. 37
0
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
    x_avg = mean(xs)
    y_avg = mean(ys)

    s_xx = sum((x - x_avg) * (x - x_avg) for x in xs)
    s_yy = sum((y - y_avg) *(y - y_avg) for y in ys)
    s_xy = sum((x - x_avg) * (y - y_avg) for x, y in zip(xs, ys))

    b = s_xy / s_xx
    a = y_avg - b * x_avg
    r_squared = (s_xy) * (s_xy) / (s_xx * s_yy) 
    # END Question 7

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

    return predictor, r_squared
Esempio n. 38
0
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_x = mean(xs)
    mean_y = mean(ys)
    S_xx = sum([(xi - mean_x) * (xi - mean_x) for xi in xs])
    S_yy = sum([(yi - mean_y) * (yi - mean_y) for yi in ys])
    S_xy = sum([(xi - mean_x) * (yi - mean_y) for xi, yi in zip(xs, ys)])

    b = S_xy / S_xx
    a = mean_y - b * mean_x
    r_squared = (S_xy * S_xy) / (S_xx * S_yy)

    # END Question 7

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

    return predictor, r_squared
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
    """
    xs = [feature_fn(r) for r in restaurants]
    ys = [user_rating(user, restaurant_name(r)) for r in restaurants]

    # BEGIN Question 7
    "*** YOUR CODE HERE ***"
    mean_xs = mean(xs)
    mean_ys = mean(ys)
    xs_ys = zip(xs, ys)
    sxx = sum([(x - mean_xs)**2 for x in xs])
    syy = sum([(x - mean_ys)**2 for x in ys])
    sxy = sum([(x[0] - mean_xs) * (x[1] - mean_ys) for x in xs_ys])
    b = sxy / sxx
    a = mean_ys - b * mean_xs
    r_squared = (sxy * sxy) / (sxx * syy)

    # END Question 7

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

    return predictor, r_squared
Esempio n. 40
0
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
    """
    xs = [feature_fn(r) for r in restaurants]
    ys = [user_rating(user, restaurant_name(r)) for r in restaurants]

    S_xx = sum([(i - mean(xs))**2 for i in xs])
    S_yy = sum([(j - mean(ys))**2 for j in ys])
    S_xy = sum([(i - mean(xs)) * (j - mean(ys)) for i, j in zip(xs, ys)])

    b = S_xy / S_xx
    a = mean(ys) - b * mean(xs)
    r_squared = (S_xy**2) / (S_xx * S_yy)

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

    return predictor, r_squared
Esempio n. 41
0
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
    """
    xs = [feature_fn(r) for r in restaurants]
    ys = [user_rating(user, restaurant_name(r)) for r in restaurants]

    # BEGIN Question 7
    "*** YOUR CODE HERE ***"
    sxlist =[x - mean(xs) for x in xs]
    sylist = [y - mean(ys) for y in ys]
    sxylist = zip(sxlist,sylist)

    Sxx = sum(pow(x,2) for x in sxlist)
    Syy = sum(pow(y,2) for y in sylist)
    Sxy = sum(a[0] * a[1] for a in sxylist)

    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
Esempio n. 42
0
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
    sxx=sum([((x - mean(xs))*(x - mean(xs))) for x in xs])
    syy=sum([((y - mean(ys))*(y - mean(ys))) for y in ys])
    sx=[(x-mean(xs)) for x in xs]
    sy=[(y-mean(ys)) for y in ys]
    
    sxy=sum(part[0]*part[1] for part in zip(sx,sy))
    	
    b, a, r_squared = sxy/sxx, mean(ys)-sxy*mean(xs)/sxx, sxy*sxy/(sxx*syy)  # REPLACE THIS LINE WITH YOUR SOLUTION
    # END Question 7

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

    return predictor, r_squared
Esempio n. 43
0
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
    "*** REPLACE THIS LINE ***"
    #S_xx computes the sum value for xs 
    S_xx = sum([pow(x - mean(xs), 2) for x in xs]) 
    #S_yy computes the sum value for ys
    S_yy = sum([pow(y - mean(ys), 2) for y in ys])
    #S_xy computes the sum value for xy
    S_xy = sum([ (x - mean(xs)) * (y - mean(ys)) for x,y in zip(xs, ys)])

    b = S_xy / S_xx 
    a = mean(ys) - b * mean(xs) 
    r_squared = pow(S_xy, 2)/(S_xx * S_yy)  # REPLACE THIS LINE WITH YOUR SOLUTION

    # END Question 7

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

    return predictor, r_squared
Esempio n. 44
0
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
    #b, a, r_squared = 0, 0, 0  # REPLACE THIS LINE WITH YOUR SOLUTION
    mean_zipped = zip([(x - mean(xs)) for x in xs], [(y - mean(ys)) for y in ys])
    sxx = sum([((x - mean(xs)) ** 2) for x in xs])
    syy = sum([((y - mean(ys)) ** 2) for y in ys])
    sxy = sum((elem[0] * elem[1]) for elem in mean_zipped)
    b = sxy / sxx
    a = mean(ys) - ((sxy/sxx) * mean(xs))
    r_squared = (sxy * sxy) / (sxx * syy)
    # END Question 7

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

    return predictor, r_squared
Esempio n. 45
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for `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_x=mean(xs)
    mean_y=mean(ys)
    S_xx, S_xy, S_yy = 0, 0, 0
    for elem in xs:
        S_xx += (elem-mean_x) ** 2
    for elem in ys:
        S_yy += (elem-mean_y) ** 2
    for x,y in zip(xs,ys):
        S_xy+=(x-mean_x) * (y-mean_y)
    b = S_xy/S_xx
    a = mean_y-b * mean_x
    r_squared = S_xy ** 2 / (S_xx * S_yy)  # REPLACE THIS LINE WITH YOUR SOLUTION
    # END Question 7

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

    return predictor, r_squared
Esempio n. 46
0
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
    """
    xs = [feature_fn(r) for r in restaurants]
    ys = [user_rating(user, restaurant_name(r)) for r in restaurants]

    # BEGIN Question 7
    inside_x = [(r - mean(xs)) for r in xs]
    inside_y = [(r - mean(ys)) for r in ys]
    combine_xy = zip(inside_x, inside_y)

    sxx = sum([r ** 2 for r in inside_x])
    syy = sum([r ** 2 for r in inside_y])
    sxy = sum([r[0] * r[1] for r in combine_xy])

    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
Esempio n. 47
0
def group_by_centroid(restaurants, centroids):
    """Return a list of lists, where each list contains all restaurants nearest
    to some item in CENTROIDS. Each item in RESTAURANTS should appear once in
    the result, along with the other restaurants nearest to the same centroid.
    No empty lists should appear in the result.
    """
    "*** YOUR CODE HERE ***"
    cent_list = [find_closest(restaurant_location(restaurant), centroids) for restaurant in restaurants]
    return group_by_first(zip(cent_list, restaurants))
Esempio n. 48
0
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
    closest_centroid = [find_closest(restaurant_location(x), centroids) for x in restaurants]
    restaurants_and_their_centroids = zip(closest_centroid, restaurants)
    return group_by_first(restaurants_and_their_centroids)
Esempio n. 49
0
def group_by_centroid(events, centroids):
    """Return a list of lists, where each list contains all restaurants nearest
    to some item in CENTROIDS. Each item in RESTAURANTS should appear once in
    the result, along with the other restaurants nearest to the same centroid.
    No empty lists should appear in the result.
    """
    closestcentroid = [find_closest(event, centroids) for event in events]
    result = zip(closestcentroid, events)
    cluster = group_by_first(result)
    return cluster
Esempio n. 50
0
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
    locations = [restaurant_location(y) for y in restaurants]
    closestcentroid=[find_closest(x,centroids) for x in locations]
    result = zip(closestcentroid,restaurants)
    clusterlist = group_by_first(result)
    return clusterlist
Esempio n. 51
0
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]

#    print(xs)
#    print(ys)
    # BEGIN Question 7
    mean_x = mean(xs)
    mean_y = mean(ys)
    S_xx = sum([(i - mean_x)**2 for i in xs])
    S_yy = sum([(i - mean_y)**2 for i in ys])

#    print([(i - mean_x)**2 for i in xs])
#    print([(i - mean_y)**2 for i in ys])
    zipped_xy = zip(xs, ys)
    S_xy = sum([(i[0] - mean_x) * (i[1] - mean_y) for i in zipped_xy])

#    print(S_xx)
#    print(S_yy)
    b = S_xy/S_xx
    a, r_squared = mean_y - b*mean_x, (S_xy**2) / (S_xx * S_yy)
#    print(b)
#    print(a)
#    print(r_squared)# REPLACE THIS LINE WITH YOUR SOLUTION
    # END Question 7

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

    return predictor, r_squared
Esempio n. 52
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for 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]

    "*** YOUR CODE HERE ***"
    mean_x = mean(xs)
    mean_y = mean(ys)
    xs1 = xs
    ys1 = ys

    tempx = [pow(x - mean_x, 2) for x in xs]
    
    tempy = [pow(y - mean_y, 2) for y in ys]
    
    tempx1 = [(x - mean_x) for x in xs1]
    
    tempy1 = [(y - mean_y) for y in ys1]
    
    tempx1y1 = [a*b for a,b in zip(tempx1, tempy1)]

    s_xx = sum(tempx)
    s_yy = sum(tempy)
    s_xy = sum(tempx1y1)
    b, r_squared = (s_xy) / (s_xx), (pow(s_xy, 2))/(s_xx * s_yy)
    a = (mean(ys) - b * mean(xs))

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

    return predictor, r_squared
Esempio n. 53
0
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 dictionary from restaurant names to restaurants
    feature_fns -- A sequence of functions that each takes a restaurant
    """
    reviewed = list(user_reviewed_restaurants(user, restaurants).values())
    "*** YOUR CODE HERE ***"
    predicter_list, r_squared_list=[], []
    for fn in feature_fns:
        x, y = find_predictor(user, reviewed, fn)
        predicter_list.append(x)
        r_squared_list.append(y)
    zipped=zip(predicter_list, r_squared_list)
    return max(zipped, key=lambda x: x[1])[0]

    """r_squared_list = []
Esempio n. 54
0
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

    # Sxx = Σi(xi - mean(x))^2
    # Syy = Σi(yi - mean(y))^2
    # Sxy = Σi(xi - mean(x))(yi - mean(y))

    # b = Sxy / Sxx
    # a = mean(y) - b * mean(x)
    # R2 = Sxy2 / (Sxx * Syy)

    s_xx = sum([(x - mean(xs)) * (x - mean(xs)) for x in xs])
    s_yy = sum([(y - mean(ys)) * (y - mean(ys)) for y in ys])
    s_xy = sum([(x - mean(xs)) * (y - mean(ys)) for x, y in zip(xs, ys)])

    b = s_xy / s_xx
    a = mean(ys) - b * mean(xs)
    r_squared = s_xy * s_xy / (s_xx * s_yy)
    # END Question 7

    def predictor(restaurant):
        return b * feature_fn(restaurant) + a #给出一个餐馆,和一个特征值,能够推断分数

    return predictor, r_squared
Esempio n. 55
0
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
    """
    import math

    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
    
    xy = zip(xs, ys)

    #print('a', user)
    print(114, restaurants)
    #print('b', xs, ys)
    Sxx = sum([(x - mean(xs)) ** 2 for x in xs])
    Syy = sum([(y - mean(ys)) ** 2 for y in ys])
    Sxy = sum([(x - mean(xs)) * (y - mean(ys)) for x, y in xy])

    #b, a, r_squared = 0, 0, 0  # REPLACE THIS LINE WITH YOUR SOLUTION
    b = Sxy / Sxx
    a = mean(ys) - b * mean(xs)
    r_squared = math.sqrt(Sxy * Sxy / (Sxx * Syy))    

    # END Question 7

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

    return predictor, r_squared
Esempio n. 56
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for 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]

    "*** YOUR CODE HERE ***"

    mean_x = mean(xs)
    mean_y = mean(ys)
    lst = zip(xs, ys)

    Sxx, Syy, Sxy = 0, 0, 0
    for each in lst:
        x = each[0]
        y = each[1]
        Sxx += (x-mean_x)*(x-mean_x)
        Syy += (y-mean_y)*(y-mean_y)
        Sxy += (x-mean_x)*(y-mean_y)

    b = Sxy / Sxx
    a = mean_y - b*mean_x
    r_squared = (Sxy*Sxy) /(Sxx*Syy)

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

    return predictor, r_squared
Esempio n. 57
0
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
    xmean = mean(xs)
    Sxxvalue = [(x-xmean)**2 for x in xs]
    Sxx = sum(Sxxvalue)
    ymean = mean(ys)
    Syyvalue = [(y-ymean)**2 for y in ys]
    Syy = sum(Syyvalue)
    Sxymeanx = [(x-xmean) for x in xs]
    Sxymeany = [(y-ymean) for y in ys]
    zipmeans=zip(Sxymeanx,Sxymeany)
    final = [x*y for (x,y) in zipmeans]
    Sxy = sum(final)
    b, r_squared = Sxy/Sxx, Sxy**2/(Sxx*Syy) 
    a = ymean-(b*xmean) 

    # END Question 7

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

    return predictor, r_squared
Esempio n. 58
0
def find_predictor(user, restaurants, feature_fn):
    """Return a rating predictor (a function from restaurants to ratings),
    for 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]

    "*** YOUR CODE HERE ***"
    mean_x, mean_y = mean(xs), mean(ys)
    S_xx = 0
    S_yy = 0
    S_xy = 0
    
    for x, y in zip(xs, ys):
        S_xx = S_xx + pow(x - mean_x, 2)
        S_yy = S_yy + pow(y - mean_y, 2)
        S_xy = S_xy + (x - mean_x)*(y - mean_y)

 
    b = S_xy/S_xx 
    a = mean_y - (b*mean_x)
    r_squared = (S_xy**2)/(S_xx*S_yy)

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

    return predictor, r_squared
         pid = path.join(pt,d)
         if path.exists(pid) :
            po = pid.replace(pt,pc)
            createDirectories(po)
            copyFiles(pid,po)
            print '    +> '+pid

      pid = path.join(pt,'config')
      if path.exists(pid):
         po = pid.replace(pt,pc)
         createDirectories(po)
         copyFile(options.configFile,po)
         print '... finally copying ' + options.configFile

      print '\n... now packaging ' + cfgname
      zip(cfgname,pc,cfg['ZIPPER'])

      print '\n... now cleaning '
      removeDirectories(pc)

   if options.archiveName != '':
      print '\n... now packaging ' + cfgname + ' into ' + archive
      zip(path.basename(archive),archive,cfg['ZIPPER']) # /!\ use the last cfg value

      print '\n... now cleaning ' + archive
      removeDirectories(archive)

# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# ~~~~ Jenkins' success message ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   print '\n\nMy work is done\n\n'
Esempio n. 60
0
    msg = "Test: runned= %d succes= %d failed= %d <br>" % (
        tests_results["tests"],
        tests_results["success"],
        tests_results["tests"] - tests_results["success"],
    )
    msg += "Failures: Txt %d Tex %d Image %d <br>" % (
        tests_results["txt_failure"],
        tests_results["tex_failure"],
        tests_results["img_failure"],
    )
    msg += "<br> Link: %s/%s.html" % (link, gpu)
    if branch != 0:
        msg += "<br>Framewok: %s %s" % (branch, revision)

    utils.call("python", "mail.py", recipients, subject, msg)
    print
    print


print "*** DAVA AUTOTEST Copy results for artifact storing ***"
print "Copy results for storing in TC %s -> %s" % (output, currentDir + "/Artifacts/" + gpu)
shutil.copytree(output, currentDir + "/Artifacts/" + gpu)
for test in os.listdir(results):
    shutil.copytree(input + "/" + test, currentDir + "/Artifacts/" + gpu + "/" + test + "/input/")

    shutil.copytree(results + "/" + test, currentDir + "/Artifacts/" + gpu + "/" + test + "/expected_results/")

print "*** DAVA AUTOTEST Zip results for artefacts ***"
os.chdir(currentDir + "/Artifacts/")
utils.zip(gpu, gpu)