Scholar.CrossDecomposition.PLSSVD (Scholar v0.5.0)

Copy Markdown View Source

Partial Least Square SVD.

This transformer simply performs a SVD on the cross-covariance matrix. It is able to project both the training data x and the targets y. The training data x is projected on the left singular vectors, while the targets are projected on the right singular vectors.

Summary

Functions

Fit model to data. Takes as arguments:

Learn and apply the dimensionality reduction.

Apply the dimensionality reduction. Takes as arguments:

Functions

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

Fit model to data. Takes as arguments:

  • x - training samples, {num_samples, num_features} shaped tensor

  • y - targets, {num_samples, num_targets} shaped y tensor

Options

  • :num_components (pos_integer/0) - The number of components to keep. Should be in [1, min(n_samples, n_features, n_targets)]. The default value is 2.

  • :scale (boolean/0) - Whether to scale x and y. The default value is true.

Return Values

The function returns fitted estimator represented by struct with the following parameters:

  • :x_mean - tensor of shape {num_features} which represents x tensor mean values calculated along axis 0.

  • :y_mean - tensor of shape {num_targets} which represents x tensor mean values calculated along axis 0.

  • :x_std - tensor of shape {num_features} which represents x tensor standard deviation values calculated along axis 0.

  • :y_std - tensor of shape {num_targets} which represents y tensor standard deviation values calculated along axis 0.

  • :x_weights - tensor of shape {num_features, num_components} the left singular vectors of the SVD of the cross-covariance matrix.

  • :y_weights - tensor of shape {num_components, num_targets} the transposed right singular vectors of the SVD of the cross-covariance matrix.

Examples

iex> x = Nx.tensor([[0.0, 0.0, 1.0],
...>                [1.0, 0.0, 0.0],
...>                [2.0, 2.0, 2.0],
...>                [2.0, 5.0, 4.0]])
iex> y = Nx.tensor([[0.1, -0.2],
...>                [0.9, 1.1],
...>                [6.2, 5.9],
...>                [11.9, 12.3]])
iex> model = Scholar.CrossDecomposition.PLSSVD.fit(x, y)
iex> model.x_mean
#Nx.Tensor<
  f32[3]
  [1.25, 1.75, 1.75]
>
iex> model.y_std
#Nx.Tensor<
  f32[2]
  [5.4670987, 5.6611986]
>
iex> model.x_weights
#Nx.Tensor<
  f32[3][2]
  [
    [0.5218878, -0.11256518],
    [0.617029, 0.7342583],
    [0.58898926, -0.66947275]
  ]
>

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

Learn and apply the dimensionality reduction.

The arguments are:

  • x - training samples, {num_samples, num_features} shaped tensor

  • y - targets, {num_samples, num_targets} shaped y tensor

Options

  • :num_components (pos_integer/0) - The number of components to keep. Should be in [1, min(n_samples, n_features, n_targets)]. The default value is 2.

  • :scale (boolean/0) - Whether to scale x and y. The default value is true.

Return Values

Returns tuple with transformed data {x_transformed, y_transformed} where:

  • x_transformed is {num_samples, num_features} shaped tensor.

  • y_transformed is {num_samples, num_features} shaped tensor.

Examples

iex> x = Nx.tensor([[0.0, 0.0, 1.0],
...>                [1.0, 0.0, 0.0],
...>                [2.0, 2.0, 2.0],
...>                [2.0, 5.0, 4.0]])
iex> y = Nx.tensor([[0.1, -0.2],
...>                [0.9, 1.1],
...>                [6.2, 5.9],
...>                [11.9, 12.3]])
iex> {x, y} = Scholar.CrossDecomposition.PLSSVD.fit_transform(x, y)
iex> x
#Nx.Tensor<
  f32[4][2]
  [
    [-1.3970047, -0.1028357],
    [-1.1967875, 0.1715969],
    [0.56032246, -0.10849276],
    [2.0334697, 0.039731577]
  ]
>
iex> y
#Nx.Tensor<
  f32[4][2]
  [
    [-1.226018, -0.019301286],
    [-0.9602954, 0.040158324],
    [0.32491535, -0.043111708],
    [1.8613981, 0.022254586]
  ]
>

transform(model, x, y, opts \\ [])

Apply the dimensionality reduction. Takes as arguments:

  • fitted estimator struct which is return value of fit/3 function from this module

  • x - training samples, {num_samples, num_features} shaped tensor

  • y - targets, {num_samples, num_targets} shaped y tensor

Options

  • :num_components (pos_integer/0) - The number of components to keep. Should be in [1, min(n_samples, n_features, n_targets)]. The default value is 2.

  • :scale (boolean/0) - Whether to scale x and y. The default value is true.

Return Values

Returns tuple with transformed data {x_transformed, y_transformed} where:

  • x_transformed is {num_samples, num_features} shaped tensor.

  • y_transformed is {num_samples, num_features} shaped tensor.

Examples

iex> x = Nx.tensor([[0.0, 0.0, 1.0],
...>                [1.0, 0.0, 0.0],
...>                [2.0, 2.0, 2.0],
...>                [2.0, 5.0, 4.0]])
iex> y = Nx.tensor([[0.1, -0.2],
...>                [0.9, 1.1],
...>                [6.2, 5.9],
...>                [11.9, 12.3]])
iex> model = Scholar.CrossDecomposition.PLSSVD.fit(x, y)
iex> {x, y} = Scholar.CrossDecomposition.PLSSVD.transform(model, x, y)
iex> x
#Nx.Tensor<
  f32[4][2]
  [
    [-1.3970047, -0.1028357],
    [-1.1967875, 0.1715969],
    [0.56032246, -0.10849276],
    [2.0334697, 0.039731577]
  ]
>
iex> y
#Nx.Tensor<
  f32[4][2]
  [
    [-1.226018, -0.019301286],
    [-0.9602954, 0.040158324],
    [0.32491535, -0.043111708],
    [1.8613981, 0.022254586]
  ]
>