# In training processing, How can view precision-recall curve beautifully?

In training phase, I have the true lables and the corresponding scores. I can use sklearn.metric. precision_recall_curve(labels, scores) to calculate the precision and recall rate. But How can I plot them and view ?
I also try to use tensorboardX, there are two methods, add_pr_curve(), but it requires the input is the predicted probability, not the scores. Another method is add_pr_curve_raw(), it requires many parameters, such as positive_num, negative_num, precision, recall, it is hard or difficult for me to calculate these parameter values. So, What can I do for viewing the pr curve in the training phase? is there a good way to implement it? Thanks in advance.

You could use Visdom to plot your curve.
Since you already have the necessary numpy arrays representing the precision and recall, you could try to plot the line using viz.line.
I’m not sure, which arguments tensorboardX expects for the PR curve.

Let me know, if visdom would be an alternative or if you would like to stick to tensorboardX.

Thank you very much for your reply. My codes are like this:

import matplotlib.pyplot as plt
fig,ax=plt.subplots()
ax.step(recall,precision,color=‘r’,alpha=0.99,where=‘post’)
ax.fill_between(recall, precision, alpha=0.2, color=‘b’, step=‘post’)
plt.xlabel(‘Recall’)
plt.ylabel(‘Precision’)
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title(‘2-class Precision-Recall curve: AP={0:0.2f}’.format(
average_precision))
plt.close(fig)

I used the method of add_figure to save and view the pr curve.

I want to view the pr curve in each epoch. So, I want to save all them and view them.

If you would like to use `matplotlib` and just visualize the figures, one way would be to save then as `svg` data and send them to visdom.
Using this approach you wouldn’t need to recreate the figures in visdom.
Here is a small example:

``````import re
import io
from visdom import Visdom

labels = np.random.randint(0, 2, (10,))
scores = np.random.uniform(0, 1, (10,))
precision, recall, thresholds = precision_recall_curve(
labels, scores)

fig, ax = plt.subplots()
ax.step(recall,precision,color='r',alpha=0.99,where='post')
ax.fill_between(recall, precision, alpha=0.2, color='b', step='post')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])

viz = Visdom()
imgdata = io.StringIO()
fig.savefig(imgdata, format='svg')

svg_str = imgdata.getvalue()
# Scale the figure
svg_str = re.sub('width=".*pt"', 'width="100%"', svg_str)
svg_str = re.sub('height=".*pt"', 'height="100%"', svg_str)
viz.svg(svg_str)
``````
2 Likes