Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving an Matlabplot as an MLFlow artifact

I am using DataBricks and Spark 7.4ML,

The following code successfully logs the params and metrics, and I can see the ROCcurve.png in the MLFLOW gui (just the item in the tree below the model). But the actually plot is blank. Why?

with mlflow.start_run(run_name="logistic-regression") as run:
  pipeModel = pipe.fit(trainDF)
  mlflow.spark.log_model(pipeModel, "model")
  predTest = pipeModel.transform(testDF)
  predTrain = pipeModel.transform(trainDF)
  evaluator=BinaryClassificationEvaluator(labelCol="arrivedLate")
  trainROC = evaluator.evaluate(predTrain)
  testROC = evaluator.evaluate(predTest)
  print(f"Train ROC: {trainROC}")
  print(f"Test ROC: {testROC}")
  mlflow.log_param("Dataset Name", "Flights " + datasetName)
  mlflow.log_metric(key="Train ROC", value=trainROC)
  mlflow.log_metric(key="Test ROC", value=testROC)

  lrModel = pipeModel.stages[3]
  trainingSummary = lrModel.summary
  roc = trainingSummary.roc.toPandas()
  plt.plot(roc['FPR'],roc['TPR'])
  plt.ylabel('False Positive Rate')
  plt.xlabel('True Positive Rate')
  plt.title('ROC Curve')
  plt.show()
  plt.savefig("ROCcurve.png")
  mlflow.log_artifact("ROCcurve.png")
  plt.close()
  
  display(predTest.select(stringCols + ["arrivedLate", "prediction"]))

What the notebook shows:

enter image description here

What the MLFlow shows:

enter image description here

like image 856
Dr.YSG Avatar asked Dec 04 '20 15:12

Dr.YSG


Video Answer


2 Answers

Put plt.show() after plt.savefig() - plt.show() will remove your plot because it is shown already.

like image 175
mck Avatar answered Oct 13 '22 05:10

mck


import mlflow 
import matplotlib.pyplot as plt

fig, axs = plt.subplots(2)
x0, y0 = [1,2,3], [1,2,3]
x1, y1 = [1,2,3], [1,2,3]
axs[0].plot(x0, y0)
axs[1].plot(x1, y1)
mlflow.log_figure(fig, 'my_plot.png')
like image 6
Yapi Avatar answered Oct 13 '22 07:10

Yapi