View Source Scholar.Preprocessing.StandardScaler (Scholar v0.3.1)
Standardizes the tensor by removing the mean and scaling to unit variance.
Formula for input tensor $x$:
$$ z = \frac{x - \mu}{\sigma} $$
Where $\mu$ is the mean of the samples, and $\sigma$ is the standard deviation. Standardization can be helpful in cases where the data follows a Gaussian distribution (or Normal distribution) without outliers.
Centering and scaling happen independently on each feature by computing the relevant statistics on the samples in the training set. Mean and standard deviation are then stored to be used on new samples.
Summary
Functions
Compute the standard deviation and mean of samples to be used for later scaling.
Standardizes the tensor by removing the mean and scaling to unit variance.
Performs the standardization of the tensor using a fitted scaler.
Functions
Compute the standard deviation and mean of samples to be used for later scaling.
Options
:axes
- Axes to calculate the distance over. By default the distance is calculated between the whole tensors.
Return values
Returns a struct with the following parameters:
standard_deviation
: the calculated standard deviation of samples.mean
: the calculated mean of samples.
Examples
iex> Scholar.Preprocessing.StandardScaler.fit(Nx.tensor([[1, -1, 2], [2, 0, 0], [0, 1, -1]]))
%Scholar.Preprocessing.StandardScaler{
standard_deviation: Nx.tensor(
[
[1.0657403469085693]
]
),
mean: Nx.tensor(
[
[0.4444444477558136]
]
)
}
Standardizes the tensor by removing the mean and scaling to unit variance.
Examples
iex> t = Nx.tensor([[1, -1, 2], [2, 0, 0], [0, 1, -1]])
iex> Scholar.Preprocessing.StandardScaler.fit_transform(t)
#Nx.Tensor<
f32[3][3]
[
[0.5212860703468323, -1.3553436994552612, 1.4596009254455566],
[1.4596009254455566, -0.4170288145542145, -0.4170288145542145],
[-0.4170288145542145, 0.5212860703468323, -1.3553436994552612]
]
>
Performs the standardization of the tensor using a fitted scaler.
Examples
iex> t = Nx.tensor([[1, -1, 2], [2, 0, 0], [0, 1, -1]])
iex> scaler = Scholar.Preprocessing.StandardScaler.fit(t)
%Scholar.Preprocessing.StandardScaler{
standard_deviation: Nx.tensor(
[
[1.0657403469085693]
]
),
mean: Nx.tensor(
[
[0.4444444477558136]
]
)
}
iex> Scholar.Preprocessing.StandardScaler.transform(scaler, t)
#Nx.Tensor<
f32[3][3]
[
[0.5212860703468323, -1.3553436994552612, 1.4596009254455566],
[1.4596009254455566, -0.4170288145542145, -0.4170288145542145],
[-0.4170288145542145, 0.5212860703468323, -1.3553436994552612]
]
>