def read_item(userId: str, itemID: str):

    userID = userId
    itemID = itemID

    df1 = pd.DataFrame(data={"userID": [int(userID)], "itemID": [str(itemID)]})

    bpr = pickle.load(open("lightGCN_model.p", "rb"))
    #bpr = pickle.load( open( "trained_model.p", "rb" ))
    #bpr = pickle.load( open( "new_model.p", "rb" ))
    with Timer() as t:
        foo = predict_ranking(bpr,
                              df1,
                              usercol='userID',
                              itemcol='itemID',
                              remove_seen=True)
    print("Took {} seconds for prediction.".format(t))

    foo["userID"] = foo["userID"].apply(lambda x: round(x))
    foo["itemID"] = foo["itemID"].apply(lambda x: (x))

    result = foo[foo["userID"] == int(userID)].sort_values(
        by='prediction', ascending=False).head(10)
    result = result.drop(columns=["prediction", "userID"], axis=1)

    #data = {
    #   "data":result
    #}
    #return json.dumps(data)

    return result.to_json()
예제 #2
0
def recommend_k_bpr(model, test, train):
    with Timer() as t:
        topk_scores = predict_ranking(
            model,
            train,
            usercol=DEFAULT_USER_COL,
            itemcol=DEFAULT_ITEM_COL,
            predcol=DEFAULT_PREDICTION_COL,
            remove_seen=True,
        )
    return topk_scores, t
예제 #3
0
def test_predict_ranking(rating_true):
    train_set = cornac.data.Dataset.from_uir(rating_true.itertuples(index=False), seed=42)
    bpr = cornac.models.BPR(k=100, max_iter=10000, seed=42).fit(train_set)

    preds = predict_ranking(bpr, rating_true, remove_seen=False)

    n_users = len(rating_true["userID"].unique())
    n_items = len(rating_true["itemID"].unique())
    assert preds.shape[0] == n_users * n_items

    assert set(preds.columns) == {"userID", "itemID", "prediction"}
    assert preds["userID"].dtypes == rating_true["userID"].dtypes
    assert preds["itemID"].dtypes == rating_true["itemID"].dtypes
    # perfect ranking achieved
    assert 1e-10 > 1 - ndcg_at_k(rating_true, preds)
    assert 1e-10 > 1 - recall_at_k(rating_true, preds)
def read_item(userId: str, itemID: str):

    userID = userId
    itemID = itemID

    df1 = pd.DataFrame(data={"userID": [int(userID)], "itemID": [int(itemID)]})

    svd = pickle.load(open("SVD_pickle.p", "rb"))
    with Timer() as t:
        foo = predict_ranking(svd,
                              df1,
                              usercol='userID',
                              itemcol='itemID',
                              remove_seen=True)
    print("Took {} seconds for prediction.".format(t))

    foo["userID"] = foo["userID"].apply(lambda x: round(x))
    foo["itemID"] = foo["itemID"].apply(lambda x: round(x))
    result = foo[foo["userID"] == int(userID)].sort_values(
        by='prediction', ascending=False).head(10)
    result = result.drop(columns=["prediction", "userID"], axis=1)

    return result.to_json()