Ejemplo n.º 1
0
def _compute_expected_results(X,
                              y=None,
                              n_coefs=None,
                              n_bins=4,
                              strategy='quantile',
                              drop_sum=False,
                              anova=False,
                              norm_mean=False,
                              norm_std=False,
                              alphabet=None):
    """Compute the expected results."""
    X = np.asarray(X)
    if norm_mean:
        X -= X.mean(axis=1)[:, None]
    if norm_std:
        X /= X.std(axis=1)[:, None]
    X_fft = np.fft.rfft(X)
    X_fft = np.vstack([np.real(X_fft), np.imag(X_fft)])
    X_fft = X_fft.reshape(n_samples, -1, order='F')
    if drop_sum:
        X_fft = X_fft[:, 2:-1]
    else:
        X_fft = np.hstack([X_fft[:, :1], X_fft[:, 2:-1]])
    if n_coefs is not None:
        if anova:
            _, p = f_classif(X_fft, y)
            support = np.argsort(p)[:n_coefs]
            X_fft = X_fft[:, support]
        else:
            X_fft = X_fft[:, :n_coefs]

    mcb = MultipleCoefficientBinning(n_bins=n_bins,
                                     strategy=strategy,
                                     alphabet=alphabet)
    arr_desired = mcb.fit_transform(X_fft)
    return arr_desired
Ejemplo n.º 2
0
import numpy as np
import matplotlib.pyplot as plt
from pyts.approximation import MultipleCoefficientBinning

# Parameters
n_samples, n_timestamps = 100, 12

# Toy dataset
rng = np.random.RandomState(41)
X = rng.randn(n_samples, n_timestamps)

# MCB transformation
n_bins = 3
mcb = MultipleCoefficientBinning(n_bins=n_bins, strategy='quantile')
X_mcb = mcb.fit_transform(X)

# Show the results for two time series
plt.figure(figsize=(6, 4))

plt.plot(X[0], 'o--', ms=4, label='First time series')
for x, y, s in zip(range(n_timestamps), X[0], X_mcb[0]):
    plt.text(x, y, s, ha='center', va='bottom', fontsize=14, color='C0')

plt.plot(X[5], 'o--', ms=4, label='Second time series')
for x, y, s in zip(range(n_timestamps), X[5], X_mcb[5]):
    plt.text(x, y, s, ha='center', va='bottom', fontsize=14, color='C1')

plt.hlines(mcb.bin_edges_.T, np.arange(n_timestamps) - 0.5,
           np.arange(n_timestamps) + 0.5, color='g',
           linestyles='--', linewidth=0.7)
Ejemplo n.º 3
0
def test_constant_sample():
    """Test that a ValueError is raised with a constant sample."""
    discretizer = MultipleCoefficientBinning()
    msg_error = "At least one timestamp is constant."
    with pytest.raises(ValueError, match=msg_error):
        discretizer.fit_transform(np.ones((10, 15)))