FuzzyClassifier is the (abstract) R6 class shared by every classifier of
the package. It implements the common workflow:
$new(...)creates an untrained model holding its hyperparameters;$fit(train, cl)estimates the model and reports the progress;$predict(newdata, type)classifies new observations;$set_params(...)changes hyperparameters (the model must be re-trained).
Do not instantiate it directly: use one of the concrete classes, e.g.
FuzzyGaussianNaiveBayesModel, FuzzyGammaNaiveBayesModel or
GauNBFuzzyParamModel. Each of them also has a functional interface (e.g.
FuzzyGaussianNaiveBayes()) that trains and returns the R6 object, and every
classifier is available to tidymodels through fuzzy_naive_bayes().
Since $fit() and $set_params() return the object invisibly, calls can be
chained: FuzzyGammaNaiveBayesModel$new()$fit(x, y)$predict(x_new).
Public fields
paramsNamed list with the hyperparameters of the classifier.
estimatesList with the quantities estimated by
$fit()(NULLwhile the model has not been trained).predictorsNames of the predictors seen during training.
classesClasses (levels of the response) seen during training.
n_obsNumber of training observations.
fit_timeTraining time, in seconds.
Active bindings
labelName of the classifier (read-only); it depends on whether the membership functions are used (
fuzzy).
Methods
FuzzyClassifier$fit()
Estimate the classifier from training data.
Usage
FuzzyClassifier$fit(train, cl, verbose = fuzzy_verbose())Arguments
trainMatrix or data frame of training set cases.
clFactor (or vector) of true classifications of the training set.
verboseLogical; if
TRUE(default, seefuzzy_verbose()) a message reports the training progress.
FuzzyClassifier$predict()
Classify new observations.
Usage
FuzzyClassifier$predict(newdata, type = c("class", "prob", "matrix", "raw"))FuzzyClassifier$set_params()
Update hyperparameters. The model is reset and must be
trained again with $fit().
Examples
model <- FuzzyGaussianNaiveBayesModel$new(fuzzy = TRUE)
model$fit(train = iris[, -5], cl = iris$Species)
#> ℹ Fuzzy Gaussian Naive Bayes (Zadeh-based): 150 observations, 4 predictors and 3 classes
#> ℹ Training the model: estimating its parameters
#> ✔ Model trained successfully [7ms]
#>
model
#> <FuzzyGaussianNaiveBayes> Fuzzy Gaussian Naive Bayes (Zadeh-based)
#> ✔ Trained on 150 observations in 0.003s
#> Hyperparameters: fuzzy = TRUE
#> Predictors (4): Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
#> Classes (3): setosa, versicolor, virginica
head(model$predict(iris[, -5]))
#> [1] setosa setosa setosa setosa setosa setosa
#> Levels: setosa versicolor virginica
head(model$predict(iris[, -5], type = "prob"))
#> setosa versicolor virginica
#> [1,] 1 3.089056e-21 2.007145e-29
#> [2,] 1 1.868147e-19 1.505721e-27
#> [3,] 1 2.429303e-20 2.675230e-28
#> [4,] 1 2.714064e-19 2.829119e-27
#> [5,] 1 1.054105e-21 1.242936e-29
#> [6,] 1 3.466495e-15 2.992237e-22
# S3 generics also work
head(predict(model, iris[, -5]))
#> [1] setosa setosa setosa setosa setosa setosa
#> Levels: setosa versicolor virginica
# Change a hyperparameter and re-train, silently
model$set_params(fuzzy = FALSE)$fit(iris[, -5], iris$Species, verbose = FALSE)
model$label
#> [1] "Gaussian Naive Bayes"