Scale features in a datasets.
Usage
feature_rescale(x, x_min = NULL, x_max = NULL)
feature_derescale(x_rescaled, x_min, x_max)
feature_norm(x, x_norm = NULL)
feature_denorm(x_norm_std, x_norm = NULL)
feature_standardize(x, x_mean = NULL, x_sd = NULL)
feature_destandardize(x_std, x_mean = NULL, x_sd = NULL)
Arguments
- x
Numeric values
- x_min
Minimum non-normalized numeric value
- x_max
Maximum non-normalized numeric value
- x_rescaled
Rescaled values of
x
.- x_norm
Euclidean norm of x
- x_norm_std
Euclidean vector of normalized
x
values.- x_mean
Mean of
x
values- x_sd
Standard Deviation of
x
values- x_std
Z-transformed
x
values
Details
The following functions provide a means to either scale features or to descale the features and return them to normal. These functions are ideal for working with optimizers.
Feature Scale | Feature Descale |
feature_rescale | feature_derescale |
feature_norm | feature_denorm |
feature_standardize | feature_destandardize |
Feature Rescaling
Convert the original data \(x\) to \(x_{scaled}\):
$$x[scaled] = (x-x[min])/(x[max]-x[min])$$
To move from the rescaled value \(x_{scaled}\) to the original value \(x\) use:
$$x = x[scaled] * (x[max] - x[min]) + x[min]$$
Feature Standardization
Convert the original data \(x\) to \(x_{std}\):
$$x[std] = (x-avg[x])/(sigma[x])$$
To move from the standardized value \(x_{std}\) to the original value \(x\) use:
$$x = x[std] * sigma[x] + avg[x]$$
Feature Normalization
Convert the original data \(x\) to \(x_{norm}\):
$$x[norm] = (x)/||x||$$
To move from the normalized value \(x_{norm}\) to the original value \(x\) use:
$$x = x[norm] * ||x||$$
Examples
# Rescaling Features
temperatures = c(94.2, 88.1, 32, 0)
temp_min = min(temperatures)
temp_max = max(temperatures)
temperatures_norm = feature_rescale(temp_min, temp_max)
temperatures_denorm = feature_derescale(temperatures_norm, temp_min, temp_max)
all.equal(temperatures, temperatures_denorm)
#> [1] "Numeric: lengths (4, 1) differ"
# Norming Features
x = 1:10
x_norm = sqrt(sum(x^2))
x_norm_std = feature_norm(x, x_norm)
x_recover = feature_denorm(x_norm_std, x_norm)
all.equal(x, x_recover)
#> [1] TRUE
# Standardizing Features
x = 1:10
x_mean = mean(x)
x_sd = sd(x)
x_std = feature_standardize(x, x_mean, x_sd)
x_recovery = feature_destandardize(x, x_mean, x_sd)
all.equal(x, x_recovery)
#> [1] "Mean relative difference: 3.02765"