Fill Modes (for Missing Dimensions)

An example plot of the different fill modes of sksfa.SFA.

The input data does only have effective dimension 2 (the other dimensions are linearly dependent), but 3 slow features are set to be extracted. The different fill_mode options will fill the missing features in different ways. Setting ‘fill_mode’ to None, would make the transformer throw an exception.

x(t), A⋅x(t) + b, Extracted features, fill_mode='noise', Extracted features, fill_mode='zero', Extracted features, fill_mode='fastest'

Out:

/home/docs/checkouts/readthedocs.org/user_builds/sklearn-sfa/envs/latest/lib/python3.7/site-packages/sksfa/_sfa.py:337: RuntimeWarning: During whitening, 2 trivial components with roughly zero explained variance have been found. This probably means that the effective dimension of your input is too low to find the desired {self.n_components_} slow features. Ways to deal with this are:
        Injecting noise via the 'noise_std' parameter.
        Providing more data.
        Lowering the 'n_components' parameter.
        Lowering the threshold 'robustness_cutoff'.
Since 'fill_mode' is set to noise, missing output features will be filled by independent white-noise.
  warnings.warn(warning_string, RuntimeWarning)
/home/docs/checkouts/readthedocs.org/user_builds/sklearn-sfa/envs/latest/lib/python3.7/site-packages/sksfa/_sfa.py:337: RuntimeWarning: During whitening, 2 trivial components with roughly zero explained variance have been found. This probably means that the effective dimension of your input is too low to find the desired {self.n_components_} slow features. Ways to deal with this are:
        Injecting noise via the 'noise_std' parameter.
        Providing more data.
        Lowering the 'n_components' parameter.
        Lowering the threshold 'robustness_cutoff'.
Since 'fill_mode' is set to zero, missing output features will be filled by a 0 signal.
  warnings.warn(warning_string, RuntimeWarning)
/home/docs/checkouts/readthedocs.org/user_builds/sklearn-sfa/envs/latest/lib/python3.7/site-packages/sksfa/_sfa.py:337: RuntimeWarning: During whitening, 2 trivial components with roughly zero explained variance have been found. This probably means that the effective dimension of your input is too low to find the desired {self.n_components_} slow features. Ways to deal with this are:
        Injecting noise via the 'noise_std' parameter.
        Providing more data.
        Lowering the 'n_components' parameter.
        Lowering the threshold 'robustness_cutoff'.
Since 'fill_mode' is set to fastest, missing output features will be filled by duplicates of the fastest signal.
  warnings.warn(warning_string, RuntimeWarning)

import numpy as np
from sksfa import SFA
import matplotlib.pyplot as plt


n_samples = 500
n_slow_features = 3
dim = 4
fill_modes = ["noise", "zero", "fastest"]

# Generate different randomly shifted time-scales
t = np.linspace(0, 2*np.pi, n_samples).reshape(n_samples, 1)

# Generate latent cosine signals
x = np.hstack([np.cos(t), 0.5 * np.cos(t), np.cos(2 * t), 1.5 * np.cos(t)])

# Compute random affine mapping of cosines (observed)
A = np.random.normal(0, 1, (dim, dim))
b = np.random.normal(0, 2, (1, dim))
data = np.dot(x, A) + b

# Extract slow features from observed data

# Plot cosines, mapped data, and extracted features
fig, ax = plt.subplots(2 + len(fill_modes), 1, sharex=True)
fig.set_size_inches(8, 18)
fig.subplots_adjust(hspace=0.5)
for d in range(n_slow_features):
    ax[0].plot(x[:, d])
ax[1].plot(data)
for idx, fill_mode in enumerate(fill_modes):
    sfa = SFA(n_slow_features, fill_mode=fill_mode)
    slow_features = sfa.fit_transform(data)
    ax[2 + idx].plot(slow_features[:, :-1])
    ax[2 + idx].plot(slow_features[:, -1], linestyle=":", color="purple")
    ax[2 + idx].set_title(f"Extracted features, fill_mode='{fill_mode}'")
    ax[2 + idx].set_xlabel("Time t")
ax[0].set_title("x(t)")
ax[1].set_title("A⋅x(t) + b")
for idx in range(2 + len(fill_modes)):
    ax[idx].set_ylabel("Features")
plt.show()

Total running time of the script: ( 0 minutes 0.446 seconds)

Gallery generated by Sphinx-Gallery