Learnx.PolynomialRegression (Learnx v0.2.0)
Polynomial Regression.
Generates new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2]. Then, with this feature matrix, a Linear Regression can be performed.
Link to this section Summary
Link to this section Types
regressor()
@type regressor() :: %Learnx.PolynomialRegression{ coef: tensor(), degree: number(), intercept: number(), n_features: number() }
Polynomial regressor.
coef : tensor of shape {n_polynomial_features}. Estimated coefficients for the linear regression problem. The dimensions of the tensor will depend on the number of polynomial features generated by the features matrix, which depends on the degree and number of features.
intercept : float. Independent term in the linear model. Set to nil if fit_intercept=false.
n_features : int. Number of features seen during fit.
degree : positive int, default=2. If a single int is given, it specifies the maximal degree of the polynomial features. If a tuple (min_degree, max_degree) is passed, then min_degree is the minimum and max_degree is the maximum polynomial degree of the generated features. Note that min_degree=0 and min_degree=1 are equivalent as outputting the degree zero term is determined by include_bias.
tensor()
@type tensor() :: Nx.Tensor.t()
Link to this section Functions
fit(x, y, degree \\ 2, opts \\ [])
Fits polynomial model.
parameters
Parameters
x : list or tensor of shape {n_samples, n_features} or {n_samples,}. Training data.
y : list or tensor of shape {n_samples}. Target values. Will be cast to X's type if necessary.
returns
Returns
A polynomial regressor containing the coefficients, intercept (if chosen), number of features and the degree.
examples
Examples
Using lists as inputs, degree 3:
iex> x = [-1, 1, 3, 5]
iex> y = [6, 8, 10, 12]
iex> reg = Learnx.PolynomialRegression.fit(x, y, 3)
iex> reg.n_features
1
iex> reg.degree
3
iex> reg.intercept
6.999995231628418
iex> reg.coef
#Nx.Tensor<
f32[3]
[0.9999974966049194, 1.0758638381958008e-5, -1.9297003746032715e-6]
>
Using tensors as inputs, degree 2, no bias:
iex> x = [[1, 1], [1, 2], [2, 2], [2, 3]]
iex> y = [6, 8, 10, 12]
iex> reg = Learnx.PolynomialRegression.fit(x, y, 3, include_bias: true)
iex> reg.n_features
1
iex> reg.degree
2
iex> reg.intercept
nil
iex> reg.coef
#Nx.Tensor<
f32[2]
[1.5637593269348145, 0.23489943146705627]
>
predict(regressor, x)
Predicts using the polynomial model.
parameters
Parameters
regressor : trained regressor to use for the predictions.
x : tensor of shape {n_samples, n_features}. Samples.
returns
Returns
Predicted value or values. Returns the prediction(s) in the type of the input, i. e. if x is a tensor, it returns a tensor; and if x is a number, it returns a number.
transform(x, n, bias \\ false)
@spec transform(tensor(), pos_integer(), boolean()) :: any()
Transforms and returns x.
Generates the feature matrix from the matrix, the result is based on the degree.
parameters
Parameters
x : tensor of shape {n_samples, n_features}. Samples.
degree : non-negative integer. Degree of the transformed matrix to return.
include_bias : boolean, default=true. If
true
, then include a bias column, the feature in which all polynomial powers are zero (i.e. a column of ones - acts as an intercept term in a linear model).
returns
Returns
Transformed version of x.