View Source Scholar.Decomposition.TruncatedSVD (Scholar v0.4.0)
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.6871105432510376, 0.7265529036521912],
[0.7265529036521912, -0.6871105432510376]
]
>
iex> tsvd.singular_values
#Nx.Tensor<
f32[2]
[7.528080940246582, 0.7601959705352783]
>
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.6871105432510376, 0.7265529036521912],
[1.413663387298584, 0.039442360401153564],
[4.240990161895752, 0.1183270812034607],
[6.017930030822754, -0.18578583002090454]
]
>
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.9478826522827148, 2.260593891143799],
[2.481153964996338, 1.906071662902832],
[3.023407220840454, 1.352442979812622],
[5.174456596374512, -0.46385863423347473],
[6.521108150482178, -1.6488237380981445]
]
>