Ejemplo n.º 1
0
def test_dump():

    predictions = [Prediction(None, None, None, None, None)]
    algo = AlgoBase()
    trainset = Trainset(dict(), *[None] * 8)

    with tempfile.NamedTemporaryFile() as tmp_file:
        dump(tmp_file.name, predictions, trainset, algo)
Ejemplo n.º 2
0
def evaluate_model(model: AlgoBase, test_set: [(int, int, float)]) -> dict:
    global fit_time
    starts = time.time()
    predictions = model.test(test_set)
    #     print("It has been {0} seconds since the evaluation started".format(time.time() - starts))

    metrics_dict = {}
    metrics_dict['RMSE'] = accuracy.rmse(predictions, verbose=False)
    metrics_dict['MAE'] = accuracy.rmse(predictions, verbose=False)
    metrics_dict['test_time'] = time.time() - starts
    metrics_dict['fit_time'] = fit_time
    return metrics_dict
def get_user_recommendation(model: AlgoBase, user_id: int, k: int, data: pandas.DataFrame, movies : pandas.DataFrame
                           ) -> pandas.DataFrame:
    """Makes movie recommendations a user.
    
    Parameters
    ----------
        model : AlgoBase
            A trained surprise model
        user_id : int
            The user for whom the recommendation will be done.
        k : int
            The number of items to recommend.
        data :  pandas.DataFrame
            The data needed to do the recommendation ( ratings dataframe ).
        movies : pandas.DataFrame
            The dataframe containing the movies metadata (title, genre, etc)
        
    Returns
    -------
    pandas.Dataframe
        A dataframe with the k movies that will be recommended the user. The dataframe should have the following
        columns (movie_name : str, movie_genre : str, predicted_rating : float, true_rating : float)
        
    Notes
    -----
    - You should create other functions that are used in this one and not put all the code in the same function.
        For example to create the final dataframe, instead of implemented all the code
        in this function (get_user_recommendation), you can create a new one (create_recommendation_dataframe)
        that will be called in this function.
    - You can add other arguments to the function if you need to.
    """
    data_to_test = create_user_movieId_trueRatings_dataframe(data)
    predictions = model.test(data_to_test)
    top_n = get_top_n(predictions, n=k)
    top_n_df = create_top_n_dataframe(top_n, data_to_test)
    recommendation_df = create_recommendation_dataframe(top_n_df, movies, user_id).head(k)  
    return recommendation_df


#####################################################################################################################
def predict_fn(data: List[InferenceRequest],
               model: SurpriseAlgoBase) -> List[surprise.Prediction]:
    """Predict function called for inference"""
    return [
        model.predict(datum.uid, datum.iid, verbose=True) for datum in data
    ]
Ejemplo n.º 5
0
def evaluate_model(model: AlgoBase, test_set: [(int, int, float)]) -> dict:
    predictions = model.test(test_set)
    metrics_dict = {}
    metrics_dict['RMSE'] = accuracy.rmse(predictions, verbose=False)
    metrics_dict['MAE'] = accuracy.rmse(predictions, verbose=False)
    return metrics_dict
Ejemplo n.º 6
0
 def model_prediction(self, model_class: AlgoBase, test: list) -> list:
     pred = model_class.test(test)
     return pred
Ejemplo n.º 7
0
 def train(self, param_model: AlgoBase, trainset: Trainset) -> AlgoBase:
     param_model.fit(trainset)
     return param_model
Ejemplo n.º 8
0
def make_predictions(model: AlgoBase, test_set: [(int, int, float)]) -> dict:
    predictions = model.test(test_set)
    return predictions