Scholar.Linear.LogisticRegression (Scholar v0.4.2)

Copy Markdown View Source

Multiclass logistic regression.

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

fit(x, y, opts \\ [])

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

Options

  • :num_classes (pos_integer/0) - Required. Number of output classes.

  • :max_iterations (pos_integer/0) - Maximum number of gradient descent iterations to perform. The default value is 1000.

  • :alpha - Constant that multiplies the L2 regularization term, controlling regularization strength. If 0, no regularization is applied. The default value is 1.0.

  • :tol - Convergence tolerance. If the infinity norm of the gradient is less than :tol, the algorithm is considered to have converged. The default value is 0.0001.

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.

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(
    [
      [0.0915902629494667, -0.09159023314714432],
      [-0.1507941037416458, 0.1507941335439682]
    ]
  ),
  bias: Nx.tensor([-0.06566660106182098, 0.06566664576530457])
}

predict(model, x)

Makes predictions with the given model on inputs x.

Output predictions have shape {n_samples} when train target is shaped either {n_samples} or {n_samples, 1}.

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([1])

predict_probability(model, x)

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([[0.10075931251049042, 0.8992406725883484]])