3
$\begingroup$

Let's say we train a classifier on the titanic dataset

cl = Classify[
  ResourceData["Sample Data: Titanic Survival"] -> "SurvivalStatus"]

We can then pass data through the classifier, which returns the class probabilities

cl[RandomSample[ResourceData["Sample Data: Titanic Survival"], 
  1], "Probabilities"]

Is there a way to take this classifier and use it outside of the notebook either by exporting the classifier, or by creating a Cloud API?

Although it can be exported as .wmlf, to the best of my knowledge it can’t be utilised outside of the mathematica framework

$\endgroup$

1 Answer 1

2
$\begingroup$

After a bit of playing around, I think I have a workable solution to share.

Firstly, let's create a classifier using the titanic datset.

cl = Classify[
  ResourceData["Sample Data: Titanic Survival"] -> "SurvivalStatus"]

Let's now define an API function. Within the function, we want to define the independent variables that are passed through the classifier to get the desired output.

For this dataset, there are three inputs

  • Class - a string - 1st, 2nd or 3rd
  • Age - a number
  • Sex - string - male or female

The output of the function is going to be in term of the class probabilities - "survived" & "died"

func = APIFunction[{"Class" -> "String", "Age" -> "Number", "Sex" -> "String"}, 
  cl[{#Class, #Age, #Sex}, "Probabilities"] &]

Let's now deploy this and set the appropriate permissions.

co = CloudDeploy[func, Permissions -> "Public"]

We can then choose to embed the code into the c# script.

EmbedCode[co, "C#"]

We can now test the API function within MMA using URLExecute[]

URLExecute["API Link", {"Class" -> "3rd", "Age" -> 30, "Sex" -> "male"}]

Which returns the desired output.

<|"died" -> 0.9095863222570499, "survived" -> 
0.09041367774295012|>
$\endgroup$
2
  • $\begingroup$ How many cloud credits does each call cost? $\endgroup$
    – M.R.
    Commented Jun 14, 2021 at 15:22
  • $\begingroup$ 2 credits per call for this case. I added a second output to the API function which returned the SHAP Values which in total cost 4. I gather it's 2 credits per outcome. $\endgroup$
    – isaac5122
    Commented Jun 15, 2021 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.