# `Scholar.Linear.LinearRegression`
[🔗](https://github.com/elixir-nx/scholar/blob/main/lib/scholar/linear/linear_regression.ex#L1)

Ordinary least squares linear regression.

Time complexity of linear regression is $O((K^2) * (K+N))$ where $N$ is the number of samples
and $K$ is the number of features.

# `fit`
[🔗](https://github.com/elixir-nx/scholar/blob/main/lib/scholar/linear/linear_regression.ex#L69)

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

## Options

* `:sample_weights` - The weights for each observation. If not provided,
  all observations are assigned equal weight.

* `:fit_intercept?` (`t:boolean/0`) - If set to `true`, a model will fit the intercept. Otherwise,
  the intercept is set to `0.0`. The intercept is an independent term
  in a linear model. Specifically, it is the expected mean value
  of targets for a zero-vector on input. The default value is `true`.

## Return Values

  The function returns a struct with the following parameters:

  * `:coefficients` - Estimated coefficients for the linear regression problem.

  * `:intercept` - Independent term in the linear model.

## Examples

    iex> x = Nx.tensor([[1.0, 2.0], [3.0, 2.0], [4.0, 7.0]])
    iex> y = Nx.tensor([4.0, 3.0, -1.0])
    iex> model = Scholar.Linear.LinearRegression.fit(x, y)
    iex> model.coefficients
    #Nx.Tensor<
      f32[2]
      [-0.49724647402763367, -0.7010394930839539]
    >
    iex> model.intercept
    #Nx.Tensor<
      f32
      5.8964691162109375
    >

# `predict`
[🔗](https://github.com/elixir-nx/scholar/blob/main/lib/scholar/linear/linear_regression.ex#L132)

Makes predictions with the given `model` on input `x`.

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

## Examples

    iex> x = Nx.tensor([[1.0, 2.0], [3.0, 2.0], [4.0, 7.0]])
    iex> y = Nx.tensor([4.0, 3.0, -1.0])
    iex> model = Scholar.Linear.LinearRegression.fit(x, y)
    iex> Scholar.Linear.LinearRegression.predict(model, Nx.tensor([[2.0, 1.0]]))
    Nx.tensor(
      [4.200936794281006]
    )

---

*Consult [api-reference.md](api-reference.md) for complete listing*
