I am trying to visualize the graph about the precision-recall curve
This is the data frame provided:
import pandas as pd
import pyspark
import sklearn
from recommenders.utils.spark_utils import start_or_get_spark
from recommenders.evaluation.spark_evaluation import SparkRankingEvaluation, SparkRatingEvaluation
COL_USER = "UserId"
COL_ITEM = "MovieId"
COL_RATING = "Rating"
COL_PREDICTION = "Rating"
HEADER = {"col_user": COL_USER, "col_item": COL_ITEM, "col_rating": COL_RATING, "col_prediction": COL_PREDICTION,}
df_true = pd.DataFrame(
{
COL_USER: [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
COL_ITEM: [1, 2, 3, 1, 4, 5, 6, 7, 2, 5, 6, 8, 9, 10, 11, 12, 13, 14],
COL_RATING: [5, 4, 3, 5, 5, 3, 3, 1, 5, 5, 5, 4, 4, 3, 3, 3, 2, 1],
}
)
df_pred = pd.DataFrame(
{
COL_USER: [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
COL_ITEM: [3, 10, 12, 10, 3, 5, 11, 13, 4, 10, 7, 13, 1, 3, 5, 2, 11, 14],
COL_PREDICTION: [14, 13, 12, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5]
}
)
spark = start_or_get_spark("EvaluationTesting", "local")
dfs_true = spark.createDataFrame(df_true)
dfs_pred = spark.createDataFrame(df_pred)
precisionatk = spark_rank_eval.precision_at_k()
recallatk = spark_rank_eval.recall_at_k()
I have tried several code but it not work well
PrecisionRecallDisplay.from_predictions(dfs_true, dfs_pred, ax=ax)
or
precision, recall, thresholds = precision_recall_curve(dfs_true, dfs_pred)
plt.fill_between(recall, precision)
This is the output I expect:
enter image description here
Source full code: https://github.com/recommenders-team/recommenders/blob/main/examples/03_evaluate/evaluation.ipynb
New contributor
Rachel Tran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.