View Source Scholar.NaiveBayes.Multinomial (Scholar v0.3.1)

Naive Bayes classifier for multinomial models.

The multinomial Naive Bayes classifier is suitable for classification with discrete features (e.g., word counts for text classification)

Time complexity is $O(K * N * C)$ where $N$ is the number of samples and $K$ is the number of features, and $C$ is the number of classes.

Summary

Functions

Fits a naive Bayes model. The function assumes that 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.

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 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 is 1.0.

  • :force_alpha (boolean/0) - If false and alpha is less than 1e-10, it will set alpha to 1e-10. If true, alpha will remain unchanged. This may cause numerical errors if alpha is too close to 0. The default value is true.

  • :fit_priors (boolean/0) - Whether to learn class prior probabilities or not. If false, a uniform prior will be used. The default value is true.

  • :class_priors - Prior probabilities of the classes. If specified, the priors are not adjusted according to the data.

  • :sample_weights - List of num_samples elements. A list of 1.0 values is used 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 - Number of samples encountered for each (class, feature) during fitting. This value is weighted by the sample weight when provided.

  • :feature_log_probability - Empirical log probability of features given a class, P(x_i|y).

Examples

iex> x = Nx.iota({4, 3})
iex> y = Nx.tensor([1, 2, 0, 2])
iex> Scholar.NaiveBayes.Multinomial.fit(x, y, num_classes: 3)
%Scholar.NaiveBayes.Multinomial{
  feature_count: Nx.tensor(
    [
      [6.0, 7.0, 8.0],
      [0.0, 1.0, 2.0],
      [12.0, 14.0, 16.0]
    ]
  ),
  class_count: Nx.tensor(
    [1.0, 1.0, 2.0]
  ),
  class_log_priors: Nx.tensor(
    [-1.3862943649291992, -1.3862943649291992, -0.6931471824645996]
  ),
  feature_log_probability: Nx.tensor(
    [
      [-1.232143759727478, -1.0986123085021973, -0.9808292388916016],
      [-1.7917594909667969, -1.0986123085021973, -0.6931471824645996],
      [-1.241713285446167, -1.0986123085021973, -0.9734492301940918]
    ]
  )
}

iex> x = Nx.iota({4, 3})
iex> y = Nx.tensor([1, 2, 0, 2])
iex> Scholar.NaiveBayes.Multinomial.fit(x, y, num_classes: 3, sample_weights: [1, 6, 2, 3])
%Scholar.NaiveBayes.Multinomial{
  feature_count: Nx.tensor(
    [
      [12.0, 14.0, 16.0],
      [0.0, 1.0, 2.0],
      [45.0, 54.0, 63.0]
    ]
  ),
  class_count: Nx.tensor(
    [2.0, 1.0, 9.0]
  ),
  class_log_priors: Nx.tensor(
    [-1.7917594909667969, -2.4849066734313965, -0.28768205642700195]
  ),
  feature_log_probability: Nx.tensor(
    [
      [-1.241713285446167, -1.0986123085021973, -0.9734492301940918],
      [-1.7917594909667969, -1.0986123085021973, -0.6931471824645996],
      [-1.2773041725158691, -1.0986123085021973, -0.9470624923706055]
    ]
  )
}

Perform classification on an array of test vectors x using model.

Examples

iex> x = Nx.iota({4, 3})
iex> y = Nx.tensor([1, 2, 0, 2])
iex> model = Scholar.NaiveBayes.Multinomial.fit(x, y, num_classes: 3)
iex> Scholar.NaiveBayes.Multinomial.predict(model, Nx.tensor([[6, 2, 4], [8, 5, 9]]))
#Nx.Tensor<
  s64[2]
  [2, 2]
>
Link to this function

predict_joint_log_probability(model, x)

View Source

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.Multinomial.fit(x, y, num_classes: 3)
iex> Scholar.NaiveBayes.Multinomial.predict_joint_log_probability(model, Nx.tensor([[6, 2, 4], [8, 5, 9]]))
#Nx.Tensor<
  f32[2][3]
  [
    [-14.899698257446289, -17.106664657592773, -14.23444938659668],
    [-25.563968658447266, -27.45175552368164, -24.880958557128906]
  ]
>
Link to this function

predict_log_probability(model, x)

View Source

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.Multinomial.fit(x, y, num_classes: 3)
iex> Scholar.NaiveBayes.Multinomial.predict_log_probability(model, Nx.tensor([[6, 2, 4], [8, 5, 9]]))
#Nx.Tensor<
  f32[2][3]
  [
    [-1.1167821884155273, -3.3237485885620117, -0.45153331756591797],
    [-1.141427993774414, -3.029214859008789, -0.4584178924560547]
  ]
>
Link to this function

predict_probability(model, x)

View Source

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.Multinomial.fit(x, y, num_classes: 3)
iex> Scholar.NaiveBayes.Multinomial.predict_probability(model, Nx.tensor([[6, 2, 4], [8, 5, 9]]))
#Nx.Tensor<
  f32[2][3]
  [
    [0.32733139395713806, 0.036017563194036484, 0.6366512179374695],
    [0.3193626403808594, 0.048353586345911026, 0.6322832107543945]
  ]
>