@@ -0,0 +1,2476 @@
+def predict(predictions_vector_0, predictions_vector_1, mixing_variable=0.5, num_partitions=30):%0A    %22%22%22Apply a simple linear hybrid recommender.%0A%0A    This function implements the simple linear hybrid recommender Zhou et. al:%0A%0A    %22Solving the apparent diversity-accuracy dilemma of recommender systems%22%0A    http://arxiv.org/pdf/0808.2670.pdf Equation 5%0A%0A    It takes the weighted linear average of two previous recommendations and%0A    uses the new average as its own predicted rating:%0A%0A        new_rating = mixing_variable * rating_a + (1 - mixing_variable) * rating_b%0A%0A    Args:%0A        predictions_vector_0 (rdd): Contains prediction tuples of the form%0A            (user_id, item_id, predicted_rating) generated from a recommender%0A            algorithm.%0A        predictions_vector_1 (rdd): Contains prediction tuples of the form%0A            (user_id, item_id, predicted_rating) generated from a second%0A            recommender algorithm.%0A        mixing_variable (float): A float in the range %5B0., 1.%5D which determines%0A            how to weight the two predictions. If %60mixing_variable%60 is 0 then%0A            %60predictions_vector_0%60 is given all the weight (and%0A            %60predictions_vector_1%60 is ignored). If %60mixing_variable%60 is 1 then%0A            %60predictions_vector_1%60 is given all the weight (and%0A            %60predictions_vector_0%60 is ignored). Defaults to 0.5.%0A        num_partitions (int): The number of partitions to use for the returned%0A            data. Defaults to 30.%0A%0A    Returns:%0A        rdd: An rdd containing prediction tuples of the form%0A            (user_id, item_id, rating)%0A%0A    Raises:%0A        ValueError: If %60mixing_variable%60 is not within the range %5B0, 1%5D%0A%0A    %22%22%22%0A    # Check the mixing_variable is set to an acceptable value%0A    if not 0 %3C= mixing_variable %3C= 1:%0A        raise ValueError('mixing_variable must be within the range %5B0, 1%5D')%0A%0A    # Short-circuit in the trivial cases%0A    if mixing_variable == 0:%0A        return predictions_vector_0%0A    elif mixing_variable == 1:%0A        return predictions_vector_1%0A%0A    # Otherwise calculate the linear average%0A    keyed_vector_0 = predictions_vector_0%5C%0A        .map(lambda (u, i, r): ((u, i), r))%0A    keyed_vector_1 = predictions_vector_1%5C%0A        .map(lambda (u, i, r): ((u, i), r))%0A%0A    predictions = keyed_vector_0.join(keyed_vector_1)%5C%0A        .map(lambda ((u, i), (r0, r1)): (u, i, (1. - mixing_variable) * r0 + mixing_variable * r1))%5C%0A        .coalesce(num_partitions)%0A%0A    return predictions%0A

Add a simple hybrid recommender from Zhou et. al