Naive Bayes classifier for categorical features.
The categorical Naive Bayes classifier is suitable for classification with discrete features that are categorically distributed. The categories of each feature are drawn from a categorical distribution.
Summary
Functions
Fits a naive Bayes model. The function assumes that the targets y are integers
between 0 and num_classes - 1 (inclusive). Otherwise, those samples will not
contribute to class_count.
Perform classification on an array of test vectors x using model.
You need to add sorted classes from the training data as the second argument.
Return joint log probability estimates for the test vector x using model.
Return log-probability estimates for the test vector x using model.
Return probability estimates for the test vector x using model.
Functions
Fits a naive Bayes model. The function assumes that the targets y are integers
between 0 and num_classes - 1 (inclusive). Otherwise, those samples will not
contribute to class_count.
Options
:num_classes(pos_integer/0) - Required. Number of different classes used in training.:alpha- Additive (Laplace/Lidstone) smoothing parameter (set alpha to 0.0 and force_alpha to true, for no smoothing). The default value is1.0.:force_alpha(boolean/0) - Iffalseand alpha is less than 1e-10, it will set alpha to 1e-10. Iftrue, alpha will remain unchanged. This may cause numerical errors if alpha is too close to 0. The default value istrue.:fit_priors(boolean/0) - Whether to learn class prior probabilities or not. Iffalse, a uniform prior will be used. The default value istrue.:class_priors- Prior probabilities of the classes. If specified, the priors are not adjusted according to the data.:sample_weights- List ofnum_sampleselements. A list of 1.0 values is used if none is given.:min_categories- List of minimum number of categories per feature. Determines the number of categories automatically from the training data if none is given.
Return Values
The function returns a struct with the following parameters:
:class_count- Number of samples encountered for each class during fitting. This value is weighted by the sample weight when provided.:class_log_priors- Smoothed empirical log probability for each class.:feature_count- A (num_features, num_classes, num_categories) tensor tracking the weighted count of each (feature, class, category) combination. Calculated by summing the weighted occurrences of feature values for each class-label during fitting.:feature_log_probability- Empirical log probability of features given a class,P(x_i|y).
Examples
iex> x = Nx.tensor([[1, 2, 2], [1, 2, 1], [2, 2, 0]])
iex> y = Nx.tensor([0, 1, 1])
iex> Scholar.NaiveBayes.Categorical.fit(x, y, num_classes: 2)
%Scholar.NaiveBayes.Categorical{
feature_count: Nx.tensor(
[
[
[0.0, 1.0, 0.0],
[0.0, 1.0, 1.0]
],
[
[0.0, 0.0, 1.0],
[0.0, 0.0, 2.0]
],
[
[0.0, 0.0, 1.0],
[1.0, 1.0, 0.0]
]
]
),
class_count: Nx.tensor([1.0, 2.0]),
class_log_priors: Nx.tensor([-1.0986123085021973, -0.40546512603759766]),
feature_log_probability: Nx.tensor(
[
[
[-1.3862943649291992, -0.6931471824645996, -1.3862943649291992],
[-1.6094379425048828, -0.9162907600402832, -0.9162907600402832]
],
[
[-1.3862943649291992, -1.3862943649291992, -0.6931471824645996],
[-1.6094379425048828, -1.6094379425048828, -0.5108256340026855]
],
[
[-1.3862943649291992, -1.3862943649291992, -0.6931471824645996],
[-0.9162907600402832, -0.9162907600402832, -1.6094379425048828]
]
]
)
}
iex> x = Nx.tensor([[1, 2, 2], [1, 2, 1], [2, 2, 0]])
iex> y = Nx.tensor([0, 1, 1])
iex> Scholar.NaiveBayes.Categorical.fit(x, y, num_classes: 2, force_alpha: false, alpha: 0.0)
%Scholar.NaiveBayes.Categorical{
feature_count: Nx.tensor(
[
[
[0.0, 1.0, 0.0],
[0.0, 1.0, 1.0]
],
[
[0.0, 0.0, 1.0],
[0.0, 0.0, 2.0]
],
[
[0.0, 0.0, 1.0],
[1.0, 1.0, 0.0]
]
]
),
class_count: Nx.tensor(
[1.0, 2.0]
),
class_log_priors: Nx.tensor(
[-1.0986123085021973, -0.40546512603759766]
),
feature_log_probability: Nx.tensor(
[
[
[-23.025850296020508, 0.0, -23.025850296020508],
[-23.718997955322266, -0.6931471824645996, -0.6931471824645996]
],
[
[-23.025850296020508, -23.025850296020508, 0.0],
[-23.718997955322266, -23.718997955322266, 0.0]
],
[
[-23.025850296020508, -23.025850296020508, 0.0],
[-0.6931471824645996, -0.6931471824645996, -23.718997955322266]
]
]
)
}
Perform classification on an array of test vectors x using model.
You need to add sorted classes from the training data as the second argument.
Examples
iex> x = Nx.iota({4, 3})
iex> y = Nx.tensor([1, 2, 0, 2])
iex> model = Scholar.NaiveBayes.Categorical.fit(x, y, num_classes: 3)
iex> Scholar.NaiveBayes.Categorical.predict(model, Nx.tensor([[6, 2, 4], [8, 5, 9]]), Nx.tensor([0, 1, 2]))
#Nx.Tensor<
s32[2]
[0, 2]
>
Return joint log probability estimates for the test vector x using model.
Examples
iex> x = Nx.iota({4, 3})
iex> y = Nx.tensor([1, 2, 0, 2])
iex> model = Scholar.NaiveBayes.Categorical.fit(x, y, num_classes: 3)
iex> Scholar.NaiveBayes.Categorical.predict_joint_log_probability(model, Nx.tensor([[6, 2, 4], [8, 5, 9]]))
#Nx.Tensor<
f32[2][3]
[
[-8.140898704528809, -8.83404541015625, -8.382061004638672],
[-8.83404541015625, -8.83404541015625, -8.382061004638672]
]
>
Return log-probability estimates for the test vector x using model.
Examples
iex> x = Nx.iota({4, 3})
iex> y = Nx.tensor([1, 2, 0, 2])
iex> model = Scholar.NaiveBayes.Categorical.fit(x, y, num_classes: 3)
iex> Scholar.NaiveBayes.Categorical.predict_log_probability(model, Nx.tensor([[6, 2, 4], [8, 5, 9]]))
#Nx.Tensor<
f32[2][3]
[
[-0.8266787528991699, -1.5198254585266113, -1.0678410530090332],
[-1.272965431213379, -1.272965431213379, -0.8209810256958008]
]
>
Return probability estimates for the test vector x using model.
Examples
iex> x = Nx.iota({4, 3})
iex> y = Nx.tensor([1, 2, 0, 2])
iex> model = Scholar.NaiveBayes.Categorical.fit(x, y, num_classes: 3)
iex> Scholar.NaiveBayes.Categorical.predict_probability(model, Nx.tensor([[6, 2, 4], [8, 5, 9]]))
#Nx.Tensor<
f32[2][3]
[
[0.43749991059303284, 0.21875005960464478, 0.34374985098838806],
[0.28000006079673767, 0.28000006079673767, 0.4399997889995575]
]
>