View Source Scholar.Linear.PolynomialRegression (Scholar v0.3.1)

Least squares polynomial regression.

Time complexity of polynomial 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 polynomial regression model for sample inputs x and sample targets y.

Makes predictions with the given model on input x.

Computes the feature matrix for polynomial regression.

Functions

Fits a polynomial 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.

  • :degree (pos_integer/0) - The degree of the feature matrix to return. Must be an integer equal or greater than 1. 1 returns the input matrix. The default value is 2.

  • :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 polynomial regression problem.

  • :intercept - Independent term in the polynomial 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.PolynomialRegression.fit(x, y, degree: 1)
iex> model.coefficients
#Nx.Tensor<
  f32[2]
  [-0.49724647402763367, -0.7010394930839539]
>
iex> model.intercept
#Nx.Tensor<
  f32
  5.8964691162109375
>
iex> model.degree
1

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.PolynomialRegression.fit(x, y, degree: 2)
iex> model.coefficients
#Nx.Tensor<
  f32[5]
  [-0.021396614611148834, -0.004854436963796616, -0.08849868923425674, -0.06221142038702965, -0.04369127005338669]
>
iex> model.intercept
#Nx.Tensor<
  f32
  4.418517112731934
>
iex> model.degree
2

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.PolynomialRegression.fit(x, y, degree: 2)
iex> Scholar.Linear.PolynomialRegression.predict(model, Nx.tensor([[2.0, 1.0]]))
#Nx.Tensor<
  f32[1]
  [3.8487606048583984]
>
Link to this function

transform(x, opts \\ [])

View Source

Computes the feature matrix for polynomial regression.

  • :degree (pos_integer/0) - The degree of the feature matrix to return. Must be an integer equal or greater than 1. 1 returns the input matrix. The default value is 2.

  • :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.

Examples

iex> x = Nx.tensor([[2]])
iex> Scholar.Linear.PolynomialRegression.transform(x, degree: 0)
** (NimbleOptions.ValidationError) invalid value for :degree option: expected positive integer, got: 0

iex> x = Nx.tensor([[2]])
iex> Scholar.Linear.PolynomialRegression.transform(x, degree: 5, fit_intercept?: false)
#Nx.Tensor<
  s64[1][5]
  [
    [2, 4, 8, 16, 32]
  ]
>

iex> x = Nx.tensor([[2, 3]])
iex> Scholar.Linear.PolynomialRegression.transform(x)
#Nx.Tensor<
  s64[1][6]
  [
    [1, 2, 3, 4, 6, 9]
  ]
>

iex> x = Nx.iota({3, 2})
iex> Scholar.Linear.PolynomialRegression.transform(x, fit_intercept?: false)
#Nx.Tensor<
  s64[3][5]
  [
    [0, 1, 0, 0, 1],
    [2, 3, 4, 6, 9],
    [4, 5, 16, 20, 25]
  ]
>