Пример #1
0
def get_dft_coefs(X, n_coefs):
    """
    :param X: the training set  numpy array shape:[n_samples, n_time_steps]
    :param n_coefs: number of the coefficients
    :return: numpy array shape: [n_samples, n_coefs]
    """
    dft = DiscreteFourierTransform(n_coefs=n_coefs,
                                   norm_mean=False,
                                   norm_std=False)
    X_dft = dft.fit_transform(X)
    n_samples = len(X)
    if n_coefs % 2 == 0:
        real_idx = np.arange(1, n_coefs, 2)
        imag_idx = np.arange(2, n_coefs, 2)
        X_dft_new = np.c_[X_dft[:, :1],
                          X_dft[:, real_idx] + 1j * np.c_[X_dft[:, imag_idx],
                                                          np.zeros(
                                                              (n_samples, ))]]
    else:
        real_idx = np.arange(1, n_coefs, 2)
        imag_idx = np.arange(2, n_coefs + 1, 2)
        X_dft_new = np.c_[X_dft[:, :1],
                          X_dft[:, real_idx] + 1j * X_dft[:, imag_idx]]
    return X_dft_new
Пример #2
0
import numpy as np
import matplotlib.pyplot as plt
from pyts.approximation import DiscreteFourierTransform

# Parameters
n_samples, n_timestamps = 100, 48

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

# DFT transformation
n_coefs = 30
dft = DiscreteFourierTransform(n_coefs=n_coefs,
                               norm_mean=False,
                               norm_std=False)
X_dft = dft.fit_transform(X)

# Compute the inverse transformation
if n_coefs % 2 == 0:
    real_idx = np.arange(1, n_coefs, 2)
    imag_idx = np.arange(2, n_coefs, 2)
    X_dft_new = np.c_[X_dft[:, :1],
                      X_dft[:, real_idx] + 1j * np.c_[X_dft[:, imag_idx],
                                                      np.zeros((n_samples, ))]]
else:
    real_idx = np.arange(1, n_coefs, 2)
    imag_idx = np.arange(2, n_coefs + 1, 2)
    X_dft_new = np.c_[X_dft[:, :1],
                      X_dft[:, real_idx] + 1j * X_dft[:, imag_idx]]
X_irfft = np.fft.irfft(X_dft_new, n_timestamps)
Пример #3
0
reconstructed5 = ssa.view_reconstruction(*[ssa.Xs[i] for i in streams5],
                                         names=streams5,
                                         return_df=True)
ssa.forecast_recurrent(steps_ahead=48, singular_values=streams10, plot=True)

ts_copy5 = ssa.ts.copy()
ts_copy5['Reconstruction'] = reconstructed5.Reconstruction.values
ts_copy5.plot(title='Original vs. Reconstructed Time Series')
plt.show()

# Functioning Discrete Fourer Transform Approach
from pyts.approximation import DiscreteFourierTransform

n_coefs = 21
dft = DiscreteFourierTransform(n_coefs=n_coefs,
                               norm_mean=False,
                               norm_std=False)
X_dft = dft.fit_transform(ticker.daily.adjclose.values.reshape(1, -1))
n_samples, n_timestamps = 1, ticker.daily.shape[0]
if n_coefs % 2 == 0:
    real_idx = np.arange(1, n_coefs, 2)
    imag_idx = np.arange(2, n_coefs, 2)
    X_dft_new = np.c_[X_dft[:, :1],
                      X_dft[:, real_idx] + 1j * np.c_[X_dft[:, imag_idx],
                                                      np.zeros((n_samples, ))]]
else:
    real_idx = np.arange(1, n_coefs, 2)
    imag_idx = np.arange(2, n_coefs + 1, 2)
    X_dft_new = np.c_[X_dft[:, :1],
                      X_dft[:, real_idx] + 1j * X_dft[:, imag_idx]]
X_irfft = np.fft.irfft(X_dft_new, n_timestamps)
Пример #4
0
def test_actual_results(params):
    """Test that the actual results are the expected ones."""
    arr_actual = DiscreteFourierTransform(**params).fit_transform(X, y)
    arr_desired = _compute_expected_results(X, y, **params)
    np.testing.assert_array_equal(arr_actual, arr_desired)
Пример #5
0
def test_parameter_check(params, error, err_msg):
    """Test parameter validation."""
    dft = DiscreteFourierTransform(**params)
    with pytest.raises(error, match=re.escape(err_msg)):
        dft.fit(X, y)
Пример #6
0
def test_fit_transform(params):
    """Test that fit and transform yield the same results as fit_transform."""
    arr_1 = DiscreteFourierTransform(**params).fit(X, y).transform(X)
    arr_2 = DiscreteFourierTransform(**params).fit_transform(X, y)
    np.testing.assert_array_equal(arr_1, arr_2)