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

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.

Summary

Functions

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

Makes predictions with the given model on input x.

Functions

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? (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
>

Makes predictions with the given model on input x.

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]
)