Introduction to Multiclass

Multiclass projects in DataRobot are projects that allow for prediction of more than two classes (unlike binary prediction, which is for precisely two classes). Currently, DataRobot supports predicting up to 10 different classes.

Connect to DataRobot

To explore multiclass projects, let's first connect to DataRobot. First, you must load the DataRobot R package library.

If you have set up a credentials file, library(datarobot) will initialize a connection to DataRobot automatically. Otherwise, you can specify your endpoint and apiToken as in this example to connect to DataRobot directly. For more information on connecting to DataRobot, see the "Introduction to DataRobot" vignette.

library(datarobot)
endpoint <- "https://<YOUR DATAROBOT URL GOES HERE>/api/v2"
apiToken <- "<YOUR API TOKEN GOES HERE>"
ConnectToDataRobot(endpoint = endpoint, token = apiToken)

Creating a Multiclass Project

Let's predict for the iris dataset:

library(knitr)
data(iris) # Load `iris` from R data memory.
kable(iris)

If your target is categorical and has a cardinality of up to 10, we will automatically select a Multiclass targetType and that argument is not needed when calling StartProject. However, if the target is numerical and you would like to force it to be seen as a Multiclass project in DataRobot, you can specify the targetType as seen below:

project <- StartProject(iris,
                        projectName = "multiclassExample",
                        target = "Species",
                        targetType = TargetType$Multiclass,
                        maxWait = 600)

Now we can build a model:

blueprint <- ListBlueprints(project)[[1]]
RequestNewModel(project, blueprint)

And then we can get predictions:

model <- ListModels(project)[[1]]
predictions <- Predict(model, iris)
print(table(predictions))
message("request issued, waiting for predictions")
message("Multiclass with labels setosa, versicolor, virginica")
print(table(readRDS("multiclassPredictions.rds")))

You can also get a dataframe with the probabilities of each class using type = "probability":

predictions <- Predict(model, iris, type = "probability")
kable(head(predictions))
message("request issued, waiting for predictions")
message("Multiclass with labels setosa, versicolor, virginica")
library(knitr)
kable(head(readRDS("multiclassPredictionProbs.rds")))

Confusion Charts

The confusion chart is a chart that helps understand how the multiclass model performs:

confusionChart <- GetConfusionChart(model, source = DataPartition$VALIDATION)
kable(capture.output(confusionChart))
kable(capture.output(readRDS("confusionChart.rds")))

Here, we can see the source comes from the "validation" partition (options are in the DataPartition object), and class metrics show:

The confusion chart also shows a full confusion matrix with one row and one column for each class, showing how each class was predicted or mispredicted. The columns represent the predicted classes and the rows represent the actual classes.



Try the datarobot package in your browser

Any scripts or data that you put into this service are public.

datarobot documentation built on Nov. 3, 2023, 1:07 a.m.