View Source Scholar.Linear.LogisticRegression (Scholar v0.3.0)

Logistic regression in both binary and multinomial variants.

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

Summary

Functions

Fits a logistic regression model for sample inputs x and sample targets y.

Makes predictions with the given model on inputs x.

Calculates probabilities of predictions with the given model on inputs x.

Functions

Fits a logistic regression model for sample inputs x and sample targets y.

Depending on number of classes the function chooses either binary or multinomial logistic regression.

Options

  • :num_classes (pos_integer/0) - Required. number of classes contained in the input tensors.

  • :iterations (pos_integer/0) - number of iterations of gradient descent performed inside logistic regression. The default value is 1000.

  • :learning_loop_unroll (boolean/0) - If true, the learning loop is unrolled. The default value is false.

  • :optimizer - The optimizer name or {init, update} pair of functions (see Polaris.Optimizers for more details). The default value is :sgd.

  • :eps (float/0) - The convergence tolerance. If the abs(loss) < size(x) * :eps, the algorithm is considered to have converged. The default value is 1.0e-8.

Return Values

The function returns a struct with the following parameters:

  • :coefficients - Coefficient of the features in the decision function.

  • :bias - Bias added to the decision function.

  • :mode - Indicates whether the problem is binary classification (:num_classes set to 2) or multinomial (:num_classes is bigger than 2). For binary classification set to :binary, otherwise set to :multinomial.

Examples

iex> x = Nx.tensor([[1.0, 2.0], [3.0, 2.0], [4.0, 7.0]])
iex> y = Nx.tensor([1, 0, 1])
iex> Scholar.Linear.LogisticRegression.fit(x, y, num_classes: 2)
%Scholar.Linear.LogisticRegression{
  coefficients: Nx.tensor(
    [
      [2.5531527996063232, -0.5531544089317322],
      [-0.35652396082878113, 2.3565237522125244]
    ]
  ),
  bias: Nx.tensor(
    [-0.28847914934158325, 0.28847917914390564]
  )
}

Makes predictions with the given model on inputs x.

Examples

iex> x = Nx.tensor([[1.0, 2.0], [3.0, 2.0], [4.0, 7.0]])
iex> y = Nx.tensor([1, 0, 1])
iex> model = Scholar.Linear.LogisticRegression.fit(x, y, num_classes: 2)
iex> Scholar.Linear.LogisticRegression.predict(model, Nx.tensor([[-3.0, 5.0]]))
#Nx.Tensor<
  s64[1]
  [1]
>
Link to this function

predict_probability(model, x)

View Source

Calculates probabilities of predictions with the given model on inputs x.

Examples

iex> x = Nx.tensor([[1.0, 2.0], [3.0, 2.0], [4.0, 7.0]])
iex> y = Nx.tensor([1, 0, 1])
iex> model = Scholar.Linear.LogisticRegression.fit(x, y, num_classes: 2)
iex> Scholar.Linear.LogisticRegression.predict_probability(model, Nx.tensor([[-3.0, 5.0]]))
#Nx.Tensor<
  f32[1][2]
  [
    [6.470913388456623e-11, 1.0]
  ]
>