Dimensionality reduction using truncated SVD (aka LSA).
This transformer performs linear dimensionality reduction by means of truncated singular value decomposition (SVD).
Summary
Functions
Fit model on training data X.
Options
:num_components(pos_integer/0) - Desired dimensionality of output data. The default value is2.:num_iter(pos_integer/0) - Number of iterations for randomized SVD solver. The default value is5.:num_oversamples(pos_integer/0) - Number of oversamples for randomized SVD solver. The default value is10.:key- Key for random tensor generation. If the key is not provided, it is set toNx.Random.key(System.system_time()).
Return Values
The function returns a struct with the following parameters:
:components- tensor of shape{num_components, num_features}The right singular vectors of the input data.:explained_variance- tensor of shape{num_components}The variance of the training samples transformed by a projection to each component.:explained_variance_ratio- tensor of shape{num_components}Percentage of variance explained by each of the selected components.:singular_values- ndarray of shape{num_components}The singular values corresponding to each of the selected components.
Examples
iex> key = Nx.Random.key(0)
iex> x = Nx.tensor([[0, 0], [1, 0], [1, 1], [3, 3], [4, 4.5]])
iex> tsvd = Scholar.Decomposition.TruncatedSVD.fit(x, num_components: 2, key: key)
iex> tsvd.components
#Nx.Tensor<
f32[2][2]
[
[0.68631727, 0.72730225],
[0.72730225, -0.68631727]
]
>
iex> tsvd.singular_values
#Nx.Tensor<
f32[2]
[7.5280886, 0.7601881]
>
Fit model to X and perform dimensionality reduction on X.
Options
:num_components(pos_integer/0) - Desired dimensionality of output data. The default value is2.:num_iter(pos_integer/0) - Number of iterations for randomized SVD solver. The default value is5.:num_oversamples(pos_integer/0) - Number of oversamples for randomized SVD solver. The default value is10.:key- Key for random tensor generation. If the key is not provided, it is set toNx.Random.key(System.system_time()).
Return Values
X_new tensor of shape {num_samples, num_components} - reduced version of X.
Examples
iex> key = Nx.Random.key(0)
iex> x = Nx.tensor([[0, 0], [1, 0], [1, 1], [3, 3], [4, 4.5]])
iex> Scholar.Decomposition.TruncatedSVD.fit_transform(x, num_components: 2, key: key)
#Nx.Tensor<
f32[5][2]
[
[0.0, 0.0],
[0.68631727, 0.72730225],
[1.4136195, 0.04098499],
[4.2408586, 0.122954965],
[6.0181293, -0.17921868]
]
>
iex> key = Nx.Random.key(0)
iex> x = Nx.tensor([[0, 0, 3], [1, 0, 3], [1, 1, 3], [3, 3, 3], [4, 4.5, 3]])
iex> Scholar.Decomposition.TruncatedSVD.fit_transform(x, num_components: 2, key: key)
#Nx.Tensor<
f32[5][2]
[
[1.9499693, 2.258082],
[2.4829183, 1.9048187],
[3.0246549, 1.3492496],
[5.1740265, -0.4684149],
[6.5195804, -1.6550317]
]
>